fix dashboard custom theme, expose HideForGuest for api (#434)

* fix: dashboard custom theme

* api: expose HideForGuest
This commit is contained in:
UUBulb 2024-10-10 13:38:09 +08:00 committed by GitHub
parent 55f5c89c1c
commit 0b7f43b149
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 10 deletions

View File

@ -84,7 +84,7 @@ func routers(r *gin.Engine) {
} }
func loadThirdPartyTemplates(tmpl *template.Template) *template.Template { func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
var ret = tmpl ret := tmpl
themes, err := os.ReadDir("resource/template") themes, err := os.ReadDir("resource/template")
if err != nil { if err != nil {
log.Printf("NEZHA>> Error reading themes folder: %v", err) log.Printf("NEZHA>> Error reading themes folder: %v", err)
@ -96,6 +96,12 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
} }
themeDir := theme.Name() themeDir := theme.Name()
if strings.HasPrefix(themeDir, "dashboard-") {
// load dashboard templates, ignore desc file
ret = loadTemplates(ret, themeDir)
continue
}
if !strings.HasPrefix(themeDir, "theme-") { if !strings.HasPrefix(themeDir, "theme-") {
log.Printf("NEZHA>> Invalid theme name: %s", themeDir) log.Printf("NEZHA>> Invalid theme name: %s", themeDir)
continue continue
@ -115,22 +121,27 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
} }
// load templates // load templates
templatePath := filepath.Join("resource", "template", themeDir, "*.html") ret = loadTemplates(ret, themeDir)
t, err := ret.ParseGlob(templatePath)
if err != nil {
log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err)
continue
}
themeKey := strings.TrimPrefix(themeDir, "theme-") themeKey := strings.TrimPrefix(themeDir, "theme-")
model.Themes[themeKey] = themeName.String() model.Themes[themeKey] = themeName.String()
ret = t
} }
return ret return ret
} }
func loadTemplates(tmpl *template.Template, themeDir string) *template.Template {
// load templates
templatePath := filepath.Join("resource", "template", themeDir, "*.html")
t, err := tmpl.ParseGlob(templatePath)
if err != nil {
log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err)
return tmpl
}
return t
}
var funcMap = template.FuncMap{ var funcMap = template.FuncMap{
"tr": func(id string, dataAndCount ...interface{}) string { "tr": func(id string, dataAndCount ...interface{}) string {
conf := i18n.LocalizeConfig{ conf := i18n.LocalizeConfig{

View File

@ -940,7 +940,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
return return
} }
if _, yes := model.Themes[sf.DashboardTheme]; !yes { if _, yes := model.DashboardThemes[sf.DashboardTheme]; !yes {
c.JSON(http.StatusOK, model.Response{ c.JSON(http.StatusOK, model.Response{
Code: http.StatusBadRequest, Code: http.StatusBadRequest,
Message: fmt.Sprintf("后台主题不存在:%s", sf.DashboardTheme), Message: fmt.Sprintf("后台主题不存在:%s", sf.DashboardTheme),

View File

@ -34,6 +34,7 @@ type CommonServerInfo struct {
IPV6 string `json:"ipv6"` IPV6 string `json:"ipv6"`
ValidIP string `json:"valid_ip"` ValidIP string `json:"valid_ip"`
DisplayIndex int `json:"display_index"` DisplayIndex int `json:"display_index"`
HideForGuest bool `json:"hide_for_guest"`
} }
// StatusResponse 服务器状态子结构 包含服务器信息与状态信息 // StatusResponse 服务器状态子结构 包含服务器信息与状态信息
@ -150,6 +151,7 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
IPV6: ipv6, IPV6: ipv6,
ValidIP: validIP, ValidIP: validIP,
DisplayIndex: v.DisplayIndex, DisplayIndex: v.DisplayIndex,
HideForGuest: v.HideForGuest,
} }
res.Result = append(res.Result, &StatusResponse{ res.Result = append(res.Result, &StatusResponse{
CommonServerInfo: info, CommonServerInfo: info,