From 81dcfe0da7a962d60b6cfd3020319ffcef11b27f Mon Sep 17 00:00:00 2001 From: Pylogmon Date: Sat, 11 May 2024 11:46:16 +0800 Subject: [PATCH] feat: Add /quit API --- hub/route/quit.go | 25 +++++++++++++++++++++++++ hub/route/server.go | 1 + main.go | 2 +- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 hub/route/quit.go diff --git a/hub/route/quit.go b/hub/route/quit.go new file mode 100644 index 00000000..46b2e0c1 --- /dev/null +++ b/hub/route/quit.go @@ -0,0 +1,25 @@ +package route + +import ( + "net/http" + "runtime" + "os" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/render" + "github.com/metacubex/mihomo/hub/executor" +) + +func quitRouter() http.Handler { + r := chi.NewRouter() + r.Get("/", quitCore) + return r +} + +func quitCore(w http.ResponseWriter, r *http.Request) { + render.JSON(w, r, render.M{"status": "ok"}) + executor.Shutdown() + if runtime.GOOS == "windows" { + os.Exit(0) + } +} \ No newline at end of file diff --git a/hub/route/server.go b/hub/route/server.go index c28782b9..8e325cb7 100644 --- a/hub/route/server.go +++ b/hub/route/server.go @@ -91,6 +91,7 @@ func router(isDebug bool, withAuth bool) *chi.Mux { r.Mount("/dns", dnsRouter()) r.Mount("/restart", restartRouter()) r.Mount("/upgrade", upgradeRouter()) + r.Mount("/quit", quitRouter()) addExternalRouters(r) }) diff --git a/main.go b/main.go index afe9cfd2..29adee6f 100644 --- a/main.go +++ b/main.go @@ -130,7 +130,7 @@ func main() { termSign := make(chan os.Signal, 1) hupSign := make(chan os.Signal, 1) - signal.Notify(termSign, syscall.SIGINT, syscall.SIGTERM) + signal.Notify(termSign, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL) signal.Notify(hupSign, syscall.SIGHUP) for { select {