Files
xinfra/server/cmd/server/main.go
T
wonder 1d799ac0e7 feat(server): 优化 Swagger 配置,支持动态 host
- 移除 swagger.json/yaml 中硬编码的 host 字段
- 在 docs.go 中将 Host 设置为空字符串以支持动态检测
- 添加前端 /swagger 代理配置,支持开发环境访问 Swagger UI
- 在 main.go 中导入 docs 包注册 Swagger 文档
2026-07-15 13:30:59 +08:00

81 lines
2.1 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 main
import (
"fmt"
_ "github.com/1024XEngineer/xinfra/server/docs"
"github.com/1024XEngineer/xinfra/server/internal/handler"
"github.com/1024XEngineer/xinfra/server/internal/middleware"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
// @title xinfra API
// @version 1.0
// @description xinfra 平台后端 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
// @BasePath /api/v1
// @securityDefinitions.apikey BearerAuth
// @in header
// @name Authorization
// @description 请输入 Bearer Token(例如:Bearer xxx)
func main() {
r := gin.Default()
// 加载中间件
r.Use(middleware.Logger())
r.Use(middleware.Recovery())
// Swagger UI
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("/swagger/doc.json")))
// 健康检查
r.GET("/api/v1/ping", handler.Ping)
// 测试路由组 — 用于验证统一响应格式
testGroup := r.Group("/api/v1/test")
{
// 成功响应
testGroup.GET("/success", handler.TestSuccess)
// 业务错误 — 传入错误码
// GET /api/v1/test/error/10001 → LDAP 认证失败
// GET /api/v1/test/error/10002 → 用户名或密码错误
// GET /api/v1/test/error/10003 → Token 已过期
// GET /api/v1/test/error/20001 → 任务创建失败
// GET /api/v1/test/error/20002 → 任务执行超时
testGroup.GET("/error/:code", handler.TestError)
// HTTP 500 错误
testGroup.GET("/500", handler.Test500)
// HTTP 401 未授权
testGroup.GET("/401", handler.Test401)
// HTTP 403 禁止访问
testGroup.GET("/403", handler.Test403)
// 模拟超时(sleep 超过前端 10s timeout)
testGroup.GET("/timeout", handler.TestTimeout)
// 返回分页数据
testGroup.GET("/paginated", handler.TestPaginated)
}
fmt.Println("Server starting on :8080...")
if err := r.Run(":8080"); err != nil {
panic(err)
}
}