首次提交:初始化项目代码

This commit is contained in:
sunct
2026-07-31 15:24:14 +08:00
commit 8d3c97bd01
73 changed files with 11720 additions and 0 deletions
@@ -0,0 +1,47 @@
package routes
import (
"cloudnest/internal/interface/http/handler"
"cloudnest/internal/middleware"
"github.com/gin-gonic/gin"
)
// RegisterAuthRoutes registers authentication-related routes.
// RegisterAuthRoutes 注册认证相关的路由。
//
// Parameters:
// - r: The router group to register routes on.
// - h: The authentication handler.
// - emailHandler: The email verification handler.
//
// Routes:
// - POST /register: Register a new user.
// - POST /login: Authenticate and get JWT token.
// - POST /email-code: Send email verification code.
// - GET /profile: Get user profile (requires JWT).
// - PUT /profile: Update user profile (requires JWT).
// - PUT /password: Change password (requires JWT).
//
// 参数:
// - r: 要注册路由的路由器组。
// - h: 认证处理器。
// - emailHandler: 邮箱验证码处理器。
//
// 路由:
// - POST /register: 注册新用户。
// - POST /login: 认证并获取 JWT 令牌。
// - POST /email-code: 发送邮箱验证码。
// - GET /profile: 获取用户个人信息(需要 JWT)。
// - PUT /profile: 更新用户个人信息(需要 JWT)。
// - PUT /password: 修改密码(需要 JWT)。
func RegisterAuthRoutes(r *gin.RouterGroup, h *handler.AuthHandler, emailHandler *handler.EmailHandler, jwtSecret string) {
r.POST("/register", h.Register)
r.POST("/login", h.Login)
r.POST("/email-code", emailHandler.SendEmailCode)
protected := r.Group("")
protected.Use(middleware.JWT(jwtSecret))
protected.GET("/profile", h.Profile)
protected.PUT("/profile", h.UpdateProfile)
protected.PUT("/password", h.ChangePassword)
}
@@ -0,0 +1,17 @@
package routes
import (
"cloudnest/internal/interface/http/handler"
"github.com/gin-gonic/gin"
)
// RegisterCaptchaRoutes registers captcha-related routes.
// RegisterCaptchaRoutes 注册验证码相关的路由。
//
// Routes:
// - GET /captcha: Generate a new captcha (returns captcha_id and image_url).
// - GET /captcha/:id/image: Get captcha image by ID.
func RegisterCaptchaRoutes(r *gin.RouterGroup, h *handler.CaptchaHandler) {
r.GET("/captcha", h.GenerateCaptcha)
r.GET("/captcha/:id/image", h.GetCaptchaImage)
}
@@ -0,0 +1,53 @@
package routes
import (
"cloudnest/internal/interface/http/handler"
"github.com/gin-gonic/gin"
)
// RegisterFileRoutes registers file management routes.
// RegisterFileRoutes 注册文件管理路由。
//
// Parameters:
// - r: The router group to register routes on.
// - h: The file handler.
//
// Routes:
// - POST /upload: Upload a file.
// - GET /: List all files for the authenticated user.
// - GET /download/:name: Get a pre-signed download URL.
// - DELETE /:name: Delete a file.
//
// Note:
// - These routes require JWT authentication.
//
// 参数:
// - r: 要注册路由的路由器组。
// - h: 文件处理器。
//
// 路由:
// - POST /upload: 上传文件。
// - GET /: 获取认证用户的所有文件列表。
// - GET /download/:name: 获取预签名下载 URL。
// - DELETE /:name: 删除文件。
//
// 注意:
// - 这些路由需要 JWT 认证。
func RegisterFileRoutes(r *gin.RouterGroup, h *handler.FileHandler) {
r.POST("/upload", h.Upload)
r.GET("/upload/check", h.CheckUpload)
r.POST("/upload/chunk", h.UploadChunk)
r.POST("/upload/complete", h.CompleteUpload)
r.GET("/", h.List)
r.GET("/download/:name", h.Download)
r.GET("/content/:name", h.Content)
r.DELETE("/:name", h.Delete)
r.POST("/soft-delete/:name", h.SoftDelete)
r.POST("/favorite/:name", h.Favorite)
r.DELETE("/favorite/:name", h.Unfavorite)
r.GET("/favorites", h.ListFavorites)
r.GET("/recycle-bin", h.RecycleBin)
r.POST("/restore/:name", h.Restore)
r.DELETE("/recycle-bin/:name", h.DeleteFromRecycleBin)
r.GET("/preview/:name", h.Preview)
}
+102
View File
@@ -0,0 +1,102 @@
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)
}