102 lines
3.9 KiB
Go
102 lines
3.9 KiB
Go
package routes
|
|
|
|
import (
|
|
"cloudnest/internal/interface/http/handler"
|
|
"cloudnest/internal/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ServiceType defines the type of service that is running.
|
|
// ServiceType 定义运行的服务类型。
|
|
type ServiceType string
|
|
|
|
const (
|
|
// ServiceTypeAuth is the authentication service.
|
|
// ServiceTypeAuth 是认证服务。
|
|
ServiceTypeAuth ServiceType = "auth"
|
|
|
|
// ServiceTypeFile is the file management service.
|
|
// ServiceTypeFile 是文件管理服务。
|
|
ServiceTypeFile ServiceType = "file"
|
|
|
|
// ServiceTypeAll is for testing purposes, registers all routes.
|
|
// ServiceTypeAll 用于测试目的,注册所有路由。
|
|
ServiceTypeAll ServiceType = "all"
|
|
)
|
|
|
|
// SetupRoutes registers API routes based on the service type.
|
|
// SetupRoutes 根据服务类型注册 API 路由。
|
|
//
|
|
// Parameters:
|
|
// - engine: The Gin engine instance.
|
|
// - serviceType: The type of service (auth, file, or all).
|
|
// - authHandler: The authentication handler (required for auth service).
|
|
// - fileHandler: The file handler (required for file service).
|
|
// - jwtSecret: The secret key for JWT token validation.
|
|
//
|
|
// Note:
|
|
// - Authentication routes are public and don't require JWT.
|
|
// - File routes require JWT authentication via middleware.JWT.
|
|
// - Each service should only register its own routes to avoid conflicts.
|
|
//
|
|
// 参数:
|
|
// - engine: Gin 引擎实例。
|
|
// - serviceType: 服务类型 (auth, file, 或 all)。
|
|
// - authHandler: 认证处理器(认证服务必需)。
|
|
// - fileHandler: 文件处理器(文件服务必需)。
|
|
// - jwtSecret: JWT 令牌验证的密钥。
|
|
//
|
|
// 注意:
|
|
// - 认证路由是公开的,不需要 JWT。
|
|
// - 文件路由需要通过 middleware.JWT 进行 JWT 认证。
|
|
// - 每个服务应该只注册自己的路由以避免冲突。
|
|
func SetupRoutes(engine *gin.Engine, serviceType ServiceType, authHandler *handler.AuthHandler, fileHandler *handler.FileHandler, captchaHandler *handler.CaptchaHandler, emailHandler *handler.EmailHandler, jwtSecret string) {
|
|
api := engine.Group("/api/v1")
|
|
|
|
RegisterCaptchaRoutes(api, captchaHandler)
|
|
|
|
switch serviceType {
|
|
case ServiceTypeAuth:
|
|
auth := api.Group("/auth")
|
|
RegisterAuthRoutes(auth, authHandler, emailHandler, jwtSecret)
|
|
case ServiceTypeFile:
|
|
files := api.Group("/files")
|
|
files.Use(middleware.JWT(jwtSecret))
|
|
RegisterFileRoutes(files, fileHandler)
|
|
case ServiceTypeAll:
|
|
auth := api.Group("/auth")
|
|
RegisterAuthRoutes(auth, authHandler, emailHandler, jwtSecret)
|
|
files := api.Group("/files")
|
|
files.Use(middleware.JWT(jwtSecret))
|
|
RegisterFileRoutes(files, fileHandler)
|
|
}
|
|
}
|
|
|
|
// SetupAuthRoutes registers only authentication routes.
|
|
// SetupAuthRoutes 只注册认证路由。
|
|
//
|
|
// This function is used by the authentication service to register
|
|
// only its own routes, avoiding conflicts with the file service.
|
|
//
|
|
// 此函数用于认证服务注册自己的路由,避免与文件服务冲突。
|
|
func SetupAuthRoutes(engine *gin.Engine, authHandler *handler.AuthHandler, captchaHandler *handler.CaptchaHandler, emailHandler *handler.EmailHandler, jwtSecret string) {
|
|
api := engine.Group("/api/v1")
|
|
RegisterCaptchaRoutes(api, captchaHandler)
|
|
auth := api.Group("/auth")
|
|
RegisterAuthRoutes(auth, authHandler, emailHandler, jwtSecret)
|
|
}
|
|
|
|
// SetupFileRoutes registers only file management routes.
|
|
// SetupFileRoutes 只注册文件管理路由。
|
|
//
|
|
// This function is used by the file service to register
|
|
// only its own routes, avoiding conflicts with the auth service.
|
|
//
|
|
// 此函数用于文件服务注册自己的路由,避免与认证服务冲突。
|
|
func SetupFileRoutes(engine *gin.Engine, fileHandler *handler.FileHandler, captchaHandler *handler.CaptchaHandler, jwtSecret string) {
|
|
api := engine.Group("/api/v1")
|
|
RegisterCaptchaRoutes(api, captchaHandler)
|
|
files := api.Group("/files")
|
|
files.Use(middleware.JWT(jwtSecret))
|
|
RegisterFileRoutes(files, fileHandler)
|
|
} |