//go:build windows package main import ( "context" "fmt" "golang.org/x/sys/windows/svc" ) type windowsServiceHandler struct { run func(context.Context) error } func runPlatformService(name string, run func(context.Context) error) error { if err := svc.Run(name, windowsServiceHandler{run: run}); err != nil { return fmt.Errorf("Windows Service %q: %w", name, err) } return nil } func (h windowsServiceHandler) Execute(_ []string, requests <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) { const accepted = svc.AcceptStop | svc.AcceptShutdown status <- svc.Status{State: svc.StartPending} ctx, cancel := context.WithCancel(context.Background()) done := make(chan error, 1) go func() { done <- h.run(ctx) }() status <- svc.Status{State: svc.Running, Accepts: accepted} for { select { case request := <-requests: switch request.Cmd { case svc.Stop, svc.Shutdown: status <- svc.Status{State: svc.StopPending, Accepts: accepted} cancel() } case err := <-done: cancel() if err != nil { return true, 1 } return false, 0 } } }