-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_windows.go
More file actions
91 lines (73 loc) · 1.95 KB
/
Copy pathservice_windows.go
File metadata and controls
91 lines (73 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//go:build windows
package main
import (
"context"
"log"
"time"
"golang.org/x/sys/windows/svc"
)
const serviceName = "VCollabWebRDP"
type vCollabService struct{}
func (s *vCollabService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const acceptedCmds = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Run the HTTP server in the background
errCh := make(chan error, 1)
go func() {
errCh <- runServer(ctx)
}()
changes <- svc.Status{State: svc.Running, Accepts: acceptedCmds}
for {
select {
case err := <-errCh:
if err != nil {
log.Printf("Server error: %v", err)
changes <- svc.Status{State: svc.StopPending}
return false, 1
}
changes <- svc.Status{State: svc.StopPending}
return false, 0
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
log.Println("Service stop/shutdown requested")
changes <- svc.Status{State: svc.StopPending}
cancel()
// Wait for server to finish
<-errCh
return false, 0
}
}
}
}
func runService() {
isService, err := svc.IsWindowsService()
if err != nil {
log.Fatalf("Failed to detect service mode: %v", err)
}
if isService {
// Running as a Windows service — hand off to SCM
log.Println("Running as Windows service")
if err := svc.Run(serviceName, &vCollabService{}); err != nil {
log.Fatalf("Service failed: %v", err)
}
return
}
// Running interactively — just run the server with signal handling
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Handle Ctrl+C for interactive use
go func() {
waitForSignal()
cancel()
}()
if err := runServer(ctx); err != nil {
log.Fatalf("Server failed: %v", err)
}
}