81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package admin
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"os"
|
|
"runtime"
|
|
|
|
"git.pyer.club/kingecg/gohttpd/model"
|
|
"git.pyer.club/kingecg/gohttpd/server"
|
|
)
|
|
|
|
type RunStatus struct {
|
|
Goroutines int `json:"goroutines"`
|
|
}
|
|
|
|
func about(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("About Page"))
|
|
|
|
}
|
|
|
|
func setConfig(w http.ResponseWriter, r *http.Request) {
|
|
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)
|
|
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))
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
var AdminServerMux *server.RestMux
|
|
|
|
func init() {
|
|
AdminServerMux = server.NewRestMux("/api")
|
|
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])
|
|
AdminServerMux.HandleFunc("GET", "/config/:id", http.HandlerFunc(getServerConfigure))
|
|
AdminServerMux.HandleFunc("GET", "/status", http.HandlerFunc(getStatus))
|
|
AdminServerMux.Use(server.BasicAuth)
|
|
}
|