8898437e9e
- 引入 swaggo/gin-swagger 和 swaggo/files 依赖 - 配置 Swagger UI 路由 /swagger/* - 为测试 handler 函数添加 Swagger 注解 - 将 main.go 中的内联 handler 重构到 handler/test.go 以支持注解 - 生成 Swagger 文档 (docs.go, swagger.json, swagger.yaml) - Makefile 新增 make swagger 命令 - .gitignore 添加 server/docs/ 以跟踪生成的文档 - 添加 API 元信息(标题、版本、联系方式、许可证、安全定义)
342 lines
12 KiB
JSON
342 lines
12 KiB
JSON
{
|
||
"swagger": "2.0",
|
||
"info": {
|
||
"description": "xinfra 平台后端 API 文档",
|
||
"title": "xinfra API",
|
||
"termsOfService": "http://swagger.io/terms/",
|
||
"contact": {
|
||
"name": "API Support",
|
||
"url": "http://www.swagger.io/support",
|
||
"email": "support@swagger.io"
|
||
},
|
||
"license": {
|
||
"name": "Apache 2.0",
|
||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
||
},
|
||
"version": "1.0"
|
||
},
|
||
"host": "localhost:8080",
|
||
"basePath": "/api/v1",
|
||
"paths": {
|
||
"/ping": {
|
||
"get": {
|
||
"description": "检查服务器是否正常运行",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"系统"
|
||
],
|
||
"summary": "健康检查",
|
||
"responses": {
|
||
"200": {
|
||
"description": "OK",
|
||
"schema": {
|
||
"allOf": [
|
||
{
|
||
"$ref": "#/definitions/handler.Response"
|
||
},
|
||
{
|
||
"type": "object",
|
||
"properties": {
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"time": {
|
||
"type": "string"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/401": {
|
||
"get": {
|
||
"description": "模拟返回 HTTP 401 未授权错误",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试 401 错误",
|
||
"responses": {
|
||
"401": {
|
||
"description": "Unauthorized",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/403": {
|
||
"get": {
|
||
"description": "模拟返回 HTTP 403 禁止访问错误",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试 403 错误",
|
||
"responses": {
|
||
"403": {
|
||
"description": "Forbidden",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/500": {
|
||
"get": {
|
||
"description": "模拟返回 HTTP 500 服务器内部错误",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试 500 错误",
|
||
"responses": {
|
||
"500": {
|
||
"description": "Internal Server Error",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/error/{code}": {
|
||
"get": {
|
||
"description": "根据错误码返回对应的业务错误响应",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试业务错误响应",
|
||
"parameters": [
|
||
{
|
||
"type": "integer",
|
||
"description": "业务错误码 (10001-10099: 认证相关, 20001-20099: 任务相关)",
|
||
"name": "code",
|
||
"in": "path",
|
||
"required": true
|
||
}
|
||
],
|
||
"responses": {
|
||
"200": {
|
||
"description": "OK",
|
||
"schema": {
|
||
"allOf": [
|
||
{
|
||
"$ref": "#/definitions/handler.Response"
|
||
},
|
||
{
|
||
"type": "object",
|
||
"properties": {
|
||
"details": {
|
||
"type": "string"
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
},
|
||
"400": {
|
||
"description": "Bad Request",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/paginated": {
|
||
"get": {
|
||
"description": "测试返回分页数据的统一响应格式",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试分页响应",
|
||
"responses": {
|
||
"200": {
|
||
"description": "OK",
|
||
"schema": {
|
||
"allOf": [
|
||
{
|
||
"$ref": "#/definitions/handler.Response"
|
||
},
|
||
{
|
||
"type": "object",
|
||
"properties": {
|
||
"data": {
|
||
"allOf": [
|
||
{
|
||
"$ref": "#/definitions/handler.PaginatedData"
|
||
},
|
||
{
|
||
"type": "object",
|
||
"properties": {
|
||
"items": {
|
||
"type": "array",
|
||
"items": {
|
||
"type": "object"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/success": {
|
||
"get": {
|
||
"description": "测试返回成功的统一响应格式",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试成功响应",
|
||
"responses": {
|
||
"200": {
|
||
"description": "OK",
|
||
"schema": {
|
||
"allOf": [
|
||
{
|
||
"$ref": "#/definitions/handler.Response"
|
||
},
|
||
{
|
||
"type": "object",
|
||
"properties": {
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"role": {
|
||
"type": "string"
|
||
},
|
||
"username": {
|
||
"type": "string"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
]
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"/test/timeout": {
|
||
"get": {
|
||
"description": "模拟请求超时(15秒延迟,超过前端 10s timeout)",
|
||
"consumes": [
|
||
"application/json"
|
||
],
|
||
"produces": [
|
||
"application/json"
|
||
],
|
||
"tags": [
|
||
"测试"
|
||
],
|
||
"summary": "测试超时响应",
|
||
"responses": {
|
||
"200": {
|
||
"description": "OK",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
},
|
||
"408": {
|
||
"description": "Request Timeout",
|
||
"schema": {
|
||
"$ref": "#/definitions/handler.Response"
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"definitions": {
|
||
"handler.PaginatedData": {
|
||
"type": "object",
|
||
"properties": {
|
||
"items": {},
|
||
"total": {
|
||
"type": "integer"
|
||
}
|
||
}
|
||
},
|
||
"handler.Response": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {
|
||
"description": "业务错误码,0 表示成功",
|
||
"type": "integer"
|
||
},
|
||
"data": {
|
||
"description": "响应数据,成功时返回"
|
||
},
|
||
"details": {
|
||
"description": "错误详情,失败时返回",
|
||
"type": "string"
|
||
},
|
||
"message": {
|
||
"description": "响应消息",
|
||
"type": "string"
|
||
}
|
||
}
|
||
}
|
||
},
|
||
"securityDefinitions": {
|
||
"BearerAuth": {
|
||
"description": "请输入 Bearer Token(例如:Bearer xxx)",
|
||
"type": "apiKey",
|
||
"name": "Authorization",
|
||
"in": "header"
|
||
}
|
||
}
|
||
} |