fix REST api

This commit is contained in:
kingecg 2023-12-13 21:09:01 +08:00
parent 6e63b5f0af
commit 7f32c1a38f
4 changed files with 13 additions and 14 deletions

2
.vscode/launch.json vendored
View File

@ -12,7 +12,7 @@
"program": "${workspaceFolder}",
"env": {
"GODEBUG": "cgocheck=0",
"_go_daemon": "g_dtasj"
"_go_daemon": "g_dtask"
},
"args": [
"run",

View File

@ -1,7 +1,6 @@
package admin
import (
"encoding/json"
"net/http"
"os"
"runtime"
@ -22,7 +21,7 @@ func about(w http.ResponseWriter, r *http.Request) {
func setConfig(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctxData := ctx.Value(server.RequestCtxKey("ctxData")).(map[string]interface{})
ctxData := ctx.Value(server.RequestCtxKey("data")).(map[string]interface{})
data, ok := ctxData["data"]
if !ok {
w.WriteHeader(http.StatusBadRequest)
@ -44,13 +43,13 @@ func setConfig(w http.ResponseWriter, r *http.Request) {
func getServerConfigure(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctxData := ctx.Value(server.RequestCtxKey("ctxData")).(map[string]interface{})
ctxData := ctx.Value(server.RequestCtxKey("data")).(map[string]interface{})
id, ok := ctxData["id"]
if ok {
data := model.GetServerConfig(id.(string))
configContent, _ := json.Marshal(data)
/// configContent, _ := json.Marshal(data)
w.WriteHeader(http.StatusOK)
w.Write(server.NewSuccessResult(configContent))
w.Write(server.NewSuccessResult(data))
} else {
http.NotFound(w, r)
}
@ -61,9 +60,9 @@ func getStatus(w http.ResponseWriter, r *http.Request) {
ret := RunStatus{
Goroutines: runtime.NumGoroutine(),
}
rs, _ := json.Marshal(ret)
w.WriteHeader(http.StatusOK)
w.Write(rs)
w.Write(server.NewSuccessResult(ret))
}
var AdminServerMux *server.RestMux

View File

@ -30,11 +30,11 @@
},
"servers":[{
"port" : 8080,
"name":"test",
"name":"demo",
"paths":[
{
"path": "/",
"root": "/home/kingecg/code/gohttp/public/",
"root": "./public/",
"default": "index.html"
},
{

View File

@ -23,7 +23,7 @@ func (g *GoHttp) Start() {
g.logger.Info("start gohttpd")
// if g.conf != nil {
adminHandler := g.assembleServerMux(conf.Admin.Paths)
adminHandler.(*http.ServeMux).Handle("/api/", admin.AdminServerMux)
adminHandler.(*http.ServeMux).Handle("/api/", http.StripPrefix("/api", admin.AdminServerMux))
g.makeServer(conf.Admin, adminHandler)
for _, server := range conf.Servers {
@ -97,13 +97,13 @@ func LoadConfig() {
func normalizeServer(server *model.HttpServerConfig) {
for index, path := range server.Paths {
if path.Root != "" {
server.Paths[index].Root = NormalizePath(path.Root)
server.Paths[index].Root = utils.NormalizePath(path.Root)
}
}
if server.CertFile != "" {
server.CertFile = NormalizePath(server.CertFile)
server.CertFile = utils.NormalizePath(server.CertFile)
}
if server.KeyFile != "" {
server.KeyFile = NormalizePath(server.KeyFile)
server.KeyFile = utils.NormalizePath(server.KeyFile)
}
}