go-swagger 使用

1.  Download Swag for Go by using:

go get -u github.com/swaggo/swag/cmd/swag

2.  Add comments to your API source code, See Declarative Comments Format.

 例子 :

 

// @summary 商品搜索(全網、淘寶、京東、拼多多、唯品彙)
// @description 可全網比價,參數p 指定搜索區域
// @param q query string true "關鍵字"
// @Param p query string false "平臺來源"
// @accept json
// @produce  json
// @success 200 {string} string	"ok"
// @failure 400  "We need param [q]!"
// @failure 404 "API not found"
// @router /g/s [get]

3. Download gin-swagger by using:

go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

4. router.go 中添加路由

ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"




router.GET("/docs/*any", ginSwagger.DisablingWrapHandler(swaggerFiles.Handler, "SwaggerDisabled")) //從環境變量中獲取,來是否啓動路由

配置  /docs/*any 表示 運行主頁  http://localhost:8018/docs/index.html  就可以打開了

5. main函數中添加信息

// @title CPS api
// @version 0.1.0
// @description CPS 項目
// @host localhost:8018
// @contact.url [email protected]
// @contact.name cps team
// @BasePath /api/v1
func main() {

    注意: @host 參數 在swagger測試頁面請求的地址,

       @BasePath  也要配置正確

6. 在項目目錄中有main.go之下 執行,生成swagger文件

swag init

 7. 測試了

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章