update
This commit is contained in:
39
controllers/themes.go
Normal file
39
controllers/themes.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"go_blog/themes"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ListThemes 获取可用主题列表
|
||||
func ListThemes(c *gin.Context) {
|
||||
// 从主题管理器获取当前主题
|
||||
tm, exists := c.Get("ThemeManager")
|
||||
if !exists {
|
||||
c.JSON(500, gin.H{"error": "主题管理器未找到"})
|
||||
return
|
||||
}
|
||||
themeManager := tm.(*themes.ThemeManager)
|
||||
|
||||
// 读取 web/themes 目录下的所有主题文件夹
|
||||
themesDir := "web/themes"
|
||||
entries, err := os.ReadDir(themesDir)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": "读取主题目录失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var themeList []string
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
themeList = append(themeList, entry.Name())
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"current": themeManager.CurrentTheme(),
|
||||
"themes": themeList,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user