diff options
author | Toby Allen <[email protected]> | 2019-07-20 17:48:46 +0100 |
---|---|---|
committer | Matt Holt <[email protected]> | 2019-07-20 10:48:46 -0600 |
commit | d98f2faef960f549c973d747d2e7c484d6e845c7 (patch) | |
tree | 142bc82f1797c6024c7b2e0e4b5c31d35c3e2da7 /admin.go | |
parent | b855e661700fa8f9176f29c3c5b1cebd4cc09351 (diff) | |
download | caddy-d98f2faef960f549c973d747d2e7c484d6e845c7.tar.gz caddy-d98f2faef960f549c973d747d2e7c484d6e845c7.zip |
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
Diffstat (limited to 'admin.go')
-rw-r--r-- | admin.go | 26 |
1 files changed, 25 insertions, 1 deletions
@@ -24,10 +24,12 @@ import ( "net" "net/http" "net/http/pprof" + "os" "strings" "sync" "time" + "github.com/mholt/certmagic" "github.com/rs/cors" ) @@ -83,6 +85,7 @@ func StartAdmin(initialConfigJSON []byte) error { mux := http.NewServeMux() mux.HandleFunc("/load", handleLoadConfig) + mux.HandleFunc("/stop", handleStop) ///// BEGIN PPROF STUFF (TODO: Temporary) ///// mux.HandleFunc("/debug/pprof/", pprof.Index) @@ -149,7 +152,7 @@ type AdminRoute struct { func handleLoadConfig(w http.ResponseWriter, r *http.Request) { r.Close = true - if r.Method != "POST" { + if r.Method != http.MethodPost { http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) return } @@ -167,6 +170,27 @@ func handleLoadConfig(w http.ResponseWriter, r *http.Request) { } } +func handleStop(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + log.Println("[ADMIN] Initiating shutdown") + if err := stopAndCleanup(); err != nil { + log.Printf("[ADMIN][ERROR] stopping: %v \n", err) + } + log.Println("[ADMIN] Exiting") + os.Exit(0) +} + +func stopAndCleanup() error { + if err := Stop(); err != nil { + return err + } + certmagic.CleanUpOwnLocks() + return nil +} + // Load loads and starts a configuration. func Load(r io.Reader) error { buf := bufPool.Get().(*bytes.Buffer) |