Initial commit: Basic project structure and dependencies
This commit is contained in:
133
internal/domains/user/routes/user_routes.go
Normal file
133
internal/domains/user/routes/user_routes.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"tyapi-server/internal/domains/user/handlers"
|
||||
"tyapi-server/internal/shared/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// UserRoutes 用户路由注册器
|
||||
type UserRoutes struct {
|
||||
handler *handlers.UserHandler
|
||||
jwtAuth *middleware.JWTAuthMiddleware
|
||||
optionalAuth *middleware.OptionalAuthMiddleware
|
||||
}
|
||||
|
||||
// NewUserRoutes 创建用户路由注册器
|
||||
func NewUserRoutes(
|
||||
handler *handlers.UserHandler,
|
||||
jwtAuth *middleware.JWTAuthMiddleware,
|
||||
optionalAuth *middleware.OptionalAuthMiddleware,
|
||||
) *UserRoutes {
|
||||
return &UserRoutes{
|
||||
handler: handler,
|
||||
jwtAuth: jwtAuth,
|
||||
optionalAuth: optionalAuth,
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterRoutes 注册用户路由
|
||||
func (r *UserRoutes) RegisterRoutes(router *gin.Engine) {
|
||||
// API版本组
|
||||
v1 := router.Group("/api/v1")
|
||||
|
||||
// 公开路由(不需要认证)
|
||||
public := v1.Group("/auth")
|
||||
{
|
||||
public.POST("/login", r.handler.Login)
|
||||
public.POST("/register", r.handler.Create)
|
||||
}
|
||||
|
||||
// 需要认证的路由
|
||||
protected := v1.Group("/users")
|
||||
protected.Use(r.jwtAuth.Handle())
|
||||
{
|
||||
// 用户管理(管理员)
|
||||
protected.GET("", r.handler.List)
|
||||
protected.POST("", r.handler.Create)
|
||||
protected.GET("/:id", r.handler.GetByID)
|
||||
protected.PUT("/:id", r.handler.Update)
|
||||
protected.DELETE("/:id", r.handler.Delete)
|
||||
|
||||
// 用户搜索
|
||||
protected.GET("/search", r.handler.Search)
|
||||
|
||||
// 用户统计
|
||||
protected.GET("/stats", r.handler.GetStats)
|
||||
}
|
||||
|
||||
// 用户个人操作路由
|
||||
profile := v1.Group("/profile")
|
||||
profile.Use(r.jwtAuth.Handle())
|
||||
{
|
||||
profile.GET("", r.handler.GetProfile)
|
||||
profile.PUT("", r.handler.UpdateProfile)
|
||||
profile.POST("/change-password", r.handler.ChangePassword)
|
||||
profile.POST("/logout", r.handler.Logout)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterPublicRoutes 注册公开路由
|
||||
func (r *UserRoutes) RegisterPublicRoutes(router *gin.Engine) {
|
||||
v1 := router.Group("/api/v1")
|
||||
|
||||
// 公开的用户相关路由
|
||||
public := v1.Group("/public")
|
||||
{
|
||||
// 可选认证的路由(用户可能登录也可能未登录)
|
||||
public.Use(r.optionalAuth.Handle())
|
||||
|
||||
// 这里可以添加一些公开的用户信息查询接口
|
||||
// 比如根据用户名查看公开信息(如果用户设置为公开)
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterAdminRoutes 注册管理员路由
|
||||
func (r *UserRoutes) RegisterAdminRoutes(router *gin.Engine) {
|
||||
admin := router.Group("/admin/v1")
|
||||
admin.Use(r.jwtAuth.Handle())
|
||||
// 这里可以添加管理员权限检查中间件
|
||||
|
||||
// 管理员用户管理
|
||||
users := admin.Group("/users")
|
||||
{
|
||||
users.GET("", r.handler.List)
|
||||
users.GET("/:id", r.handler.GetByID)
|
||||
users.PUT("/:id", r.handler.Update)
|
||||
users.DELETE("/:id", r.handler.Delete)
|
||||
users.GET("/stats", r.handler.GetStats)
|
||||
users.GET("/search", r.handler.Search)
|
||||
|
||||
// 批量操作
|
||||
users.POST("/batch-delete", r.handleBatchDelete)
|
||||
users.POST("/batch-update", r.handleBatchUpdate)
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除处理器
|
||||
func (r *UserRoutes) handleBatchDelete(c *gin.Context) {
|
||||
// 实现批量删除逻辑
|
||||
// 这里可以接收用户ID列表并调用服务进行批量删除
|
||||
c.JSON(200, gin.H{"message": "Batch delete not implemented yet"})
|
||||
}
|
||||
|
||||
// 批量更新处理器
|
||||
func (r *UserRoutes) handleBatchUpdate(c *gin.Context) {
|
||||
// 实现批量更新逻辑
|
||||
c.JSON(200, gin.H{"message": "Batch update not implemented yet"})
|
||||
}
|
||||
|
||||
// RegisterHealthRoutes 注册健康检查路由
|
||||
func (r *UserRoutes) RegisterHealthRoutes(router *gin.Engine) {
|
||||
health := router.Group("/health")
|
||||
{
|
||||
health.GET("/users", func(c *gin.Context) {
|
||||
// 用户服务健康检查
|
||||
c.JSON(200, gin.H{
|
||||
"service": "users",
|
||||
"status": "healthy",
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user