swag 测试

This commit is contained in:
Shikong 2022-10-03 15:37:55 +08:00
parent a28624893d
commit 4f350ad858
7 changed files with 435 additions and 39 deletions

4
.gitignore vendored
View File

@ -88,4 +88,6 @@ fabric.properties
# Dependency directories (remove the comment below to include it)
# vendor/
etc
etc
tmp/

View File

@ -2,7 +2,9 @@
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>

View File

@ -4,7 +4,6 @@ import "gofiber.study.skcks.cn/common/response"
type CodeError struct {
*response.Response
Code response.Code `json:"code" example:"500"`
}
type CodeErrorResponse struct {
@ -14,14 +13,12 @@ type CodeErrorResponse struct {
func NewCustomError(code int, data interface{}, msg string) error {
return &CodeError{
Response: response.NewCustomResponse(code, data, msg),
Code: code,
}
}
func NewErrorWithCode(code int, msg string) error {
return &CodeError{
Response: response.NewCustomResponse(code, nil, msg),
Code: code,
}
}

View File

@ -41,23 +41,151 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"type": "string"
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"400": {
"description": "Bad Request",
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
},
"404": {
"description": "Not Found",
}
}
}
},
"/error": {
"get": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
}
}
},
"put": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"post": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"delete": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"head": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"patch": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
@ -80,6 +208,20 @@ const docTemplate = `{
"example": "OK"
}
}
},
"response.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
}
}
}`

View File

@ -33,23 +33,151 @@
"200": {
"description": "OK",
"schema": {
"type": "string"
"allOf": [
{
"$ref": "#/definitions/response.Response"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
]
}
},
"400": {
"description": "Bad Request",
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
},
"404": {
"description": "Not Found",
}
}
}
},
"/error": {
"get": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
},
"500": {
"description": "Internal Server Error",
}
}
},
"put": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"post": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"delete": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"head": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
}
}
},
"patch": {
"description": "错误信息示例",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Error"
],
"summary": "错误处理",
"responses": {
"default": {
"description": "",
"schema": {
"$ref": "#/definitions/errorx.CodeErrorResponse"
}
@ -72,6 +200,20 @@
"example": "OK"
}
}
},
"response.Response": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 200
},
"data": {},
"msg": {
"type": "string",
"example": "OK"
}
}
}
}
}

View File

@ -10,6 +10,16 @@ definitions:
example: OK
type: string
type: object
response.Response:
properties:
code:
example: 200
type: integer
data: {}
msg:
example: OK
type: string
type: object
info:
contact:
email: 919411476@qq.com
@ -33,20 +43,102 @@ paths:
"200":
description: OK
schema:
type: string
"400":
description: Bad Request
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
"404":
description: Not Found
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
"500":
description: Internal Server Error
allOf:
- $ref: '#/definitions/response.Response'
- properties:
data:
type: string
type: object
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: HelloWorld
tags:
- HelloWorld
/error:
delete:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
get:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
head:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
patch:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
post:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
put:
consumes:
- application/json
description: 错误信息示例
produces:
- application/json
responses:
default:
description: ""
schema:
$ref: '#/definitions/errorx.CodeErrorResponse'
summary: 错误处理
tags:
- Error
swagger: "2.0"

29
main.go
View File

@ -50,10 +50,11 @@ func main() {
app.Get("/swagger/*", swagger.New(swagger.Config{
DeepLinking: false,
// Expand ("list") or Collapse ("none") tag groups by default
DocExpansion: "full",
DocExpansion: "list",
}))
helloWorld(app)
errorHandler(app)
app.Get("/routes", func(ctx *fiber.Ctx) error {
return ctx.JSON(response.NewResponse(app.Stack()))
@ -78,10 +79,8 @@ func main() {
// @Tags HelloWorld
// @Accept json
// @Produce json
// @Success 200 {string} string
// @Failure 400 {object} errorx.CodeErrorResponse
// @Failure 404 {object} errorx.CodeErrorResponse
// @Failure 500 {object} errorx.CodeErrorResponse
// @Success 200 {object} response.Response{data=string}
// @Failure default {object} errorx.CodeErrorResponse
// @Router / [get]
func helloWorld(app *fiber.App) {
app.Get("/", func(c *fiber.Ctx) error {
@ -89,3 +88,23 @@ func helloWorld(app *fiber.App) {
return c.JSON(response.NewResponse("Hello, World 👋!"))
})
}
// errorHandler
//
// @Summary 错误处理
// @Description 错误信息示例
// @Tags Error
// @Accept json
// @Produce json
// @Failure default {object} errorx.CodeErrorResponse
// @Router /error [get]
// @Router /error [post]
// @Router /error [put]
// @Router /error [delete]
// @Router /error [patch]
// @Router /error [head]
func errorHandler(app *fiber.App) {
app.All("/error", func(ctx *fiber.Ctx) error {
return ctx.JSON(errorx.NewDefaultError("错误信息示例"))
})
}