Gin-swagger

订阅专栏

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所在目录执行 swag init, -g 参数是输出详细信息

执行后,会生成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

注释参数

注释详细格式参考: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 参数方法

type GetOperationLogListResponse struct {

    List  *[]model.OperationLog `json:"list"`

    Total int                  `json:"total"`

}

// @Title 应用中心操作日志

// @Author mengyilingjian@outlook.com

// @Description 获取应用中心操作日志

// @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 "结束时间"

// @Param page formData string true "页数"

// @Param size formData string true "数据条数"

// @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 参数方法

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数据"

// @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

以上生成格式文档如下:

————————————————

版权声明:本文为CSDN博主「梦逸灵箭」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/weixin_42661321/article/details/108887918

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容