自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Golang項目自動生成swagger格式接口文檔方法(一)

開發(fā) 開發(fā)工具
Swag是一款可以將Go的注釋轉(zhuǎn)換為Swagger2.0格式文檔的工具,生成接口文檔用到的注釋需要按照swag要求的格式書寫。

swag工具介紹和安裝

Swag是一款可以將Go的注釋轉(zhuǎn)換為Swagger2.0格式文檔的工具,生成接口文檔用到的注釋需要按照swag要求的格式書寫。

使用go install方式下載安裝swag

$ go install github.com/swaggo/swag/cmd/swag@latest

也可以從github的release頁面下載編譯好的二進制文件,以1.8.10版本為例:

$ wget https://github.com/swaggo/swag/releases/download/v1.8.10/swag_1.8.10_Linux_x86_64.tar.gz
$ tar zxvf swag_1.8.10_Linux_x86_64.tar.gz
$ chmod +x ./swag
$ mv swag /usr/local/bin

在包含main.go文件的項目根目錄運行swag init將會解析注釋并生成文件(docs文件夾),修改對應(yīng)注釋后再次運行swag ini即可:

swag init

使用swag fmt可以格式化SWAG注釋:

swag fmt

符合swag要求的API通用注釋寫法

在main.go的main方法添加API通用注釋

// @title         idcenter API
// @version 1.0
func main() {
//...
}

更多通用注釋字段說明和示例:

注釋

說明

示例

title

必填 應(yīng)用程序的名稱。

// @title Swagger Example API

version

必填 提供應(yīng)用程序API的版本。

// @version 1.0

description

應(yīng)用程序的簡短描述。

// @description This is a sample server celler server.

tag.name

標(biāo)簽的名稱。

// @tag.name This is the name of the tag

tag.description

標(biāo)簽的描述。

// @tag.description Cool Description

tag.docs.url

標(biāo)簽的外部文檔的URL。

// @tag.docs.url ??https://example.com??

tag.docs.description

標(biāo)簽的外部文檔說明。

// @tag.docs.description Best example documentation

termsOfService

API的服務(wù)條款。

// @termsOfService ??http://swagger.io/terms/??

contact.name

公開的API的聯(lián)系信息。

// @contact.name API Support

contact.url

聯(lián)系信息的URL。 必須采用網(wǎng)址格式。

// @contact.url ??http://www.swagger.io/support??

contact.email

聯(lián)系人/組織的電子郵件地址。 必須采用電子郵件地址的格式。

// @contact.email support@swagger.io

license.name

必填 用于API的許可證名稱。

// @license.name Apache 2.0

license.url

用于API的許可證的URL。 必須采用網(wǎng)址格式。

// @license.url ??http://www.apache.org/licenses/LICENSE-2.0.html??

host

運行API的主機(主機名或IP地址)。

// @host localhost:8080

BasePath

運行API的基本路徑。

// @BasePath /api/v1

accept

API 可以使用的 MIME 類型列表。 請注意,Accept 僅影響具有請求正文的操作,例如 POST、PUT 和 PATCH。 值必須如“Mime類型”中所述。

// @accept json

produce

API可以生成的MIME類型的列表。值必須如“Mime類型”中所述。

// @produce json

query.collection.format

請求URI query里數(shù)組參數(shù)的默認(rèn)格式:csv,multi,pipes,tsv,ssv。 如果未設(shè)置,則默認(rèn)為csv。

// @query.collection.format multi

schemes

用空格分隔的請求的傳輸協(xié)議。

// @schemes http https

externalDocs.description

Description of the external document.

// @externalDocs.description OpenAPI

externalDocs.url

URL of the external document.

// @externalDocs.url ??https://swagger.io/resources/open-api/??

x-name

擴展的鍵必須以x-開頭,并且只能使用json值

// @x-example-key {"key": "value"}

符合swag要求的具體API注釋

在接口的handler方法中添加具體的API注釋。

//  Login godoc
// @Summary 登錄
// @Schemes https
// @Description 登錄
// @Tags account
// @accept json
// @Produce json
// @Param account body param.LoginReq true "login"
// @Success 200 {object} param.JSONResult{data=param.LoginRes}
// @Router /user/login [post]
func Login(g *gin.Context) {
//...
}

參數(shù)注釋和返回注釋支持結(jié)構(gòu)體,本例中用到的結(jié)構(gòu)體在param包下面,內(nèi)容如下:

// JSONResult response body結(jié)構(gòu)
type JSONResult struct {
Code int `json:"code" binding:"required"`
Data interface{} `json:"data" binding:"required"`
Msg string `json:"msg" binding:"required"`
}

// LoginReq 登錄接口入?yún)?/span>
type LoginReq struct {
Email string `json:"email" binding:"required,min=6,max=50"` // 郵箱
Password string `json:"Password" binding:"required,min=8,max=15"` // 密碼
}

// LoginRes 登錄接口返回參數(shù)
type LoginRes struct {
Token json:"token"` // token
}

更多注釋字段說明:

注釋

描述

description

操作行為的詳細(xì)說明。

description.markdown

應(yīng)用程序的簡短描述。該描述將從名為endpointname.md的文件中讀取。

id

用于標(biāo)識操作的唯一字符串。在所有API操作中必須唯一。

tags

每個API操作的標(biāo)簽列表,以逗號分隔。

summary

該操作的簡短摘要。

accept

API 可以使用的 MIME 類型列表。 請注意,Accept 僅影響具有請求正文的操作,例如 POST、PUT 和 PATCH。 值必須如“Mime類型”中所述。

produce

API可以生成的MIME類型的列表。值必須如“Mime類型”中所述。

param

用空格分隔的參數(shù)。param name,param type,data type,is mandatory?,comment attribute(optional)

security

每個API操作的安全性。

success

以空格分隔的成功響應(yīng)。return code,{param type},data type,comment

failure

以空格分隔的故障響應(yīng)。return code,{param type},data type,comment

response

與success、failure作用相同

header

以空格分隔的頭字段。 return code,{param type},data type,comment

router

以空格分隔的路徑定義。 path,[httpMethod]

x-name

擴展字段必須以x-開頭,并且只能使用json值。

生成接口文檔

按照swag要求寫好注釋后,執(zhí)行如下命令生成文檔。

swag init

會在根目錄生成docs文件夾,里面包含swagger.json,、swagger.yaml和doc.go三個文件。

責(zé)任編輯:姜華 來源: 今日頭條
相關(guān)推薦

2023-03-08 08:48:50

Swag工具

2023-09-21 10:44:41

Web服務(wù)Swagger前端

2017-07-20 17:05:04

JavaScriptswagger-decSwagger

2024-09-10 08:15:33

Asp項目API

2017-06-20 15:39:58

Koa2 應(yīng)用動態(tài)Swagger文檔

2023-08-09 08:37:44

2020-12-07 06:05:34

apidocyapiknife4j

2020-08-06 11:45:37

數(shù)據(jù)庫文檔Swagger

2023-03-20 08:24:31

工具GoReleaser

2021-04-16 07:31:50

工具Postman接口

2022-02-16 08:21:11

JavaSwagger工具

2023-08-02 09:07:27

Golangio 包

2009-12-17 09:31:02

Ruby on Rai

2022-06-01 09:51:51

Golang方法接收者

2024-11-11 10:09:23

2009-06-29 17:03:41

自動生成Getter和Eclipse

2009-07-23 13:35:33

Ruby on Rai

2023-03-15 08:42:06

form表單設(shè)計接口

2017-08-10 16:14:07

FeignRPC模式

2024-01-01 13:27:44

pydoc工具代碼
點贊
收藏

51CTO技術(shù)棧公眾號