mirror of
https://gitee.com/shikong-sk/gofiber-study
synced 2025-02-23 15:22:14 +08:00
swag 测试
This commit is contained in:
parent
a28624893d
commit
4f350ad858
2
.gitignore
vendored
2
.gitignore
vendored
@ -89,3 +89,5 @@ fabric.properties
|
|||||||
# vendor/
|
# vendor/
|
||||||
|
|
||||||
etc
|
etc
|
||||||
|
|
||||||
|
tmp/
|
@ -2,7 +2,9 @@
|
|||||||
<module type="WEB_MODULE" version="4">
|
<module type="WEB_MODULE" version="4">
|
||||||
<component name="Go" enabled="true" />
|
<component name="Go" enabled="true" />
|
||||||
<component name="NewModuleRootManager">
|
<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="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
</component>
|
</component>
|
||||||
|
@ -4,7 +4,6 @@ import "gofiber.study.skcks.cn/common/response"
|
|||||||
|
|
||||||
type CodeError struct {
|
type CodeError struct {
|
||||||
*response.Response
|
*response.Response
|
||||||
Code response.Code `json:"code" example:"500"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CodeErrorResponse struct {
|
type CodeErrorResponse struct {
|
||||||
@ -14,14 +13,12 @@ type CodeErrorResponse struct {
|
|||||||
func NewCustomError(code int, data interface{}, msg string) error {
|
func NewCustomError(code int, data interface{}, msg string) error {
|
||||||
return &CodeError{
|
return &CodeError{
|
||||||
Response: response.NewCustomResponse(code, data, msg),
|
Response: response.NewCustomResponse(code, data, msg),
|
||||||
Code: code,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewErrorWithCode(code int, msg string) error {
|
func NewErrorWithCode(code int, msg string) error {
|
||||||
return &CodeError{
|
return &CodeError{
|
||||||
Response: response.NewCustomResponse(code, nil, msg),
|
Response: response.NewCustomResponse(code, nil, msg),
|
||||||
Code: code,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
154
docs/docs.go
154
docs/docs.go
@ -41,23 +41,151 @@ const docTemplate = `{
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
"schema": {
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"default": {
|
||||||
"description": "Bad Request",
|
"description": "",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"404": {
|
"/error": {
|
||||||
"description": "Not Found",
|
"get": {
|
||||||
|
"description": "错误信息示例",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Error"
|
||||||
|
],
|
||||||
|
"summary": "错误处理",
|
||||||
|
"responses": {
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"500": {
|
"put": {
|
||||||
"description": "Internal Server Error",
|
"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": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
@ -80,6 +208,20 @@ const docTemplate = `{
|
|||||||
"example": "OK"
|
"example": "OK"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"response.Response": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"code": {
|
||||||
|
"type": "integer",
|
||||||
|
"example": 200
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"msg": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "OK"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}`
|
}`
|
||||||
|
@ -33,23 +33,151 @@
|
|||||||
"200": {
|
"200": {
|
||||||
"description": "OK",
|
"description": "OK",
|
||||||
"schema": {
|
"schema": {
|
||||||
|
"allOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/response.Response"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"data": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"default": {
|
||||||
"description": "Bad Request",
|
"description": "",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"404": {
|
"/error": {
|
||||||
"description": "Not Found",
|
"get": {
|
||||||
|
"description": "错误信息示例",
|
||||||
|
"consumes": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"produces": [
|
||||||
|
"application/json"
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Error"
|
||||||
|
],
|
||||||
|
"summary": "错误处理",
|
||||||
|
"responses": {
|
||||||
|
"default": {
|
||||||
|
"description": "",
|
||||||
"schema": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"500": {
|
"put": {
|
||||||
"description": "Internal Server Error",
|
"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": {
|
"schema": {
|
||||||
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
"$ref": "#/definitions/errorx.CodeErrorResponse"
|
||||||
}
|
}
|
||||||
@ -72,6 +200,20 @@
|
|||||||
"example": "OK"
|
"example": "OK"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"response.Response": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"code": {
|
||||||
|
"type": "integer",
|
||||||
|
"example": 200
|
||||||
|
},
|
||||||
|
"data": {},
|
||||||
|
"msg": {
|
||||||
|
"type": "string",
|
||||||
|
"example": "OK"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -10,6 +10,16 @@ definitions:
|
|||||||
example: OK
|
example: OK
|
||||||
type: string
|
type: string
|
||||||
type: object
|
type: object
|
||||||
|
response.Response:
|
||||||
|
properties:
|
||||||
|
code:
|
||||||
|
example: 200
|
||||||
|
type: integer
|
||||||
|
data: {}
|
||||||
|
msg:
|
||||||
|
example: OK
|
||||||
|
type: string
|
||||||
|
type: object
|
||||||
info:
|
info:
|
||||||
contact:
|
contact:
|
||||||
email: 919411476@qq.com
|
email: 919411476@qq.com
|
||||||
@ -33,20 +43,102 @@ paths:
|
|||||||
"200":
|
"200":
|
||||||
description: OK
|
description: OK
|
||||||
schema:
|
schema:
|
||||||
|
allOf:
|
||||||
|
- $ref: '#/definitions/response.Response'
|
||||||
|
- properties:
|
||||||
|
data:
|
||||||
type: string
|
type: string
|
||||||
"400":
|
type: object
|
||||||
description: Bad Request
|
default:
|
||||||
schema:
|
description: ""
|
||||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
|
||||||
"404":
|
|
||||||
description: Not Found
|
|
||||||
schema:
|
|
||||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
|
||||||
"500":
|
|
||||||
description: Internal Server Error
|
|
||||||
schema:
|
schema:
|
||||||
$ref: '#/definitions/errorx.CodeErrorResponse'
|
$ref: '#/definitions/errorx.CodeErrorResponse'
|
||||||
summary: HelloWorld
|
summary: HelloWorld
|
||||||
tags:
|
tags:
|
||||||
- HelloWorld
|
- 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"
|
swagger: "2.0"
|
||||||
|
29
main.go
29
main.go
@ -50,10 +50,11 @@ func main() {
|
|||||||
app.Get("/swagger/*", swagger.New(swagger.Config{
|
app.Get("/swagger/*", swagger.New(swagger.Config{
|
||||||
DeepLinking: false,
|
DeepLinking: false,
|
||||||
// Expand ("list") or Collapse ("none") tag groups by default
|
// Expand ("list") or Collapse ("none") tag groups by default
|
||||||
DocExpansion: "full",
|
DocExpansion: "list",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
helloWorld(app)
|
helloWorld(app)
|
||||||
|
errorHandler(app)
|
||||||
|
|
||||||
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
app.Get("/routes", func(ctx *fiber.Ctx) error {
|
||||||
return ctx.JSON(response.NewResponse(app.Stack()))
|
return ctx.JSON(response.NewResponse(app.Stack()))
|
||||||
@ -78,10 +79,8 @@ func main() {
|
|||||||
// @Tags HelloWorld
|
// @Tags HelloWorld
|
||||||
// @Accept json
|
// @Accept json
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {string} string
|
// @Success 200 {object} response.Response{data=string}
|
||||||
// @Failure 400 {object} errorx.CodeErrorResponse
|
// @Failure default {object} errorx.CodeErrorResponse
|
||||||
// @Failure 404 {object} errorx.CodeErrorResponse
|
|
||||||
// @Failure 500 {object} errorx.CodeErrorResponse
|
|
||||||
// @Router / [get]
|
// @Router / [get]
|
||||||
func helloWorld(app *fiber.App) {
|
func helloWorld(app *fiber.App) {
|
||||||
app.Get("/", func(c *fiber.Ctx) error {
|
app.Get("/", func(c *fiber.Ctx) error {
|
||||||
@ -89,3 +88,23 @@ func helloWorld(app *fiber.App) {
|
|||||||
return c.JSON(response.NewResponse("Hello, World 👋!"))
|
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("错误信息示例"))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user