Files
hr_data_analyzer/routes/routes.go
2025-06-25 09:36:27 +08:00

52 lines
1.4 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 routes
import (
"github.com/gin-gonic/gin"
"hr_receiver/controllers"
"hr_receiver/middleware"
"net/http"
)
func SetupRouter() *gin.Engine {
jwtService := middleware.NewJWTService(middleware.ApiSecret, middleware.TokenExp)
r := gin.Default()
r.Use(middleware.GzipMiddleware())
trainingController := controllers.NewTrainingController()
stepTrainController := controllers.NewStepTrainingController()
v1 := r.Group("/api/v1")
{
records := v1.Group("/train-records") //.Use(middleware.AuthMiddleware())
{
records.POST("", trainingController.CreateTrainingRecord)
records.GET("/analysis", trainingController.HandleCurveAnalysis)
// 可扩展其他路由GET, PUT, DELETE等
}
steps := v1.Group("/step") //.Use(middleware.AuthMiddleware())
{
steps.POST("", stepTrainController.CreateTrainingRecord)
// 可扩展其他路由GET, PUT, DELETE等
}
auth := v1.Group("/auth")
{
auth.GET("/token", func(c *gin.Context) {
clientSecret := c.GetHeader("X-API-Key")
if clientSecret != middleware.ApiSecret {
c.JSON(http.StatusUnauthorized, gin.H{"error": "invalid secret"})
return
}
token, err := jwtService.GenerateToken()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to generate token"})
return
}
c.JSON(http.StatusOK, gin.H{"token": token})
})
}
}
return r
}