diff --git a/hub/route/server.go b/hub/route/server.go index 8e7f225f..fe0198ca 100644 --- a/hub/route/server.go +++ b/hub/route/server.go @@ -93,6 +93,7 @@ func Start(addr string, tlsAddr string, secret string, r.Mount("/cache", cacheRouter()) r.Mount("/dns", dnsRouter()) r.Mount("/restart", restartRouter()) + r.Mount("/shutdown", shutdownRouter()) r.Mount("/upgrade", upgradeRouter()) addExternalRouters(r) diff --git a/hub/route/shutdown.go b/hub/route/shutdown.go new file mode 100644 index 00000000..b26be26f --- /dev/null +++ b/hub/route/shutdown.go @@ -0,0 +1,31 @@ +package route + +import ( + "net/http" + "os" + + "github.com/metacubex/mihomo/hub/executor" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/render" +) + +func shutdownRouter() http.Handler { + r := chi.NewRouter() + r.Post("/", shutdown) + return r +} + +func shutdown(w http.ResponseWriter, r *http.Request) { + render.JSON(w, r, render.M{"status": "ok"}) + if f, ok := w.(http.Flusher); ok { + f.Flush() + } + + go shutdownExecutable() +} + +func shutdownExecutable() { + executor.Shutdown() + os.Exit(0) +}