From 7f32c1a38fa955736a896ec05719bcd94c88c18f Mon Sep 17 00:00:00 2001 From: kingecg Date: Wed, 13 Dec 2023 21:09:01 +0800 Subject: [PATCH] fix REST api --- .vscode/launch.json | 2 +- admin/admin.go | 13 ++++++------- config.json | 4 ++-- gohttp.go | 8 ++++---- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 55c594c..cf6b164 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -12,7 +12,7 @@ "program": "${workspaceFolder}", "env": { "GODEBUG": "cgocheck=0", - "_go_daemon": "g_dtasj" + "_go_daemon": "g_dtask" }, "args": [ "run", diff --git a/admin/admin.go b/admin/admin.go index 1d16944..898e205 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -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 diff --git a/config.json b/config.json index 8d16d4b..c9cbfe0 100644 --- a/config.json +++ b/config.json @@ -30,11 +30,11 @@ }, "servers":[{ "port" : 8080, - "name":"test", + "name":"demo", "paths":[ { "path": "/", - "root": "/home/kingecg/code/gohttp/public/", + "root": "./public/", "default": "index.html" }, { diff --git a/gohttp.go b/gohttp.go index e0cf1ee..8f6b154 100644 --- a/gohttp.go +++ b/gohttp.go @@ -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) } }