86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"go_blog/config"
|
|
"go_blog/models"
|
|
"go_blog/routers"
|
|
|
|
"go_blog/themes"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const templatePath = "./templates/*"
|
|
|
|
func main() {
|
|
conf, err := config.LoadConfig("config.yml")
|
|
if err != nil {
|
|
panic("配置文件加载失败: " + err.Error())
|
|
}
|
|
models.InitDatabase(conf)
|
|
// 4. 初始化主题管理器
|
|
themeManager := themes.NewManager()
|
|
// if err := themeManager.LoadTheme(conf.GetCurrentTheme()); err != nil {
|
|
// panic("主题加载失败: " + err.Error())
|
|
// }
|
|
|
|
// 5. 创建Gin实例
|
|
router := gin.Default()
|
|
router.LoadHTMLGlob(templatePath)
|
|
|
|
// 6. 注册中间件
|
|
router.Use(
|
|
gin.Logger(),
|
|
gin.Recovery(),
|
|
databaseMiddleware(models.DB),
|
|
//configMiddleware(cfg),
|
|
themeMiddleware(themeManager),
|
|
)
|
|
|
|
// 7. 设置模板路径
|
|
//router.HTMLRender = createTemplateRenderer(cfg.Theme.Current)
|
|
|
|
// 8. 注册路由
|
|
routers.RegisterRoutes(router)
|
|
// 9. 启动服务
|
|
router.Run(":" + conf.Server.Port) //router.Run()
|
|
}
|
|
|
|
// 自定义模板渲染器
|
|
// func createTemplateRenderer(currentTheme string) gin.HTMLRender {
|
|
// return gin.HTMLTemplates{
|
|
// Root: "web/templates",
|
|
// Extension: ".html",
|
|
// FuncMap: template.FuncMap{
|
|
// "safe": func(s string) template.HTML { return template.HTML(s) },
|
|
// },
|
|
// Directory: "web/themes/" + currentTheme,
|
|
// }
|
|
// }
|
|
|
|
// 数据库中间件
|
|
func databaseMiddleware(db *gorm.DB) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Set("DB", db)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// 配置上下文中间件
|
|
func configMiddleware(cfg *config.Config) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Set("Config", cfg)
|
|
c.Next()
|
|
}
|
|
}
|
|
|
|
// 主题管理器中间件
|
|
func themeMiddleware(manager *themes.ThemeManager) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
c.Set("ThemeManager", manager)
|
|
c.Next()
|
|
}
|
|
}
|