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

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