GoTest/internal/handler/router.go
2025-11-29 03:27:19 +08:00

134 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package handler
import (
"yinli-api/internal/middleware"
"yinli-api/internal/repository"
"yinli-api/internal/service"
"net/http/pprof"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// SetupRoutes 设置路由
func SetupRoutes(r *gin.Engine) {
// 初始化仓库
userRepo := repository.NewUserRepository()
// 初始化服务
userService := service.NewUserService(userRepo)
// 初始化处理器
userHandler := NewUserHandler(userService)
// 添加中间件
r.Use(middleware.CORSMiddleware())
r.Use(middleware.LoggerMiddleware())
r.Use(middleware.RequestIDMiddleware())
r.Use(middleware.RateLimitMiddleware())
r.Use(gin.Recovery())
// API 文档路由
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
// 健康检查
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
"message": "服务运行正常",
})
})
// pprof 性能分析路由 (仅在调试模式下启用)
if gin.Mode() == gin.DebugMode {
pprofGroup := r.Group("/debug/pprof")
{
pprofGroup.GET("/", gin.WrapF(pprof.Index))
pprofGroup.GET("/cmdline", gin.WrapF(pprof.Cmdline))
pprofGroup.GET("/profile", gin.WrapF(pprof.Profile))
pprofGroup.POST("/symbol", gin.WrapF(pprof.Symbol))
pprofGroup.GET("/symbol", gin.WrapF(pprof.Symbol))
pprofGroup.GET("/trace", gin.WrapF(pprof.Trace))
pprofGroup.GET("/allocs", gin.WrapH(pprof.Handler("allocs")))
pprofGroup.GET("/block", gin.WrapH(pprof.Handler("block")))
pprofGroup.GET("/goroutine", gin.WrapH(pprof.Handler("goroutine")))
pprofGroup.GET("/heap", gin.WrapH(pprof.Handler("heap")))
pprofGroup.GET("/mutex", gin.WrapH(pprof.Handler("mutex")))
pprofGroup.GET("/threadcreate", gin.WrapH(pprof.Handler("threadcreate")))
}
}
// API 路由组
api := r.Group("/api")
{
// 认证路由不需要JWT
auth := api.Group("/auth")
{
auth.POST("/register", userHandler.Register)
auth.POST("/login", userHandler.Login)
}
// 用户路由需要JWT认证
user := api.Group("/user")
user.Use(middleware.AuthMiddleware())
{
user.GET("/profile", userHandler.GetProfile)
user.PUT("/profile", userHandler.UpdateProfile)
user.PUT("/password", userHandler.ChangePassword)
}
// 管理员路由需要JWT认证和管理员权限
admin := api.Group("/admin")
admin.Use(middleware.AuthMiddleware())
admin.Use(middleware.AdminMiddleware())
{
admin.GET("/users", userHandler.GetUserList)
admin.DELETE("/users/:id", userHandler.DeleteUser)
admin.PUT("/users/:id/status", userHandler.UpdateUserStatus)
}
}
// 404 处理
r.NoRoute(func(c *gin.Context) {
c.JSON(404, gin.H{
"code": 404,
"message": "接口不存在",
})
})
}
// SetupTestRoutes 设置测试路由(用于测试环境)
func SetupTestRoutes(r *gin.Engine) {
test := r.Group("/test")
{
// 测试Redis连接
test.GET("/redis", func(c *gin.Context) {
// 这里可以添加Redis连接测试逻辑
c.JSON(200, gin.H{
"status": "ok",
"message": "Redis连接正常",
})
})
// 测试数据库连接
test.GET("/database", func(c *gin.Context) {
// 这里可以添加数据库连接测试逻辑
c.JSON(200, gin.H{
"status": "ok",
"message": "数据库连接正常",
})
})
// 测试JWT生成
test.GET("/jwt", func(c *gin.Context) {
// 这里可以添加JWT生成测试逻辑
c.JSON(200, gin.H{
"status": "ok",
"message": "JWT功能正常",
})
})
}
}