feat: swagger switch.

This commit is contained in:
2026-05-04 18:36:09 +08:00
parent 062ed63bb5
commit 30c8094f0f
3 changed files with 24 additions and 18 deletions
+2
View File
@@ -20,3 +20,5 @@ mqtt:
qos: 0 qos: 0
enable_measurement_subscriptions: false enable_measurement_subscriptions: false
enable_training_event_subscription: true enable_training_event_subscription: true
swagger:
enabled: true
+8 -3
View File
@@ -41,9 +41,14 @@ type MQTTConfig struct {
} }
type AppConfig struct { type AppConfig struct {
DB DBConfig `mapstructure:"database" yaml:"database"` DB DBConfig `mapstructure:"database" yaml:"database"`
AI AIConfig `mapstructure:"ai" yaml:"ai"` AI AIConfig `mapstructure:"ai" yaml:"ai"`
MQTT MQTTConfig `mapstructure:"mqtt" yaml:"mqtt"` MQTT MQTTConfig `mapstructure:"mqtt" yaml:"mqtt"`
Swagger SwaggerConfig `mapstructure:"swagger" yaml:"swagger"`
}
type SwaggerConfig struct {
Enabled bool `mapstructure:"enabled" yaml:"enabled"`
} }
func InitConfig() { func InitConfig() {
+14 -15
View File
@@ -4,6 +4,7 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files" swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger" ginSwagger "github.com/swaggo/gin-swagger"
"hr_receiver/config"
"hr_receiver/controllers" "hr_receiver/controllers"
_ "hr_receiver/docs" _ "hr_receiver/docs"
"hr_receiver/middleware" "hr_receiver/middleware"
@@ -49,15 +50,15 @@ func SetupRouter() *gin.Engine {
r.GET("/auth/token", deviceTokenHandler) r.GET("/auth/token", deviceTokenHandler)
// Swagger API文档 (swaggo/gin-swagger持久化鉴权信息) // 开发模式下注册 API 文档路由
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, if config.App.Swagger.Enabled {
ginSwagger.PersistAuthorization(true), r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler,
)) ginSwagger.PersistAuthorization(true),
))
// Scalar 精美API文档 r.GET("/scalar", func(c *gin.Context) {
r.GET("/scalar", func(c *gin.Context) { c.Header("Content-Type", "text/html; charset=utf-8")
c.Header("Content-Type", "text/html; charset=utf-8") c.String(http.StatusOK, `<!doctype html>
c.String(http.StatusOK, `<!doctype html>
<html> <html>
<head> <head>
<title>API Reference</title> <title>API Reference</title>
@@ -71,16 +72,14 @@ func SetupRouter() *gin.Engine {
<div id="app"></div> <div id="app"></div>
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script> <script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
<script> <script>
Scalar.createApiReference('#app', { Scalar.createApiReference('#app', {
spec: { url: '/swagger/doc.json',
url: '/swagger/doc.json', })
},
persistAuth: true,
})
</script> </script>
</body> </body>
</html>`) </html>`)
}) })
}
v1 := r.Group("/api/v1") v1 := r.Group("/api/v1")
{ {