use mysql
This commit is contained in:
21
admin/thememanager.go
Normal file
21
admin/thememanager.go
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package admin
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go_blog/themes"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
)
|
||||||
|
|
||||||
|
var themeManager = themes.NewManager("themes")
|
||||||
|
var router = gin.Default()
|
||||||
|
|
||||||
|
// 切换主题(管理后台)
|
||||||
|
func switchTheme(c *gin.Context) {
|
||||||
|
newTheme := c.PostForm("theme")
|
||||||
|
if err := themeManager.LoadTheme(newTheme); err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
themeManager.RegisterStaticRoutes(router) // 重新注册静态路由
|
||||||
|
c.JSON(200, gin.H{"status": "success"})
|
||||||
|
}
|
||||||
@@ -14,6 +14,11 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EnvDevelopment = "development"
|
||||||
|
EnvProduction = "production"
|
||||||
|
)
|
||||||
|
|
||||||
type DataBaseConfig struct {
|
type DataBaseConfig struct {
|
||||||
Host string
|
Host string
|
||||||
Port string
|
Port string
|
||||||
|
|||||||
59
main.go
59
main.go
@@ -4,6 +4,9 @@ import (
|
|||||||
"go_blog/config"
|
"go_blog/config"
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/routers"
|
"go_blog/routers"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"go_blog/themes"
|
"go_blog/themes"
|
||||||
|
|
||||||
@@ -12,26 +15,52 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// @title 个人博客系统API
|
||||||
|
// @version 1.0
|
||||||
|
// @description 基于Go语言的可定制主题博客系统
|
||||||
const templatePath = "./templates/*"
|
const templatePath = "./templates/*"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
//读取配置文件
|
||||||
|
// 1. 初始化配置
|
||||||
conf, err := config.LoadConfig("config.yml")
|
conf, err := config.LoadConfig("config.yml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("配置文件加载失败: " + err.Error())
|
slog.Error("配置加载失败", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
models.InitDatabase(conf)
|
|
||||||
// 4. 初始化主题管理器
|
// 2. 初始化日志系统
|
||||||
themeManager := themes.NewManager()
|
// if err := logger.Initialize(cfg.Log); err != nil {
|
||||||
// if err := themeManager.LoadTheme(conf.GetCurrentTheme()); err != nil {
|
// slog.Error("日志初始化失败", "error", err)
|
||||||
// panic("主题加载失败: " + err.Error())
|
// os.Exit(1)
|
||||||
// }
|
// }
|
||||||
|
// defer logger.Flush()
|
||||||
|
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||||
|
slog.SetDefault(logger)
|
||||||
|
|
||||||
|
// 3. 初始化数据库
|
||||||
|
models.InitDatabase(conf)
|
||||||
|
|
||||||
|
// 4. 初始化主题系统
|
||||||
|
themeManager := themes.NewManager(conf.Theme.Current)
|
||||||
|
if err := themeManager.LoadTheme(conf.Theme.Current); err != nil {
|
||||||
|
slog.Error("主题系统初始化失败", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
// 5. 创建Gin实例
|
// 5. 创建Gin实例
|
||||||
|
// 根据环境设置Gin模式
|
||||||
|
if conf.Env == config.EnvProduction {
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
}
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.LoadHTMLGlob(templatePath)
|
router.LoadHTMLGlob(templatePath)
|
||||||
|
themeManager.RegisterStaticRoutes(router)
|
||||||
|
|
||||||
// 6. 注册中间件
|
// 6. 注册中间件
|
||||||
router.Use(
|
router.Use(
|
||||||
|
loggerMiddleware(),
|
||||||
gin.Logger(),
|
gin.Logger(),
|
||||||
gin.Recovery(),
|
gin.Recovery(),
|
||||||
databaseMiddleware(models.DB),
|
databaseMiddleware(models.DB),
|
||||||
@@ -45,7 +74,7 @@ func main() {
|
|||||||
// 8. 注册路由
|
// 8. 注册路由
|
||||||
routers.RegisterRoutes(router)
|
routers.RegisterRoutes(router)
|
||||||
// 9. 启动服务
|
// 9. 启动服务
|
||||||
router.Run(":" + conf.Server.Port) //router.Run()
|
router.Run(":" + conf.Server.Port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自定义模板渲染器
|
// 自定义模板渲染器
|
||||||
@@ -83,3 +112,19 @@ func themeMiddleware(manager *themes.ThemeManager) gin.HandlerFunc {
|
|||||||
c.Next()
|
c.Next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loggerMiddleware() gin.HandlerFunc {
|
||||||
|
return func(c *gin.Context) {
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
c.Next()
|
||||||
|
|
||||||
|
duration := time.Since(start)
|
||||||
|
slog.Info("请求处理完成",
|
||||||
|
"status", c.Writer.Status(),
|
||||||
|
"method", c.Request.Method,
|
||||||
|
"path", c.Request.URL.Path,
|
||||||
|
"duration", duration,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"go_blog/config"
|
"go_blog/config"
|
||||||
"go_blog/pkg/util"
|
"go_blog/pkg/util"
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
@@ -34,14 +36,21 @@ func InitDatabase(conf *config.Config) {
|
|||||||
TablePrefix: conf.DataBase.Prefix,
|
TablePrefix: conf.DataBase.Prefix,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
panic("数据库连接失败: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("数据库连接失败", "error", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
sqlDB, _ := db.DB()
|
||||||
|
defer func() {
|
||||||
|
if err := sqlDB.Close(); err != nil {
|
||||||
|
slog.Error("数据库关闭异常", "error", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
DB = db
|
DB = db
|
||||||
// 3. 自动迁移数据模型
|
// 3. 自动迁移数据模型
|
||||||
DB.AutoMigrate(&Account{})
|
db.AutoMigrate(&Account{})
|
||||||
DB.AutoMigrate(&Content{})
|
db.AutoMigrate(&Content{})
|
||||||
// if err := db.AutoMigrate(&models.Article{}, &models.User{}); err != nil {
|
// if err := db.AutoMigrate(&models.Article{}, &models.User{}); err != nil {
|
||||||
// panic("数据库迁移失败: " + err.Error())
|
// panic("数据库迁移失败: " + err.Error())
|
||||||
// }
|
// }
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func RegisterRoutes(r *gin.Engine) {
|
func RegisterRoutes(r *gin.Engine) {
|
||||||
@@ -21,7 +22,20 @@ func RegisterRoutes(r *gin.Engine) {
|
|||||||
var pager serializers.Pager
|
var pager serializers.Pager
|
||||||
pager.InitPager(c)
|
pager.InitPager(c)
|
||||||
offset := (pager.Page - 1) * pager.PageSize
|
offset := (pager.Page - 1) * pager.PageSize
|
||||||
models.DB.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post")
|
if dbInterface, ok := c.Get("DB"); ok {
|
||||||
|
if db, ok := dbInterface.(*gorm.DB); ok {
|
||||||
|
db.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post")
|
||||||
|
} else {
|
||||||
|
log.Println("无法将 DB 转换为 *gorm.DB 类型")
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部服务器错误"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.Println("未找到键 'DB' 的上下文值")
|
||||||
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部服务器错误"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||||
"Items": items,
|
"Items": items,
|
||||||
"Pager": pager,
|
"Pager": pager,
|
||||||
|
|||||||
@@ -1,57 +1,164 @@
|
|||||||
package themes // Declare the package at the top
|
package themes
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"embed"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ThemeManager manages themes for the blog system
|
// 主题元数据结构
|
||||||
type ThemeManager struct {
|
type ThemeMeta struct {
|
||||||
CurrentTheme string // Name of the currently active theme
|
Name string `yaml:"name"`
|
||||||
Templates map[string]*template.Template // Cached compiled HTML templates
|
Author string `yaml:"author"`
|
||||||
|
Version string `yaml:"version"`
|
||||||
|
Description string `yaml:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadTheme loads a specified theme by its name
|
// 主题管理器
|
||||||
func (tm *ThemeManager) LoadTheme(themeName string) error {
|
type ThemeManager struct {
|
||||||
// 1. 读取theme.yaml验证主题有效性
|
mu sync.RWMutex
|
||||||
// 2. 预编译所有HTML模板,缓存到Templates
|
currentTheme string
|
||||||
// 3. 注册静态资源路由:/themes/[name]/static/*filepath
|
templates map[string]*template.Template
|
||||||
tm.CurrentTheme = themeName
|
baseTemplates embed.FS // 可选:嵌入基础模板
|
||||||
|
themesDir string
|
||||||
|
}
|
||||||
|
|
||||||
// Step 1: Validate the theme by reading theme.yaml
|
// 创建新主题管理器
|
||||||
themeConfigPath := fmt.Sprintf("h:/code/go_blog/web/themes/%s/theme.yaml", themeName)
|
func NewManager(themesDir string) *ThemeManager {
|
||||||
if _, err := os.Stat(themeConfigPath); os.IsNotExist(err) {
|
return &ThemeManager{
|
||||||
return fmt.Errorf("theme %s does not exist or is invalid", themeName)
|
templates: make(map[string]*template.Template),
|
||||||
|
themesDir: themesDir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 核心方法:加载主题
|
||||||
|
func (m *ThemeManager) LoadTheme(themeName string) error {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
// 1. 验证主题目录结构
|
||||||
|
themePath := filepath.Join(m.themesDir, themeName)
|
||||||
|
if !isValidTheme(themePath) {
|
||||||
|
return fmt.Errorf("invalid theme structure: %s", themeName)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 2: Precompile all HTML templates and cache them
|
// 2. 加载模板文件
|
||||||
tm.Templates = make(map[string]*template.Template)
|
tpls, err := m.parseTemplates(themePath)
|
||||||
templateDir := fmt.Sprintf("h:/code/go_blog/web/themes/%s/tmplates/", themeName)
|
if err != nil {
|
||||||
err := filepath.Walk(templateDir, func(path string, info os.FileInfo, err error) error {
|
return fmt.Errorf("template parsing failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 更新当前主题
|
||||||
|
m.currentTheme = themeName
|
||||||
|
m.templates = tpls
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前主题名称
|
||||||
|
func (m *ThemeManager) CurrentTheme() string {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
return m.currentTheme
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取编译后的模板
|
||||||
|
func (m *ThemeManager) GetTemplate(name string) *template.Template {
|
||||||
|
m.mu.RLock()
|
||||||
|
defer m.mu.RUnlock()
|
||||||
|
return m.templates[name]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 注册静态文件路由(Gin框架)
|
||||||
|
func (m *ThemeManager) RegisterStaticRoutes(router *gin.Engine) {
|
||||||
|
staticPath := filepath.Join(m.themesDir, m.currentTheme, "static")
|
||||||
|
router.StaticFS("/theme/static", gin.Dir(staticPath, false))
|
||||||
|
}
|
||||||
|
|
||||||
|
// 校验主题完整性
|
||||||
|
func isValidTheme(themePath string) bool {
|
||||||
|
requiredFiles := []string{
|
||||||
|
"theme.yaml",
|
||||||
|
"templates/home.html",
|
||||||
|
"templates/post.html",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, f := range requiredFiles {
|
||||||
|
if _, err := os.Stat(filepath.Join(themePath, f)); os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析模板文件(支持布局继承)
|
||||||
|
func (m *ThemeManager) parseTemplates(themePath string) (map[string]*template.Template, error) {
|
||||||
|
templates := make(map[string]*template.Template)
|
||||||
|
|
||||||
|
// 加载公共基础模板(可选)
|
||||||
|
baseTpl := template.New("base").Funcs(template.FuncMap{
|
||||||
|
"safeHTML": func(s string) template.HTML { return template.HTML(s) },
|
||||||
|
})
|
||||||
|
|
||||||
|
// 从主题目录加载模板
|
||||||
|
tplDir := filepath.Join(themePath, "templates")
|
||||||
|
err := filepath.WalkDir(tplDir, func(path string, d fs.DirEntry, err error) error {
|
||||||
|
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".html") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 读取模板内容
|
||||||
|
content, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".tmpl") {
|
|
||||||
tmpl, err := template.ParseFiles(path)
|
// 生成模板名称(相对路径)
|
||||||
|
name := strings.TrimPrefix(path, tplDir+string(filepath.Separator))
|
||||||
|
name = strings.TrimSuffix(name, filepath.Ext(name))
|
||||||
|
|
||||||
|
// 克隆基础模板并解析
|
||||||
|
tpl := template.Must(baseTpl.Clone())
|
||||||
|
tpl, err = tpl.Parse(string(content))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return fmt.Errorf("parse error in %s: %w", name, err)
|
||||||
}
|
|
||||||
tm.Templates[info.Name()] = tmpl
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templates[name] = tpl
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load templates: %v", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Register static resource routes
|
if len(templates) == 0 {
|
||||||
http.Handle(fmt.Sprintf("/themes/%s/static/", themeName), http.StripPrefix(fmt.Sprintf("/themes/%s/static/", themeName), http.FileServer(http.Dir(fmt.Sprintf("h:/code/go_blog/themes/%s/static", themeName)))))
|
return nil, errors.New("no valid templates found")
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return templates, nil
|
||||||
}
|
}
|
||||||
func NewManager() *ThemeManager {
|
|
||||||
return &ThemeManager{}
|
// 获取可用主题列表
|
||||||
|
func (m *ThemeManager) ListThemes() ([]string, error) {
|
||||||
|
dirs, err := os.ReadDir(m.themesDir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var themes []string
|
||||||
|
for _, d := range dirs {
|
||||||
|
if d.IsDir() && isValidTheme(filepath.Join(m.themesDir, d.Name())) {
|
||||||
|
themes = append(themes, d.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return themes, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
name: "Default Theme"
|
||||||
|
author: "Blog System"
|
||||||
|
version: "1.0.0"
|
||||||
|
description: "Default clean theme for the blog"
|
||||||
Reference in New Issue
Block a user