feat: integrate swagger documentation and gin cors middleware
This commit is contained in:
49
main.go
49
main.go
@@ -1,11 +1,34 @@
|
||||
package main
|
||||
|
||||
// @title Trace System API
|
||||
// @version 1.0
|
||||
// @description 防伪溯源系统 API 文档
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
|
||||
// @contact.name API Support
|
||||
// @contact.url http://www.swagger.io/support
|
||||
// @contact.email support@swagger.io
|
||||
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
// @host localhost:8080
|
||||
// @BasePath /api
|
||||
// @securityDefinitions.apikey BearerAuth
|
||||
// @in header
|
||||
// @name Authorization
|
||||
// @description Bearer token 认证
|
||||
|
||||
import (
|
||||
"git.beifan.cn/trace-system/backend-go/config"
|
||||
"git.beifan.cn/trace-system/backend-go/database"
|
||||
_ "git.beifan.cn/trace-system/backend-go/docs"
|
||||
"git.beifan.cn/trace-system/backend-go/logger"
|
||||
"git.beifan.cn/trace-system/backend-go/routes"
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerFiles "github.com/swaggo/files"
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -45,18 +68,14 @@ func main() {
|
||||
r.Use(gin.Recovery())
|
||||
|
||||
// 启用 CORS
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT, DELETE")
|
||||
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(204)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
})
|
||||
r.Use(cors.New(cors.Config{
|
||||
AllowOrigins: []string{"*"},
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization", "accept", "origin", "Cache-Control", "X-Requested-With"},
|
||||
ExposeHeaders: []string{"Content-Length"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 12 * 3600,
|
||||
}))
|
||||
|
||||
// 配置路由
|
||||
routes.SetupRoutes(r)
|
||||
@@ -65,10 +84,14 @@ func main() {
|
||||
api := r.Group("/api")
|
||||
routes.SetupAPIRoutes(api)
|
||||
|
||||
// Swagger 文档路由
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
// 启动服务器
|
||||
port := cfg.Server.Port
|
||||
logger.Info("服务器运行在 http://localhost:" + port)
|
||||
logger.Info("API 文档: http://localhost:" + port + "/api/health")
|
||||
logger.Info("Swagger 文档: http://localhost:" + port + "/swagger/index.html")
|
||||
logger.Info("健康检查: http://localhost:" + port + "/api/health")
|
||||
logger.Info("环境: " + cfg.Server.Environment)
|
||||
|
||||
if err := r.Run(":" + port); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user