diff --git a/.gitignore b/.gitignore index d42dadf..dd56108 100644 --- a/.gitignore +++ b/.gitignore @@ -51,5 +51,6 @@ docker-compose.override.yml # Documentation & local dev config (not tracked in repo) docs/ +!server/docs/ CLAUDE.md AGENTS.md diff --git a/Makefile b/Makefile index 0426e38..9009f5f 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ help: @echo " make test - 运行测试" @echo " make docker - 构建 Docker 镜像" @echo " make clean - 清理构建产物" + @echo " make swagger - 生成 Swagger API 文档" @echo "" # 构建前后端项目 @@ -26,16 +27,13 @@ build-server: @echo "Building server..." cd server && go build -o bin/xinfra ./cmd/server -# 启动开发服务器 -run: run-frontend run-server - -run-frontend: - @echo "Starting frontend dev server..." - cd frontend && npm run dev - -run-server: - @echo "Starting server..." - cd server && go run ./cmd/server +# 启动开发服务器(前后端并行) +run: + @echo "Starting development servers..." + @trap 'kill 0' EXIT; \ + cd frontend && npm run dev & \ + cd server && go run ./cmd/server & \ + wait # 运行测试 test: test-frontend test-server @@ -75,6 +73,12 @@ dev-down: @echo "Stopping local development environment..." docker-compose down +# Swagger 文档 +swagger: + @echo "Generating Swagger documentation..." + cd server && swag init -g internal/router/router.go -o ./docs + @echo "Swagger docs generated at server/docs/" + # 数据库迁移 migrate-up: @echo "Running database migrations..." diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json index 4e5ea1b..27918ea 100644 --- a/frontend/tsconfig.json +++ b/frontend/tsconfig.json @@ -15,9 +15,8 @@ "noUnusedLocals": true, "noUnusedParameters": true, "noFallthroughCasesInSwitch": true, - "baseUrl": ".", "paths": { - "@/*": ["src/*"] + "@/*": ["./src/*"] } }, "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"], diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3fc0822..c06670d 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -31,6 +31,10 @@ export default defineConfig({ target: 'http://localhost:8080', changeOrigin: true, }, + '/swagger': { + target: 'http://localhost:8080', + changeOrigin: true, + }, }, }, build: { diff --git a/server/Dockerfile.backend b/server/Dockerfile.backend index 527ec8e..40c40f7 100644 --- a/server/Dockerfile.backend +++ b/server/Dockerfile.backend @@ -9,6 +9,7 @@ COPY go.mod go.sum ./ RUN go mod download COPY cmd ./cmd +COPY docs ./docs COPY internal ./internal RUN CGO_ENABLED=0 GOOS=linux go build -o /out/authserver ./cmd/server diff --git a/server/cmd/server/main.go b/server/cmd/server/main.go index d7db5b6..fea67b2 100644 --- a/server/cmd/server/main.go +++ b/server/cmd/server/main.go @@ -3,6 +3,7 @@ package main import ( "log" + _ "github.com/1024XEngineer/xinfra/server/docs" "github.com/1024XEngineer/xinfra/server/internal/config" "github.com/1024XEngineer/xinfra/server/internal/database" "github.com/1024XEngineer/xinfra/server/internal/router" diff --git a/server/docs/docs.go b/server/docs/docs.go new file mode 100644 index 0000000..9a6275c --- /dev/null +++ b/server/docs/docs.go @@ -0,0 +1,366 @@ +// Package docs Code generated by swaggo/swag. DO NOT EDIT +package docs + +import "github.com/swaggo/swag" + +const docTemplate = `{ + "schemes": {{ marshal .Schemes }}, + "swagger": "2.0", + "info": { + "description": "{{escape .Description}}", + "title": "{{.Title}}", + "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": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "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" + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "", + BasePath: "/api/v1", + Schemes: []string{}, + Title: "xinfra API", + Description: "xinfra 平台后端 API 文档", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/server/docs/swagger.json b/server/docs/swagger.json new file mode 100644 index 0000000..17a1625 --- /dev/null +++ b/server/docs/swagger.json @@ -0,0 +1,341 @@ +{ + "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" + }, + "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" + } + } +} \ No newline at end of file diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml new file mode 100644 index 0000000..183efc1 --- /dev/null +++ b/server/docs/swagger.yaml @@ -0,0 +1,211 @@ +basePath: /api/v1 +definitions: + handler.PaginatedData: + properties: + items: {} + total: + type: integer + type: object + handler.Response: + properties: + code: + description: 业务错误码,0 表示成功 + type: integer + data: + description: 响应数据,成功时返回 + details: + description: 错误详情,失败时返回 + type: string + message: + description: 响应消息 + type: string + type: object +info: + contact: + email: support@swagger.io + name: API Support + url: http://www.swagger.io/support + description: xinfra 平台后端 API 文档 + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + termsOfService: http://swagger.io/terms/ + title: xinfra API + version: "1.0" +paths: + /ping: + get: + consumes: + - application/json + description: 检查服务器是否正常运行 + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/handler.Response' + - properties: + data: + properties: + time: + type: string + type: object + type: object + summary: 健康检查 + tags: + - 系统 + /test/401: + get: + consumes: + - application/json + description: 模拟返回 HTTP 401 未授权错误 + produces: + - application/json + responses: + "401": + description: Unauthorized + schema: + $ref: '#/definitions/handler.Response' + summary: 测试 401 错误 + tags: + - 测试 + /test/403: + get: + consumes: + - application/json + description: 模拟返回 HTTP 403 禁止访问错误 + produces: + - application/json + responses: + "403": + description: Forbidden + schema: + $ref: '#/definitions/handler.Response' + summary: 测试 403 错误 + tags: + - 测试 + /test/500: + get: + consumes: + - application/json + description: 模拟返回 HTTP 500 服务器内部错误 + produces: + - application/json + responses: + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/handler.Response' + summary: 测试 500 错误 + tags: + - 测试 + /test/error/{code}: + get: + consumes: + - application/json + description: 根据错误码返回对应的业务错误响应 + parameters: + - description: '业务错误码 (10001-10099: 认证相关, 20001-20099: 任务相关)' + in: path + name: code + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/handler.Response' + - properties: + details: + type: string + type: object + "400": + description: Bad Request + schema: + $ref: '#/definitions/handler.Response' + summary: 测试业务错误响应 + tags: + - 测试 + /test/paginated: + get: + consumes: + - application/json + description: 测试返回分页数据的统一响应格式 + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/handler.Response' + - properties: + data: + allOf: + - $ref: '#/definitions/handler.PaginatedData' + - properties: + items: + items: + type: object + type: array + type: object + type: object + summary: 测试分页响应 + tags: + - 测试 + /test/success: + get: + consumes: + - application/json + description: 测试返回成功的统一响应格式 + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/handler.Response' + - properties: + data: + properties: + role: + type: string + username: + type: string + type: object + type: object + summary: 测试成功响应 + tags: + - 测试 + /test/timeout: + get: + consumes: + - application/json + description: 模拟请求超时(15秒延迟,超过前端 10s timeout) + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.Response' + "408": + description: Request Timeout + schema: + $ref: '#/definitions/handler.Response' + summary: 测试超时响应 + tags: + - 测试 +securityDefinitions: + BearerAuth: + description: 请输入 Bearer Token(例如:Bearer xxx) + in: header + name: Authorization + type: apiKey +swagger: "2.0" diff --git a/server/go.mod b/server/go.mod index 8fe33ab..a52b91b 100644 --- a/server/go.mod +++ b/server/go.mod @@ -5,17 +5,27 @@ go 1.26 require ( github.com/gin-gonic/gin v1.11.0 github.com/golang-jwt/jwt/v5 v5.3.0 + github.com/swaggo/files v1.0.1 + github.com/swaggo/gin-swagger v1.6.1 + github.com/swaggo/swag v1.16.3 gorm.io/driver/mysql v1.6.0 gorm.io/gorm v1.30.1 ) require ( filippo.io/edwards25519 v1.1.0 // indirect + github.com/KyleBanks/depth v1.2.1 // indirect + github.com/PuerkitoBio/purell v1.1.1 // indirect + github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect github.com/bytedance/sonic v1.14.0 // indirect github.com/bytedance/sonic/loader v0.3.0 // indirect github.com/cloudwego/base64x v0.1.6 // indirect github.com/gabriel-vasile/mimetype v1.4.8 // indirect github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-openapi/jsonpointer v0.19.5 // indirect + github.com/go-openapi/jsonreference v0.19.6 // indirect + github.com/go-openapi/spec v0.20.4 // indirect + github.com/go-openapi/swag v0.19.15 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-playground/validator/v10 v10.27.0 // indirect @@ -24,11 +34,13 @@ require ( github.com/goccy/go-yaml v1.18.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect + github.com/mailru/easyjson v0.7.6 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect github.com/quic-go/qpack v0.5.1 // indirect @@ -45,4 +57,5 @@ require ( golang.org/x/text v0.27.0 // indirect golang.org/x/tools v0.34.0 // indirect google.golang.org/protobuf v1.36.9 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/server/go.sum b/server/go.sum index d4f772e..d16bd2e 100644 --- a/server/go.sum +++ b/server/go.sum @@ -1,20 +1,39 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= +github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc= +github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE= +github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI= +github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M= +github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE= github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ= github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA= github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA= github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI= github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM= github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8= +github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= +github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= +github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY= +github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= +github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs= +github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns= +github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M= +github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I= +github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk= +github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= +github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -38,18 +57,32 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y= github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= +github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= +github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -62,38 +95,82 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= +github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= +github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs7cY= +github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw= +github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= +github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.0 h1:Qd2W2sQawAfG8XSvzwhBeoGq71zXOC/Q1E9y/wUcsUA= github.com/ugorji/go/codec v1.3.0/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/mock v0.5.0 h1:KAMbZvZPyBPWgD14IrIQ38QCyjwpvVVV6K/bHl1IwQU= go.uber.org/mock v0.5.0/go.mod h1:ge71pBPLYDk7QIi1LupWxdAykm7KIEFchiOqd6z7qMM= golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c= golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.40.0 h1:r4x+VvoG5Fm+eJcxMaY8CQM7Lb0l1lsmjGBQ6s8BfKM= golang.org/x/crypto v0.40.0/go.mod h1:Qr1vMER5WyS2dfPHAlsOj01wgLbsyWtFn/aY+5+ZdxY= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w= golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.42.0 h1:jzkYrhi3YQWD6MLBJcsklgQsoAcw89EcZbJw8Z614hs= golang.org/x/net v0.42.0/go.mod h1:FF1RA5d3u7nAYA4z2TkclSCKh68eSXtiFwcWQpPXdt8= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI= golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.27.0 h1:4fGWRpyh641NLlecmyl4LOe6yDdfaYNrGb2zdfo4JV4= golang.org/x/text v0.27.0/go.mod h1:1D28KMCvyooCX9hBiosv5Tz/+YLxj0j7XhWjpSUF7CU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo= golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.36.9 h1:w2gp2mA27hUeUzj9Ex9FBjsBm40zfaDtEWow293U7Iw= google.golang.org/protobuf v1.36.9/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.6.0 h1:eNbLmNTpPpTOVZi8MMxCi2aaIm0ZpInbORNXDwyLGvg= diff --git a/server/internal/handler/test.go b/server/internal/handler/test.go new file mode 100644 index 0000000..1b3ca88 --- /dev/null +++ b/server/internal/handler/test.go @@ -0,0 +1,127 @@ +package handler + +import ( + "fmt" + "net/http" + "strconv" + "time" + + "github.com/1024XEngineer/xinfra/server/internal/response" + + "github.com/gin-gonic/gin" +) + +// Ping 健康检查 +// @Summary 健康检查 +// @Description 检查服务器是否正常运行 +// @Tags 系统 +// @Accept json +// @Produce json +// @Success 200 {object} Response{data=object{time=string}} +// @Router /ping [get] +func Ping(c *gin.Context) { + response.Success(c, gin.H{"time": time.Now().Format(time.RFC3339)}) +} + +// TestSuccess 测试成功响应 +// @Summary 测试成功响应 +// @Description 测试返回成功的统一响应格式 +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 200 {object} Response{data=object{username=string,role=string}} +// @Router /test/success [get] +func TestSuccess(c *gin.Context) { + response.Success(c, gin.H{ + "username": "testuser", + "role": "admin", + }) +} + +// TestError 测试业务错误响应 +// @Summary 测试业务错误响应 +// @Description 根据错误码返回对应的业务错误响应 +// @Tags 测试 +// @Accept json +// @Produce json +// @Param code path int true "业务错误码 (10001-10099: 认证相关, 20001-20099: 任务相关)" +// @Success 200 {object} Response{details=string} +// @Failure 400 {object} Response +// @Router /test/error/{code} [get] +func TestError(c *gin.Context) { + codeStr := c.Param("code") + code, err := strconv.Atoi(codeStr) + if err != nil { + response.BadRequest(c, "错误码必须是数字") + return + } + details := fmt.Sprintf("这是错误码 %d 的测试响应", code) + response.Error(c, http.StatusOK, code, details) +} + +// Test500 测试 500 错误 +// @Summary 测试 500 错误 +// @Description 模拟返回 HTTP 500 服务器内部错误 +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 500 {object} Response +// @Router /test/500 [get] +func Test500(c *gin.Context) { + response.InternalError(c, "模拟服务器内部错误") +} + +// Test401 测试 401 错误 +// @Summary 测试 401 错误 +// @Description 模拟返回 HTTP 401 未授权错误 +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 401 {object} Response +// @Router /test/401 [get] +func Test401(c *gin.Context) { + response.Unauthorized(c, "模拟 Token 过期") +} + +// Test403 测试 403 错误 +// @Summary 测试 403 错误 +// @Description 模拟返回 HTTP 403 禁止访问错误 +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 403 {object} Response +// @Router /test/403 [get] +func Test403(c *gin.Context) { + response.Forbidden(c, "模拟无权限") +} + +// TestTimeout 测试超时响应 +// @Summary 测试超时响应 +// @Description 模拟请求超时(15秒延迟,超过前端 10s timeout) +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 200 {object} Response +// @Failure 408 {object} Response +// @Router /test/timeout [get] +func TestTimeout(c *gin.Context) { + time.Sleep(15 * time.Second) + response.Success(c, "这条消息不应该出现") +} + +// TestPaginated 测试分页响应 +// @Summary 测试分页响应 +// @Description 测试返回分页数据的统一响应格式 +// @Tags 测试 +// @Accept json +// @Produce json +// @Success 200 {object} Response{data=PaginatedData{items=[]object}} +// @Router /test/paginated [get] +func TestPaginated(c *gin.Context) { + items := []gin.H{ + {"id": 1, "name": "item-1"}, + {"id": 2, "name": "item-2"}, + {"id": 3, "name": "item-3"}, + } + response.SuccessWithPaginated(c, 100, items) +} diff --git a/server/internal/router/router.go b/server/internal/router/router.go index 583edbf..9ccc733 100644 --- a/server/internal/router/router.go +++ b/server/internal/router/router.go @@ -6,6 +6,8 @@ import ( "github.com/1024XEngineer/xinfra/server/internal/service" "github.com/gin-gonic/gin" + swaggerFiles "github.com/swaggo/files" + ginSwagger "github.com/swaggo/gin-swagger" "gorm.io/gorm" ) @@ -14,6 +16,21 @@ type Dependencies struct { DB *gorm.DB } +// New 初始化路由。 +// @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 New(deps Dependencies) *gin.Engine { if deps.Config.AppEnv == "prod" { gin.SetMode(gin.ReleaseMode) @@ -22,6 +39,33 @@ func New(deps Dependencies) *gin.Engine { r := gin.New() r.Use(gin.Logger(), gin.Recovery()) + registerSwaggerRoutes(r) + registerTestRoutes(r) + registerAuthServerRoutes(r, deps) + + return r +} + +func registerSwaggerRoutes(r *gin.Engine) { + r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.URL("/swagger/doc.json"))) +} + +func registerTestRoutes(r *gin.Engine) { + r.GET("/api/v1/ping", handler.Ping) + + testGroup := r.Group("/api/v1/test") + { + testGroup.GET("/success", handler.TestSuccess) + testGroup.GET("/error/:code", handler.TestError) + testGroup.GET("/500", handler.Test500) + testGroup.GET("/401", handler.Test401) + testGroup.GET("/403", handler.Test403) + testGroup.GET("/timeout", handler.TestTimeout) + testGroup.GET("/paginated", handler.TestPaginated) + } +} + +func registerAuthServerRoutes(r *gin.Engine, deps Dependencies) { auditService := service.NewAuditService(deps.DB) authService := service.NewAuthService(deps.Config, deps.DB, auditService) wayenService := service.NewWayenService(deps.Config, deps.DB) @@ -55,6 +99,4 @@ func New(deps Dependencies) *gin.Engine { protected.PUT("/wayen/credential", wayenHandler.SaveCredential) protected.GET("/clouddm/login", clouddmHandler.Login) } - - return r }