| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package caddy |
| 16 |
|
| 17 |
import ( |
| 18 |
"os" |
| 19 |
"path/filepath" |
| 20 |
|
| 21 |
"golang.org/x/sys/windows/svc" |
| 22 |
|
| 23 |
"github.com/caddyserver/caddy/v2/notify" |
| 24 |
) |
| 25 |
|
| 26 |
func init() { |
| 27 |
isService, err := svc.IsWindowsService() |
| 28 |
if err != nil || !isService { |
| 29 |
return |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
execPath, err := os.Executable() |
| 35 |
if err == nil { |
| 36 |
_ = os.Chdir(filepath.Dir(execPath)) |
| 37 |
} |
| 38 |
|
| 39 |
go func() { |
| 40 |
_ = svc.Run("", runner{}) |
| 41 |
}() |
| 42 |
} |
| 43 |
|
| 44 |
type runner struct{} |
| 45 |
|
| 46 |
func (runner) Execute(args []string, request <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) { |
| 47 |
notify.SetGlobalStatus(status) |
| 48 |
status <- svc.Status{State: svc.StartPending} |
| 49 |
|
| 50 |
for { |
| 51 |
req := <-request |
| 52 |
switch req.Cmd { |
| 53 |
case svc.Interrogate: |
| 54 |
status <- req.CurrentStatus |
| 55 |
case svc.Stop, svc.Shutdown: |
| 56 |
status <- svc.Status{State: svc.StopPending} |
| 57 |
exitProcessFromSignal("SIGINT") |
| 58 |
return false, 0 |
| 59 |
} |
| 60 |
} |
| 61 |
} |