This commit is contained in:
张超
2025-07-12 15:56:36 +08:00
parent 117902d4d8
commit 5fd7411f9a
11 changed files with 66 additions and 97 deletions

View File

@@ -3,9 +3,9 @@ package routers // Add the package declaration at the top
import (
"fmt"
"go_blog/controllers"
"go_blog/controllers/admin"
"go_blog/models"
"go_blog/serializers"
"go_blog/themes" // <-- 确保导入 themes 包
"go_blog/utils"
"log"
"net/http"
"strconv"
@@ -73,14 +73,14 @@ func RegisterRoutes(r *gin.Engine) {
c.String(http.StatusInternalServerError, "Theme manager not found")
return
}
themeManager, ok := tm.(*themes.ThemeManager)
themeManager, ok := tm.(*utils.ThemeManager)
if !ok {
c.String(http.StatusInternalServerError, "Invalid theme manager type")
return
}
var items []models.Content
var pager serializers.Pager
var pager utils.Pager
pager.InitPager(c) // 这会从查询参数中读取 page 和 pageSize
offset := (pager.Page - 1) * pager.PageSize
@@ -126,7 +126,7 @@ func RegisterRoutes(r *gin.Engine) {
c.String(http.StatusInternalServerError, "Theme manager not found")
return
}
themeManager, ok := tm.(*themes.ThemeManager)
themeManager, ok := tm.(*utils.ThemeManager)
if !ok {
c.String(http.StatusInternalServerError, "Invalid theme manager type")
return
@@ -157,29 +157,30 @@ func RegisterRoutes(r *gin.Engine) {
r.GET("/", controllers.Home)
r.GET("/post/:id", controllers.ShowPost)
admin := r.Group("/admin")
admin.Use(CheckAdminAuth())
adminGroup := r.Group("/admin")
adminGroup.Use(CheckAdminAuth())
{
// 无需认证的公开路由(登录/注册)
// 新增:处理 /admin 根路径跳转
admin.GET("/", func(c *gin.Context) {
adminGroup.GET("/", func(c *gin.Context) {
// 由于 CheckAdminAuth 中间件已处理未登录场景,此处用户必定已登录
c.Redirect(http.StatusFound, "/admin/index")
})
admin.GET("/login", controllers.ShowLoginPage)
admin.GET("/register", controllers.ShowRegisterPage)
admin.POST("/login", controllers.UsersLoginHandler)
admin.POST("/register", controllers.UsersRegisterHandler)
adminGroup.GET("/login", admin.ShowLoginPage)
adminGroup.GET("/register", admin.ShowRegisterPage)
adminGroup.POST("/login", admin.UsersLoginHandler)
adminGroup.POST("/register", admin.UsersRegisterHandler)
// 需要认证的路由组
authAdmin := admin.Group("", SessionAuthRequired())
authAdmin := adminGroup.Group("", SessionAuthRequired())
{
authAdmin.GET("/index", controllers.ShowAdminIndexPage)
authAdmin.GET("/themes", controllers.ListThemes)
authAdmin.POST("/themes/switch", controllers.SwitchTheme)
authAdmin.POST("/setpwd", controllers.UsersSetPwdHandler)
admin.GET("/logout", controllers.UsersLogoutHandler) // 添加退出路由
authAdmin.GET("/index", admin.ShowAdminIndexPage)
authAdmin.GET("/themes", admin.ListThemes)
authAdmin.POST("/themes/switch", admin.SwitchTheme)
authAdmin.POST("/setpwd", admin.UsersSetPwdHandler)
authAdmin.GET("/logout", admin.UsersLogoutHandler) // 添加退出路由
}
}