"feat(config): 支持JSON流式解析以处理多服务器配置"

This commit is contained in:
程广 2025-06-24 12:56:45 +08:00
parent eecfc51c1e
commit dc5528c558
2 changed files with 31 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"
"git.pyer.club/kingecg/gohttpd/admin"
"git.pyer.club/kingecg/gohttpd/model"
@ -113,17 +114,28 @@ func LoadConfig() {
configs, err := filepath.Glob(model.Config.IncludDir + "/*.json")
if err == nil {
for _, config := range configs {
server := model.HttpServerConfig{}
// read config content and unmarshal
content, err := os.ReadFile(config)
if err == nil {
err = json.Unmarshal(content, &server)
decoder := json.NewDecoder(strings.NewReader(string(content)))
for decoder.More() {
server := model.HttpServerConfig{}
err = decoder.Decode(&server)
if err == nil {
server.ConfPath = config
normalizeServer(&server)
model.Config.Servers = append(model.Config.Servers, &server)
} else {
break
}
}
// if err == nil {
// server.ConfPath = config
// normalizeServer(&server)
// }
}
}
}
}

View File

@ -11,3 +11,16 @@
}
]
}
{
"name": "ahttp",
"server": ["www.a.com"],
"port": 8089,
"enable_ssl":false,
"paths": [
{
"path": "/",
"root": "./example",
"default": "index.html"
}
]
}