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}", "program": "${workspaceFolder}",
"env": { "env": {
"GODEBUG": "cgocheck=0", "GODEBUG": "cgocheck=0",
"_go_daemon": "g_dtasj" "_go_daemon": "g_dtask"
}, },
"args": [ "args": [
"run", "run",

View File

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

View File

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

View File

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