gin-swagger生成API文档

github地址:https://github.com/swaggo/gin-swagger

先下载cmd包,才能执行相关命令

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

我已开始没成功,后来进入$GOPATH/bin/ 目录执行go get github.com/swaggo/swag/cmd/swag ,在bin目录下生成一个swag.exe文件,把$GOPATH/bin/ 添加到Path环境变量才算成功

示例:

package main

import (
    _ "apiwendang/docs"
    "github.com/gin-gonic/gin"
    swaggerFiles "github.com/swaggo/files"
    ginSwagger "github.com/swaggo/gin-swagger"

    //_ "github.com/swaggo/gin-swagger/example/basic/docs" // docs is generated by Swag CLI, you have to import it.
)

// @title AAA
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

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

// @host 127.0.0.1
// @BasePath
func main() {
    r := gin.New()

    //url := ginSwagger.URL("http://localhost:8080/swagger/doc.json") // The url pointing to API definition
    //r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    r.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
    r.GET("/", func(context *gin.Context) {
        context.JSON(200, gin.H{
            "msg": "successed",
        })
    })

    r.Run(":9009")
}

初始化命令,生成一个docs文件夹,内含三个文件

  • docs/docs.go
  • swagger.json
  • swagger.yaml

====

待续

猜你喜欢

转载自www.cnblogs.com/zhzhlong/p/11800787.html