diff --git a/config.sample.yaml b/config.sample.yaml index 293c2a5..ea849b3 100644 --- a/config.sample.yaml +++ b/config.sample.yaml @@ -20,3 +20,5 @@ mqtt: qos: 0 enable_measurement_subscriptions: false enable_training_event_subscription: true +swagger: + enabled: true diff --git a/config/config.go b/config/config.go index ff57b04..f80947a 100644 --- a/config/config.go +++ b/config/config.go @@ -41,9 +41,14 @@ type MQTTConfig struct { } type AppConfig struct { - DB DBConfig `mapstructure:"database" yaml:"database"` - AI AIConfig `mapstructure:"ai" yaml:"ai"` - MQTT MQTTConfig `mapstructure:"mqtt" yaml:"mqtt"` + DB DBConfig `mapstructure:"database" yaml:"database"` + AI AIConfig `mapstructure:"ai" yaml:"ai"` + MQTT MQTTConfig `mapstructure:"mqtt" yaml:"mqtt"` + Swagger SwaggerConfig `mapstructure:"swagger" yaml:"swagger"` +} + +type SwaggerConfig struct { + Enabled bool `mapstructure:"enabled" yaml:"enabled"` } func InitConfig() { diff --git a/routes/routes.go b/routes/routes.go index c1d5630..508064c 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -4,6 +4,7 @@ import ( "github.com/gin-gonic/gin" swaggerFiles "github.com/swaggo/files" ginSwagger "github.com/swaggo/gin-swagger" + "hr_receiver/config" "hr_receiver/controllers" _ "hr_receiver/docs" "hr_receiver/middleware" @@ -49,15 +50,15 @@ func SetupRouter() *gin.Engine { r.GET("/auth/token", deviceTokenHandler) - // Swagger API文档 (swaggo/gin-swagger,持久化鉴权信息) - r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, - ginSwagger.PersistAuthorization(true), - )) + // 开发模式下注册 API 文档路由 + if config.App.Swagger.Enabled { + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, + ginSwagger.PersistAuthorization(true), + )) - // Scalar 精美API文档 - r.GET("/scalar", func(c *gin.Context) { - c.Header("Content-Type", "text/html; charset=utf-8") - c.String(http.StatusOK, ` + r.GET("/scalar", func(c *gin.Context) { + c.Header("Content-Type", "text/html; charset=utf-8") + c.String(http.StatusOK, ` API Reference @@ -71,16 +72,14 @@ func SetupRouter() *gin.Engine {
`) - }) + }) + } v1 := r.Group("/api/v1") {