feat: Add /quit API

This commit is contained in:
Pylogmon 2024-05-11 11:46:16 +08:00
parent 6d1c62bbf0
commit 81dcfe0da7
3 changed files with 27 additions and 1 deletions

25
hub/route/quit.go Normal file
View file

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

View file

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

View file

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