@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 10, 22:30 UTC

master
go 67 lines 1.83 KB
Raw
1 // Copyright 2015 Matthew Holt and The Caddy Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
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 // Windows services always start in the system32 directory, try to
33 // switch into the directory where the caddy executable is.
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 // All status updates go through notify, which publishes the current
48 // status now: StartPending, or whatever Caddy reported before the
49 // service started. Detach before returning, since the dispatcher
50 // stops receiving status updates once Execute returns.
51 notify.SetGlobalStatus(status)
52 defer notify.SetGlobalStatus(nil)
53
54 for req := range request {
55 // Unsupported service controls are ignored.
56 //exhaustive:ignore
57 switch req.Cmd {
58 case svc.Interrogate:
59 notify.Interrogate()
60 case svc.Stop, svc.Shutdown:
61 _ = notify.Stopping()
62 exitProcessFromSignal("SIGINT")
63 return false, 0
64 }
65 }
66 return false, 0
67 }