訂閱專欄
swagger 是一個golang版本的swagger文檔生成器,提供了native code包裝器,并且支持主流的web框架包裹器
github 地址:https://github.com/swaggo/gin-swagger
下載安裝 swag
$ go get -u github.com/swaggo/swag/cmd/swag
1
在Go項目根文件夾中運行Swag
在main.go所在目錄執(zhí)行 swag init, -g 參數(shù)是輸出詳細(xì)信息
執(zhí)行后,會生成docs/doc.go以及docs/swagger.json,docs/swagger.yaml
$ swag init
1
下載gin-swagger
$ go get -u github.com/swaggo/gin-swagger
$ go get -u github.com/swaggo/files
1
2
然后在路由文件引入
import (
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
1
2
3
4
5
添加訪問文檔路由
// swage 文檔訪問路由\n
eng := gin.Default()
eng.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
1
2
3
注釋參數(shù)
注釋詳細(xì)格式參考:https://swaggo.github.io/swaggo.io/declarative_comments_format/general_api_info.html
主程序注釋(main.go)
// @title Golang Esign API
// @version 1.0
// @description? Golang api of demo
// @termsOfService http://github.com
// @contact.name API Support
// @contact.url http://www.cnblogs.com
// @contact.email ×××@qq.com
//@host 127.0.0.1:8081
func main() {
}
1
2
3
4
5
6
7
8
9
10
11
12
控制器注釋(congroller.go)
Get 參數(shù)方法
type GetOperationLogListResponse struct {
? ? List? *[]model.OperationLog `json:"list"`
? ? Total int? ? ? ? ? ? ? ? ? `json:"total"`
}
// @Title 應(yīng)用中心操作日志
// @Author mengyilingjian@outlook.com
// @Description 獲取應(yīng)用中心操作日志
// @Tags operationlog
// @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Param route formData string false "路由"
// @Param operator formData string false "操作者"
// @Param operation_type formData string false "操作類型 1 新增、2 刪除、3 更新"
// @Param description formData string false "操作描述"
// @Param start_time formData string false "開始時間"
// @Param end_time formData string false "結(jié)束時間"
// @Param page formData string true "頁數(shù)"
// @Param size formData string true "數(shù)據(jù)條數(shù)"
// @Success 200 {object} GetOperationLogListResponse
// @Router /api/v1/app/operationlog/appcenter [get]
func GetOperationLogList(c *gin.Context) {
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
以上生成文檔格式如下:
Post 參數(shù)方法
ReleaseTemplateAdd struct {
? ? Name? ? ? ? ? ? ? string `json:"name"`
? ? DeployEnv? ? ? ? ? string `json:"deploy_env"`
? ? GitlabType? ? ? ? int? ? `json:"gitlab_type"`
? ? GitlabBranchName? string `json:"gitlab_branch_name"`
? ? IsAutoRelease? ? ? int? ? `json:"is_auto_release"`
? ? Description? ? ? ? string `json:"description"`
? ? GitlabCITemplateID int32? `json:"gitlab_ci_template_id"`
? ? GitlabID? ? ? ? ? uint32 `json:"gitlab_id"`
}
// @Title 新增模版
// @Author mengyilingjian@outlook.com
// @Description 新增模版
// @Tags release template
// @Param Authorization header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Param body body ReleaseTemplateAdd true "JSON數(shù)據(jù)"
// @Success 200 {object} handler.ReportJSONResult
// @Router /api/v1/release/template/add [post]
func ReleaseTemplateAdd(c *gin.Context){
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
以上生成格式文檔如下:
————————————————
版權(quán)聲明:本文為CSDN博主「夢逸靈箭」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/weixin_42661321/article/details/108887918