Skip to content

Commit

Permalink
fix dashboard custom theme, expose HideForGuest for api (#434)
Browse files Browse the repository at this point in the history
* fix: dashboard custom theme

* api: expose HideForGuest
  • Loading branch information
uubulb authored Oct 10, 2024
1 parent 55f5c89 commit 0b7f43b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
29 changes: 20 additions & 9 deletions cmd/dashboard/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func routers(r *gin.Engine) {
}

func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
var ret = tmpl
ret := tmpl
themes, err := os.ReadDir("resource/template")
if err != nil {
log.Printf("NEZHA>> Error reading themes folder: %v", err)
Expand All @@ -96,6 +96,12 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
}

themeDir := theme.Name()
if strings.HasPrefix(themeDir, "dashboard-") {
// load dashboard templates, ignore desc file
ret = loadTemplates(ret, themeDir)
continue
}

if !strings.HasPrefix(themeDir, "theme-") {
log.Printf("NEZHA>> Invalid theme name: %s", themeDir)
continue
Expand All @@ -115,22 +121,27 @@ func loadThirdPartyTemplates(tmpl *template.Template) *template.Template {
}

// load templates
templatePath := filepath.Join("resource", "template", themeDir, "*.html")
t, err := ret.ParseGlob(templatePath)
if err != nil {
log.Printf("NEZHA>> Error parsing templates %s: %v", themeDir, err)
continue
}
ret = loadTemplates(ret, themeDir)

themeKey := strings.TrimPrefix(themeDir, "theme-")
model.Themes[themeKey] = themeName.String()

ret = t
}

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{
"tr": func(id string, dataAndCount ...interface{}) string {
conf := i18n.LocalizeConfig{
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/controller/member_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ func (ma *memberAPI) updateSetting(c *gin.Context) {
return
}

if _, yes := model.Themes[sf.DashboardTheme]; !yes {
if _, yes := model.DashboardThemes[sf.DashboardTheme]; !yes {
c.JSON(http.StatusOK, model.Response{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("后台主题不存在:%s", sf.DashboardTheme),
Expand Down
2 changes: 2 additions & 0 deletions service/singleton/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CommonServerInfo struct {
IPV6 string `json:"ipv6"`
ValidIP string `json:"valid_ip"`
DisplayIndex int `json:"display_index"`
HideForGuest bool `json:"hide_for_guest"`
}

// StatusResponse 服务器状态子结构 包含服务器信息与状态信息
Expand Down Expand Up @@ -150,6 +151,7 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
IPV6: ipv6,
ValidIP: validIP,
DisplayIndex: v.DisplayIndex,
HideForGuest: v.HideForGuest,
}
res.Result = append(res.Result, &StatusResponse{
CommonServerInfo: info,
Expand Down

0 comments on commit 0b7f43b

Please sign in to comment.