gfast 后端開(kāi)發(fā)-接口文檔

2022-03-29 16:37 更新

接口文檔使用?swagger?,只要你在寫代碼的時(shí)候,稍加幾個(gè)注解,文檔自動(dòng)生成。

使用方法

使用前提:先安裝?swag?

 ?go install github.com/swaggo/swag/cmd/swag? 會(huì)在?gopath?的?bin?目錄下生成?swag?可執(zhí)行文件,確保?bin?目錄已設(shè)置環(huán)境變量可全局執(zhí)行。

1、入口文件設(shè)置swagger信息

package main

import (
    _ "gfast/boot"
    _ "gfast/router"
    "github.com/gogf/gf/frame/g"
)

// @title gfast
// @version 2.0
// @description gfast 在線API文檔
// @host localhost:8200
// @BasePath /
func main() {

}

2、在控制層Controller的方法上添加注解來(lái)描述接口信息如:

// @Summary 信息列表
// @Description 信息列表
// @Tags 文章管理
// @Param data body cms_news.ReqListSearchParams true "data"
// @Success 0 {object} library.Response "{"code": 200, "data": [...]}"
// @Router /system/cms/news/list [get]
// @Security
func (c *CmsNews) List(r *ghttp.Request) {
    ...

3、生成更新api接口文檔

在 ?./? 項(xiàng)目根目錄下(和main.go文件同級(jí)目錄下)執(zhí)行:?gf swagger --pack? 生成或更新api文檔。

執(zhí)行后在項(xiàng)目根目錄會(huì)自動(dòng)生成swagger文件夾,其中保存了接口文檔數(shù)據(jù)?swagger.json?

m_8684dc7501a11186f984567d6fa83a17_r

swaggo 詳細(xì)文檔地址請(qǐng)查閱:https://github.com/swaggo/swag

goframe gf工具使用: https://goframe.org/pages/viewpage.action?pageId=1115794


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)