handlers区分backend和frontend,需要确认。

This commit is contained in:
2026-08-12 17:57:31 +08:00
parent 60869ec214
commit 42c6f2450d
20 changed files with 242 additions and 137 deletions
+67 -63
View File
@@ -3,7 +3,8 @@ package routers
import (
"strconv"
"goblog/handlers"
"goblog/handlers/backend"
"goblog/handlers/frontend"
"goblog/middleware"
"goblog/utils"
@@ -18,74 +19,77 @@ func SetupRoutes(r *gin.Engine) {
// 后台管理(放在前面避免冲突)
r.Static("/admin/static", "./static/admin")
// 独立登录页
r.GET("/admin/login", handlers.AdminLoginView)
r.GET("/admin/login", backend.AdminLoginView)
// 后台首页:可选认证中间件注入用户信息,服务端按角色渲染菜单与页面框架
r.GET("/admin", middleware.OptionalAuth(), handlers.AdminView)
r.GET("/admin", middleware.OptionalAuth(), backend.AdminView)
// 前端页面路由(可选认证:侧边栏根据登录态显示系统菜单)
r.GET("/", middleware.OptionalAuth(), handlers.IndexView)
r.GET("/post/:slug", middleware.OptionalAuth(), handlers.PostView)
r.GET("/page/:slug", middleware.OptionalAuth(), handlers.PageView)
r.GET("/search", middleware.OptionalAuth(), handlers.SearchView)
r.POST("/comment", handlers.SubmitComment)
r.GET("/", middleware.OptionalAuth(), frontend.IndexView)
r.GET("/post/:slug", middleware.OptionalAuth(), frontend.PostView)
r.GET("/page/:slug", middleware.OptionalAuth(), frontend.PageView)
r.GET("/search", middleware.OptionalAuth(), frontend.SearchView)
r.POST("/comment", frontend.SubmitComment)
// 前台退出登录:清除 Cookie 后回首页
r.GET("/logout", handlers.WebLogout)
r.GET("/logout", backend.WebLogout)
// API 路由组
api := r.Group("/api")
{
// 公开 API(支持可选认证,以便管理员查看所有文章)
api.GET("/posts", middleware.OptionalAuth(), handlers.GetPosts)
api.GET("/posts/:id", middleware.OptionalAuth(), handlers.GetPost)
api.GET("/categories", handlers.GetCategories)
api.GET("/categories/:id", handlers.GetCategory)
api.GET("/tags", handlers.GetTags)
api.GET("/tags/:id", handlers.GetTag)
api.GET("/pages", handlers.GetPages)
api.GET("/pages/:id", handlers.GetPage)
api.GET("/comments", handlers.GetComments)
api.GET("/posts", middleware.OptionalAuth(), backend.GetPosts)
api.GET("/posts/:id", middleware.OptionalAuth(), backend.GetPost)
api.GET("/categories", backend.GetCategories)
api.GET("/categories/:id", backend.GetCategory)
api.GET("/tags", backend.GetTags)
api.GET("/tags/:id", backend.GetTag)
api.GET("/pages", backend.GetPages)
api.GET("/pages/:id", backend.GetPage)
api.GET("/comments", backend.GetComments)
// 需要认证的 API
api.POST("/comments", middleware.OptionalAuth(), handlers.CreateComment)
api.POST("/comments", middleware.OptionalAuth(), backend.CreateComment)
// 用户认证
api.POST("/auth/login", handlers.Login)
api.POST("/auth/logout", handlers.Logout)
api.POST("/auth/login", backend.Login)
api.POST("/auth/logout", backend.Logout)
// 需要登录的 API
auth := api.Group("/")
auth.Use(middleware.JWTAuth())
{
auth.GET("/auth/me", handlers.GetCurrentUser)
auth.PUT("/auth/me", handlers.UpdateUser)
auth.PUT("/auth/password", handlers.ChangePassword)
auth.GET("/auth/me", backend.GetCurrentUser)
// 仪表盘统计(所有登录用户可访问,handler 内部按角色过滤数据)
auth.GET("/dashboard", middleware.OptionalAuth(), backend.GetDashboardStats)
auth.PUT("/auth/me", backend.UpdateUser)
auth.PUT("/auth/password", backend.ChangePassword)
// 文章管理(所有登录用户可新建文章,修改/删除自己的文章)
auth.POST("/posts", handlers.CreatePost)
auth.PUT("/posts/:id", handlers.UpdatePost)
auth.DELETE("/posts/:id", handlers.DeletePost)
auth.POST("/posts", backend.CreatePost)
auth.PUT("/posts/:id", backend.UpdatePost)
auth.DELETE("/posts/:id", backend.DeletePost)
// Markdown 预览(文章编辑器用)
auth.POST("/markdown/preview", handlers.PreviewMarkdown)
auth.POST("/markdown/preview", backend.PreviewMarkdown)
// 文件上传(文章编辑器封面图等)
auth.POST("/upload", handlers.UploadImage)
auth.POST("/upload", backend.UploadImage)
// 评论管理(登录用户管理自己文章的评论)
auth.GET("/my/comments", handlers.GetComments)
auth.PUT("/my/comments/:id/approve", handlers.ApproveComment)
auth.PUT("/my/comments/:id/spam", handlers.MarkSpamComment)
auth.DELETE("/my/comments/:id", handlers.DeleteComment)
auth.GET("/my/comments", backend.GetComments)
auth.PUT("/my/comments/:id/approve", backend.ApproveComment)
auth.PUT("/my/comments/:id/spam", backend.MarkSpamComment)
auth.DELETE("/my/comments/:id", backend.DeleteComment)
}
// 主题管理(可选认证)
api.GET("/themes", handlers.GetThemes)
api.GET("/theme", handlers.GetCurrentTheme)
api.POST("/theme", middleware.JWTAuth(), handlers.SwitchTheme)
api.POST("/themes/create", middleware.JWTAuth(), handlers.CreateTheme)
api.GET("/themes/:theme/files", middleware.JWTAuth(), handlers.GetThemeFiles)
api.GET("/themes/:theme/files/:file", middleware.JWTAuth(), handlers.GetThemeFile)
api.PUT("/themes/:theme/files/:file", middleware.JWTAuth(), handlers.SaveThemeFile)
api.GET("/themes", backend.GetThemes)
api.GET("/theme", backend.GetCurrentTheme)
api.POST("/theme", middleware.JWTAuth(), backend.SwitchTheme)
api.POST("/themes/create", middleware.JWTAuth(), backend.CreateTheme)
api.GET("/themes/:theme/files", middleware.JWTAuth(), backend.GetThemeFiles)
api.GET("/themes/:theme/files/:file", middleware.JWTAuth(), backend.GetThemeFile)
api.PUT("/themes/:theme/files/:file", middleware.JWTAuth(), backend.SaveThemeFile)
// 管理员 API
admin := api.Group("/admin")
@@ -94,41 +98,41 @@ func SetupRoutes(r *gin.Engine) {
// Markdown 预览(文章编辑器用)已移至登录组
// 分类管理
admin.POST("/categories", handlers.CreateCategory)
admin.PUT("/categories/:id", handlers.UpdateCategory)
admin.DELETE("/categories/:id", handlers.DeleteCategory)
admin.POST("/categories", backend.CreateCategory)
admin.PUT("/categories/:id", backend.UpdateCategory)
admin.DELETE("/categories/:id", backend.DeleteCategory)
// 标签管理
admin.POST("/tags", handlers.CreateTag)
admin.PUT("/tags/:id", handlers.UpdateTag)
admin.DELETE("/tags/:id", handlers.DeleteTag)
admin.POST("/tags", backend.CreateTag)
admin.PUT("/tags/:id", backend.UpdateTag)
admin.DELETE("/tags/:id", backend.DeleteTag)
// 页面管理
admin.POST("/pages", handlers.CreatePage)
admin.PUT("/pages/:id", handlers.UpdatePage)
admin.DELETE("/pages/:id", handlers.DeletePage)
admin.POST("/pages", backend.CreatePage)
admin.PUT("/pages/:id", backend.UpdatePage)
admin.DELETE("/pages/:id", backend.DeletePage)
// 评论管理
admin.GET("/comments", handlers.GetComments)
admin.PUT("/comments/:id/approve", handlers.ApproveComment)
admin.PUT("/comments/:id/spam", handlers.MarkSpamComment)
admin.DELETE("/comments/:id", handlers.DeleteComment)
admin.GET("/comments", backend.GetComments)
admin.PUT("/comments/:id/approve", backend.ApproveComment)
admin.PUT("/comments/:id/spam", backend.MarkSpamComment)
admin.DELETE("/comments/:id", backend.DeleteComment)
// 用户管理
admin.GET("/users", handlers.GetUsers)
admin.POST("/users", handlers.CreateUser)
admin.PUT("/users/:id", handlers.AdminUpdateUser)
admin.PUT("/users/:id/password", handlers.AdminResetPassword)
admin.DELETE("/users/:id", handlers.DeleteUser)
admin.GET("/users", backend.GetUsers)
admin.POST("/users", backend.CreateUser)
admin.PUT("/users/:id", backend.AdminUpdateUser)
admin.PUT("/users/:id/password", backend.AdminResetPassword)
admin.DELETE("/users/:id", backend.DeleteUser)
// 基础设置
admin.GET("/settings", handlers.GetSettings)
admin.PUT("/settings", handlers.UpdateSettings)
admin.GET("/settings", backend.GetSettings)
admin.PUT("/settings", backend.UpdateSettings)
// 文件管理(图片上传)
admin.POST("/upload", handlers.UploadImage)
admin.GET("/uploads", handlers.ListUploads)
admin.DELETE("/uploads/:name", handlers.DeleteUpload)
admin.POST("/upload", backend.UploadImage)
admin.GET("/uploads", backend.ListUploads)
admin.DELETE("/uploads/:name", backend.DeleteUpload)
}
}
}