2023-12-07 00:22:53 +08:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
2023-12-13 17:59:14 +08:00
|
|
|
"encoding/json"
|
2023-12-07 00:22:53 +08:00
|
|
|
"net/http"
|
2023-12-13 17:59:14 +08:00
|
|
|
"os"
|
|
|
|
"runtime"
|
2023-12-07 22:42:27 +08:00
|
|
|
|
2023-12-12 21:44:35 +08:00
|
|
|
"git.pyer.club/kingecg/gohttpd/model"
|
2023-12-07 22:42:27 +08:00
|
|
|
"git.pyer.club/kingecg/gohttpd/server"
|
2023-12-07 00:22:53 +08:00
|
|
|
)
|
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
type RunStatus struct {
|
|
|
|
Goroutines int `json:"goroutines"`
|
|
|
|
}
|
|
|
|
|
2023-12-07 00:22:53 +08:00
|
|
|
func about(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("About Page"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-11 18:15:29 +08:00
|
|
|
func setConfig(w http.ResponseWriter, r *http.Request) {
|
2023-12-13 14:36:49 +08:00
|
|
|
ctx := r.Context()
|
|
|
|
ctxData := ctx.Value(server.RequestCtxKey("ctxData")).(map[string]interface{})
|
|
|
|
data, ok := ctxData["data"]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t := data.(model.HttpServerConfig)
|
2023-12-13 17:59:14 +08:00
|
|
|
err := model.SetServerConfig(&t)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
w.Write(server.NewErrorResult(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(server.NewSuccessResult(t))
|
|
|
|
}
|
2023-12-11 18:15:29 +08:00
|
|
|
|
2023-12-13 17:59:14 +08:00
|
|
|
func getServerConfigure(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
ctxData := ctx.Value(server.RequestCtxKey("ctxData")).(map[string]interface{})
|
|
|
|
id, ok := ctxData["id"]
|
|
|
|
if ok {
|
|
|
|
data := model.GetServerConfig(id.(string))
|
|
|
|
configContent, _ := json.Marshal(data)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(server.NewSuccessResult(configContent))
|
|
|
|
} else {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
//获取当前进程的goroutines数量
|
|
|
|
ret := RunStatus{
|
|
|
|
Goroutines: runtime.NumGoroutine(),
|
|
|
|
}
|
|
|
|
rs, _ := json.Marshal(ret)
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(rs)
|
2023-12-11 18:15:29 +08:00
|
|
|
}
|
|
|
|
|
2023-12-07 22:42:27 +08:00
|
|
|
var AdminServerMux *server.RestMux
|
2023-12-07 00:22:53 +08:00
|
|
|
|
|
|
|
func init() {
|
2023-12-13 10:59:47 +08:00
|
|
|
AdminServerMux = server.NewRestMux("/api")
|
2023-12-12 21:44:35 +08:00
|
|
|
AdminServerMux.Use(server.BasicAuth)
|
|
|
|
AdminServerMux.HandleFunc("GET", "/about", http.HandlerFunc(about))
|
|
|
|
postConfigRoute := AdminServerMux.HandleFunc("POST", "/config", http.HandlerFunc(setConfig))
|
|
|
|
postConfigRoute.Add(server.Parse[model.HttpServerConfig])
|
2023-12-13 17:59:14 +08:00
|
|
|
AdminServerMux.HandleFunc("GET", "/config/:id", http.HandlerFunc(getServerConfigure))
|
|
|
|
AdminServerMux.HandleFunc("GET", "/status", http.HandlerFunc(getStatus))
|
2023-12-11 18:15:29 +08:00
|
|
|
AdminServerMux.Use(server.BasicAuth)
|
2023-12-07 00:22:53 +08:00
|
|
|
}
|