add shutdown core router

This commit is contained in:
xxnuo 2023-12-29 12:18:02 +08:00
parent 2e87c6f4da
commit ec5d90a604
2 changed files with 32 additions and 0 deletions

View file

@ -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)

31
hub/route/shutdown.go Normal file
View file

@ -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)
}