Files
photography/backend-old/internal/api/routes/routes.go
xujiang 604b9e59ba fix
2025-07-10 18:09:11 +08:00

131 lines
4.3 KiB
Go

package routes
import (
"photography-backend/internal/api/handlers"
"photography-backend/internal/api/middleware"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type Handlers struct {
AuthHandler *handlers.AuthHandler
UserHandler *handlers.UserHandler
PhotoHandler *handlers.PhotoHandler
CategoryHandler *handlers.CategoryHandler
TagHandler *handlers.TagHandler
}
func SetupRoutes(r *gin.Engine, h *Handlers, authMiddleware *middleware.AuthMiddleware, logger *zap.Logger) {
// 健康检查
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok"})
})
// API 路由组
api := r.Group("/api")
{
// 公开路由
public := api.Group("")
{
// 认证相关
auth := public.Group("/auth")
{
auth.POST("/login", h.AuthHandler.Login)
auth.POST("/refresh", h.AuthHandler.RefreshToken)
}
}
// 需要认证的路由
protected := api.Group("")
protected.Use(authMiddleware.RequireAuth())
{
// 当前用户信息
protected.GET("/me", h.UserHandler.GetCurrentUser)
protected.PUT("/me", h.UserHandler.UpdateCurrentUser)
protected.POST("/auth/logout", h.AuthHandler.Logout)
// 照片管理
photos := protected.Group("/photos")
{
photos.GET("", h.PhotoHandler.GetPhotos)
photos.POST("", h.PhotoHandler.CreatePhoto)
photos.GET("/stats", h.PhotoHandler.GetPhotoStats)
photos.POST("/upload", h.PhotoHandler.UploadPhoto)
photos.POST("/batch/update", h.PhotoHandler.BatchUpdatePhotos)
photos.POST("/batch/delete", h.PhotoHandler.BatchDeletePhotos)
photos.GET("/:id", h.PhotoHandler.GetPhoto)
photos.PUT("/:id", h.PhotoHandler.UpdatePhoto)
photos.DELETE("/:id", h.PhotoHandler.DeletePhoto)
}
// 分类管理
categories := protected.Group("/categories")
{
categories.GET("", h.CategoryHandler.GetCategories)
categories.POST("", h.CategoryHandler.CreateCategory)
categories.GET("/tree", h.CategoryHandler.GetCategoryTree)
categories.GET("/stats", h.CategoryHandler.GetCategoryStats)
categories.POST("/reorder", h.CategoryHandler.ReorderCategories)
categories.POST("/generate-slug", h.CategoryHandler.GenerateSlug)
categories.GET("/:id", h.CategoryHandler.GetCategory)
categories.PUT("/:id", h.CategoryHandler.UpdateCategory)
categories.DELETE("/:id", h.CategoryHandler.DeleteCategory)
categories.GET("/slug/:slug", h.CategoryHandler.GetCategoryBySlug)
}
// 标签管理
tags := protected.Group("/tags")
{
tags.GET("", h.TagHandler.GetTags)
tags.POST("", h.TagHandler.CreateTag)
tags.GET("/all", h.TagHandler.GetAllTags)
tags.GET("/popular", h.TagHandler.GetPopularTags)
tags.GET("/cloud", h.TagHandler.GetTagCloud)
tags.GET("/stats", h.TagHandler.GetTagStats)
tags.GET("/search", h.TagHandler.SearchTags)
tags.POST("/batch/delete", h.TagHandler.BatchDeleteTags)
tags.POST("/generate-slug", h.TagHandler.GenerateSlug)
tags.GET("/:id", h.TagHandler.GetTag)
tags.PUT("/:id", h.TagHandler.UpdateTag)
tags.DELETE("/:id", h.TagHandler.DeleteTag)
tags.GET("/slug/:slug", h.TagHandler.GetTagBySlug)
}
// 用户管理 (需要管理员权限)
admin := protected.Group("/admin")
admin.Use(authMiddleware.RequireRole("admin"))
{
users := admin.Group("/users")
{
users.GET("", h.UserHandler.GetUsers)
users.POST("", h.UserHandler.CreateUser)
users.GET("/:id", h.UserHandler.GetUser)
users.PUT("/:id", h.UserHandler.UpdateUser)
users.DELETE("/:id", h.UserHandler.DeleteUser)
}
}
}
}
// 前端公共 API (无需认证)
frontend := api.Group("/public")
{
// 公开的照片接口
frontend.GET("/photos", h.PhotoHandler.GetPhotos)
frontend.GET("/photos/:id", h.PhotoHandler.GetPhoto)
// 公开的分类接口
frontend.GET("/categories", h.CategoryHandler.GetCategories)
frontend.GET("/categories/tree", h.CategoryHandler.GetCategoryTree)
frontend.GET("/categories/:id", h.CategoryHandler.GetCategory)
frontend.GET("/categories/slug/:slug", h.CategoryHandler.GetCategoryBySlug)
// 公开的标签接口
frontend.GET("/tags", h.TagHandler.GetTags)
frontend.GET("/tags/popular", h.TagHandler.GetPopularTags)
frontend.GET("/tags/cloud", h.TagHandler.GetTagCloud)
frontend.GET("/tags/:id", h.TagHandler.GetTag)
frontend.GET("/tags/slug/:slug", h.TagHandler.GetTagBySlug)
}
}