add global config support

This commit is contained in:
wjqserver 2025-06-30 15:31:14 +08:00
parent cd1e1a42f3
commit 34d553a890
23 changed files with 1682 additions and 343 deletions

View file

@ -53,6 +53,27 @@ func RenderConfig(site string, cdb *db.ConfigDB) error {
return nil
}
func RenderGlobalConfig(paramsGob []byte, tmplContent []byte) ([]byte, error) {
// 渲染caddyfile
var globalConfig CaddyGlobalConfig
err := DecodeGobConfig(paramsGob, &globalConfig)
if err != nil {
return nil, fmt.Errorf("failed to decode global config params: %w", err)
}
parsedTmpl, parseErr := template.New("caddyfile").Parse(string(tmplContent))
if parseErr != nil {
return nil, fmt.Errorf("failed to parse global caddyfile template: %w", parseErr)
}
var renderedContentBuilder bytes.Buffer
if err := parsedTmpl.Execute(&renderedContentBuilder, globalConfig); err != nil {
return nil, fmt.Errorf("failed to render global caddyfile template: %w", err)
}
return renderedContentBuilder.Bytes(), nil
}
// 把caddycfg内容转为GOB
func EncodeGobConfig(caddycfg any) ([]byte, error) {
var buf bytes.Buffer