133 lines
4.1 KiB
Go
133 lines
4.1 KiB
Go
package routers
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"goblog/handlers"
|
|
"goblog/middleware"
|
|
"goblog/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// SetupRoutes 配置所有路由
|
|
func SetupRoutes(r *gin.Engine, cfg interface{}) {
|
|
// 静态文件
|
|
r.Static("/static", "./static")
|
|
|
|
// 后台管理(放在前面避免冲突)
|
|
r.Static("/admin/static", "./static/admin")
|
|
r.GET("/admin", func(c *gin.Context) {
|
|
c.File("./static/admin/index.html")
|
|
})
|
|
|
|
// 前端页面路由
|
|
r.GET("/", handlers.IndexView)
|
|
r.GET("/post/:slug", handlers.PostView)
|
|
r.GET("/page/:slug", handlers.PageView)
|
|
r.GET("/search", handlers.SearchView)
|
|
r.POST("/comment", handlers.SubmitComment)
|
|
|
|
// 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/register", handlers.Register)
|
|
|
|
// 需要登录的 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)
|
|
}
|
|
|
|
// 主题管理(可选认证)
|
|
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())
|
|
{
|
|
// 文章管理
|
|
admin.POST("/posts", handlers.CreatePost)
|
|
admin.PUT("/posts/:id", handlers.UpdatePost)
|
|
admin.DELETE("/posts/:id", handlers.DeletePost)
|
|
|
|
// Markdown 预览(文章编辑器用)
|
|
admin.POST("/markdown/preview", handlers.PreviewMarkdown)
|
|
|
|
// 分类管理
|
|
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)
|
|
}
|