package routers import ( "strconv" "goblog/handlers" "goblog/middleware" "goblog/utils" "github.com/gin-gonic/gin" ) // SetupRoutes 配置所有路由 func SetupRoutes(r *gin.Engine) { // 静态文件 r.Static("/static", "./static") // 后台管理(放在前面避免冲突) r.Static("/admin/static", "./static/admin") // 独立登录页 r.GET("/admin/login", handlers.AdminLoginView) // 后台首页:可选认证中间件注入用户信息,服务端按角色渲染菜单与页面框架 r.GET("/admin", middleware.OptionalAuth(), handlers.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) // 前台退出登录:清除 Cookie 后回首页 r.GET("/logout", handlers.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 api.POST("/comments", middleware.OptionalAuth(), handlers.CreateComment) // 用户认证 api.POST("/auth/login", handlers.Login) api.POST("/auth/logout", handlers.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.POST("/posts", handlers.CreatePost) auth.PUT("/posts/:id", handlers.UpdatePost) auth.DELETE("/posts/:id", handlers.DeletePost) // Markdown 预览(文章编辑器用) auth.POST("/markdown/preview", handlers.PreviewMarkdown) // 文件上传(文章编辑器封面图等) auth.POST("/upload", handlers.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) } // 主题管理(可选认证) 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 admin := api.Group("/admin") admin.Use(middleware.JWTAuth(), middleware.AdminRequired()) { // Markdown 预览(文章编辑器用)已移至登录组 // 分类管理 admin.POST("/categories", handlers.CreateCategory) admin.PUT("/categories/:id", handlers.UpdateCategory) admin.DELETE("/categories/:id", handlers.DeleteCategory) // 标签管理 admin.POST("/tags", handlers.CreateTag) admin.PUT("/tags/:id", handlers.UpdateTag) admin.DELETE("/tags/:id", handlers.DeleteTag) // 页面管理 admin.POST("/pages", handlers.CreatePage) admin.PUT("/pages/:id", handlers.UpdatePage) admin.DELETE("/pages/:id", handlers.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("/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("/settings", handlers.GetSettings) admin.PUT("/settings", handlers.UpdateSettings) // 文件管理(图片上传) admin.POST("/upload", handlers.UploadImage) admin.GET("/uploads", handlers.ListUploads) admin.DELETE("/uploads/:name", handlers.DeleteUpload) } } } // SetupTemplates 配置模板渲染器 func SetupTemplates(r *gin.Engine, theme string) { htmlRenderer := utils.LoadTemplates(theme) utils.SetGlobalRenderer(htmlRenderer) r.HTMLRender = htmlRenderer } // GetServerPort 获取服务器端口 func GetServerPort(port int) string { return ":" + strconv.Itoa(port) }