gohttp/admin/admin.go

92 lines
2.3 KiB
Go
Raw Permalink Normal View History

2023-12-07 00:22:53 +08:00
package admin
import (
"errors"
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()
2023-12-13 21:09:01 +08:00
ctxData := ctx.Value(server.RequestCtxKey("data")).(map[string]interface{})
2023-12-13 14:36:49 +08:00
data, ok := ctxData["data"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
return
}
t := data.(model.HttpServerConfig)
if t.Name == "admin" {
w.WriteHeader(http.StatusForbidden)
resp := server.NewErrorResult(errors.New("不能通过api设置管理服务器"))
w.Write(resp)
}
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()
2023-12-13 21:09:01 +08:00
ctxData := ctx.Value(server.RequestCtxKey("data")).(map[string]interface{})
2023-12-13 17:59:14 +08:00
id, ok := ctxData["id"]
if ok {
if id.(string) == "admin" {
w.WriteHeader(http.StatusForbidden)
resp := server.NewErrorResult(errors.New("不能通过api获取管理服务器配置信息"))
w.Write(resp)
return
}
2023-12-13 17:59:14 +08:00
data := model.GetServerConfig(id.(string))
2023-12-13 21:09:01 +08:00
/// configContent, _ := json.Marshal(data)
2023-12-13 17:59:14 +08:00
w.WriteHeader(http.StatusOK)
2023-12-13 21:09:01 +08:00
w.Write(server.NewSuccessResult(data))
2023-12-13 17:59:14 +08:00
} else {
http.NotFound(w, r)
}
}
func getStatus(w http.ResponseWriter, r *http.Request) {
//获取当前进程的goroutines数量
ret := RunStatus{
Goroutines: runtime.NumGoroutine(),
}
2023-12-13 21:09:01 +08:00
2023-12-13 17:59:14 +08:00
w.WriteHeader(http.StatusOK)
2023-12-13 21:09:01 +08:00
w.Write(server.NewSuccessResult(ret))
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
}