package docs import ( "encoding/json" "strings" "github.com/swaggo/swag" ) const docTemplate = `{ "swagger": "2.0", "info": { "description": "{{.Description}}", "title": "{{.Title}}", "contact": {}, "license": {}, "version": "{{.Version}}" }, "host": "{{.Host}}", "basePath": "{{.BasePath}}", "schemes": {{ marshal .Schemes }}, "paths": { "/api/v1/users": { "get": { "tags": [ "users" ], "summary": "获取用户列表", "description": "返回所有用户", "produces": [ "application/json" ], "responses": { "200": { "description": "OK", "schema": { "type": "array", "items": { "$ref": "#/definitions/model.User" } } }, "500": { "description": "Internal Server Error", "schema": { "$ref": "#/definitions/http.ErrorResponse" } } } }, "post": { "tags": [ "users" ], "summary": "创建用户", "description": "创建新的用户记录", "consumes": [ "application/json" ], "produces": [ "application/json" ], "parameters": [ { "description": "用户数据", "name": "user", "in": "body", "required": true, "schema": { "$ref": "#/definitions/model.User" } } ], "responses": { "201": { "description": "Created", "schema": { "$ref": "#/definitions/model.User" } }, "400": { "description": "Bad Request", "schema": { "$ref": "#/definitions/http.ErrorResponse" } }, "500": { "description": "Internal Server Error", "schema": { "$ref": "#/definitions/http.ErrorResponse" } } } } }, "/api/v1/users/{id}": { "get": { "tags": [ "users" ], "summary": "获取指定用户", "description": "根据用户 ID 获取详情", "produces": [ "application/json" ], "parameters": [ { "type": "integer", "format": "int64", "description": "用户 ID", "name": "id", "in": "path", "required": true } ], "responses": { "200": { "description": "OK", "schema": { "$ref": "#/definitions/model.User" } }, "400": { "description": "Bad Request", "schema": { "$ref": "#/definitions/http.ErrorResponse" } }, "404": { "description": "Not Found", "schema": { "$ref": "#/definitions/http.ErrorResponse" } } } } } }, "definitions": { "http.ErrorResponse": { "type": "object", "properties": { "error": { "type": "string" } } }, "model.User": { "type": "object", "properties": { "ID": { "type": "integer", "format": "int64" }, "CreatedAt": { "type": "string", "format": "date-time" }, "UpdatedAt": { "type": "string", "format": "date-time" }, "DeletedAt": { "type": "string", "format": "date-time" }, "name": { "type": "string" }, "email": { "type": "string" } } } } }` type swaggerInfo struct { Version string Host string BasePath string Schemes []string Title string Description string } // SwaggerInfo holds exported Swagger Info so clients can modify it var SwaggerInfo = swaggerInfo{ Version: "1.0", Host: "", BasePath: "/api/v1", Schemes: []string{"http"}, Title: "Aurora Service API", Description: "Aurora Golang 示例 REST API,基于 Gin + GORM。", } type s struct{} func (s *s) ReadDoc() string { doc := docTemplate doc = strings.Replace(doc, "{{.Version}}", SwaggerInfo.Version, -1) doc = strings.Replace(doc, "{{.Host}}", SwaggerInfo.Host, -1) doc = strings.Replace(doc, "{{.BasePath}}", SwaggerInfo.BasePath, -1) doc = strings.Replace(doc, "{{.Title}}", SwaggerInfo.Title, -1) doc = strings.Replace(doc, "{{.Description}}", SwaggerInfo.Description, -1) doc = strings.Replace(doc, "{{ marshal .Schemes }}", marshal(SwaggerInfo.Schemes), -1) return doc } func marshal(v interface{}) string { if v == nil { return "[]" } b, err := json.Marshal(v) if err != nil { return "[]" } return strings.ReplaceAll(string(b), `\u0026`, "&") } func init() { swag.Register(swag.Name, &s{}) }