102 lines
3.1 KiB
Go
102 lines
3.1 KiB
Go
package router
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
"healthflow/internal/config"
|
|
"healthflow/internal/handler"
|
|
"healthflow/internal/middleware"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func Setup(db *sql.DB, cfg *config.Config) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
|
|
// CORS
|
|
r.Use(func(c *gin.Context) {
|
|
c.Header("Access-Control-Allow-Origin", "*")
|
|
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
|
|
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
|
if c.Request.Method == "OPTIONS" {
|
|
c.AbortWithStatus(204)
|
|
return
|
|
}
|
|
c.Next()
|
|
})
|
|
|
|
// 静态文件服务 (本地上传的文件)
|
|
if cfg.LocalUploadPath != "" {
|
|
r.Static("/uploads", cfg.LocalUploadPath)
|
|
}
|
|
|
|
// Handlers
|
|
authHandler := handler.NewAuthHandler(db, cfg)
|
|
userHandler := handler.NewUserHandler(db, cfg)
|
|
uploadHandler := handler.NewUploadHandler(cfg)
|
|
versionHandler := handler.NewVersionHandler(db, cfg)
|
|
weightHandler := handler.NewWeightHandler(db, cfg)
|
|
epochHandler := handler.NewEpochHandler(db, cfg)
|
|
exerciseHandler := handler.NewExerciseHandler(db, cfg)
|
|
|
|
// R2 文件代理 (公开访问)
|
|
r.GET("/files/*filepath", uploadHandler.GetFile)
|
|
|
|
// 公开接口
|
|
r.POST("/api/auth/register", authHandler.Register)
|
|
r.POST("/api/auth/login", authHandler.Login)
|
|
r.GET("/api/version", versionHandler.GetLatestVersion)
|
|
r.POST("/api/version", versionHandler.SetVersion)
|
|
|
|
// 需要认证的接口
|
|
auth := r.Group("/api")
|
|
auth.Use(middleware.AuthMiddleware(cfg.JWTSecret))
|
|
{
|
|
// 用户
|
|
auth.GET("/user/profile", userHandler.GetProfile)
|
|
auth.PUT("/user/profile", userHandler.UpdateProfile)
|
|
auth.PUT("/user/avatar", userHandler.UpdateAvatar)
|
|
|
|
// 体重
|
|
auth.GET("/weight/goal", weightHandler.GetGoal)
|
|
auth.POST("/weight/goal", weightHandler.SetGoal)
|
|
auth.POST("/weight/record", weightHandler.RecordWeight)
|
|
auth.GET("/weight/week", weightHandler.GetWeekData)
|
|
auth.GET("/weight/stats", weightHandler.GetStats)
|
|
auth.GET("/weight/history", weightHandler.GetHistory)
|
|
|
|
// 纪元
|
|
auth.POST("/epoch", epochHandler.CreateEpoch)
|
|
auth.GET("/epoch/active", epochHandler.GetActiveEpoch)
|
|
auth.GET("/epoch/list", epochHandler.GetEpochList)
|
|
auth.GET("/epoch/:id", epochHandler.GetEpochDetail)
|
|
auth.PUT("/epoch/:id", epochHandler.UpdateEpoch)
|
|
auth.DELETE("/epoch/:id", epochHandler.DeleteEpoch)
|
|
auth.GET("/epoch/:id/plans", epochHandler.GetWeeklyPlans)
|
|
auth.PUT("/epoch/:id/plan/:planId", epochHandler.UpdateWeeklyPlan)
|
|
|
|
// 运动打卡
|
|
auth.POST("/exercise/checkin", exerciseHandler.CreateCheckin)
|
|
auth.GET("/exercise/checkins", exerciseHandler.GetCheckins)
|
|
auth.GET("/exercise/heatmap", exerciseHandler.GetHeatmap)
|
|
auth.GET("/exercise/stats", exerciseHandler.GetStats)
|
|
auth.DELETE("/exercise/checkin/:id", exerciseHandler.DeleteCheckin)
|
|
|
|
// 上传
|
|
auth.POST("/upload", uploadHandler.Upload)
|
|
|
|
// 管理员接口
|
|
admin := auth.Group("/admin")
|
|
admin.Use(middleware.AdminMiddleware())
|
|
{
|
|
admin.GET("/settings", userHandler.GetSettings)
|
|
admin.PUT("/settings", userHandler.UpdateSettings)
|
|
admin.POST("/version", versionHandler.SetVersion)
|
|
}
|
|
}
|
|
|
|
return r
|
|
}
|