@marcel91200 / caddy Mirror · read-only / commits / d98f2fae
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 14, 05:15 UTC

Add /stop endpoint to admin (#2671)

* Add stop command to admin. Exit after stop. * Return error on incorrect http Method and provide better logging. * reuse stopAndCleanup function for all graceful stops

Toby Allen committed Jul 20, 2019 at 17:48 UTC d98f2faef960f549c973d747d2e7c484d6e845c7
2 files changed +26 -8
admin.go
+25 -1
@@ -24,10 +24,12 @@ import (
24 "net"
25 "net/http"
26 "net/http/pprof"
27 + "os"
28 "strings"
29 "sync"
30 "time"
31
32 + "github.com/mholt/certmagic"
33 "github.com/rs/cors"
34 )
35
@@ -83,6 +85,7 @@ func StartAdmin(initialConfigJSON []byte) error {
85
86 mux := http.NewServeMux()
87 mux.HandleFunc("/load", handleLoadConfig)
88 + mux.HandleFunc("/stop", handleStop)
89
90 ///// BEGIN PPROF STUFF (TODO: Temporary) /////
91 mux.HandleFunc("/debug/pprof/", pprof.Index)
@@ -149,7 +152,7 @@ type AdminRoute struct {
152
153 func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
154 r.Close = true
152 - if r.Method != "POST" {
155 + if r.Method != http.MethodPost {
156 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
157 return
158 }
@@ -167,6 +170,27 @@ func handleLoadConfig(w http.ResponseWriter, r *http.Request) {
170 }
171 }
172
173 +func handleStop(w http.ResponseWriter, r *http.Request) {
174 + if r.Method != http.MethodPost {
175 + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
176 + return
177 + }
178 + log.Println("[ADMIN] Initiating shutdown")
179 + if err := stopAndCleanup(); err != nil {
180 + log.Printf("[ADMIN][ERROR] stopping: %v \n", err)
181 + }
182 + log.Println("[ADMIN] Exiting")
183 + os.Exit(0)
184 +}
185 +
186 +func stopAndCleanup() error {
187 + if err := Stop(); err != nil {
188 + return err
189 + }
190 + certmagic.CleanUpOwnLocks()
191 + return nil
192 +}
193 +
194 // Load loads and starts a configuration.
195 func Load(r io.Reader) error {
196 buf := bufPool.Get().(*bytes.Buffer)
sigtrap.go
+1 -7
@@ -18,8 +18,6 @@ import (
18 "log"
19 "os"
20 "os/signal"
21 -
22 - "github.com/mholt/certmagic"
21 )
22
23 // TrapSignals create signal/interrupt handlers as best it can for the
@@ -57,16 +55,12 @@ func trapSignalsCrossPlatform() {
55 func gracefulStop(sigName string) {
56 exitCode := ExitCodeSuccess
57
60 - // first stop all the apps
61 - err := Stop()
58 + err := stopAndCleanup()
59 if err != nil {
60 log.Printf("[ERROR] %s stop: %v", sigName, err)
61 exitCode = ExitCodeFailedQuit
62 }
63
67 - // always, always, always try to clean up locks
68 - certmagic.CleanUpOwnLocks()
69 -
64 log.Printf("[INFO] %s: Shutdown done", sigName)
65 os.Exit(exitCode)
66 }