项目作者: swaggo

项目描述 :
gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
高级语言: Go
项目地址: git://github.com/swaggo/gin-swagger.git
创建时间: 2017-06-22T00:45:54Z
项目社区:https://github.com/swaggo/gin-swagger

开源协议:MIT License

下载


gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.

Build Status
Codecov branch
Go Report Card
GoDoc
Release

Usage

Start using it

  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
  1. go get -u github.com/swaggo/swag/cmd/swag

Starting in Go 1.17, installing executables with go get is deprecated. go install may be used instead:

  1. go install github.com/swaggo/swag/cmd/swag@latest
  1. Run the Swag at your Go project root path(for instance ~/root/go-project-name),
    Swag will parse comments and generate required files(docs folder and docs/doc.go)
    at ~/root/go-project-name/docs.
  1. swag init
  1. Download gin-swagger by using:
  1. go get -u github.com/swaggo/gin-swagger
  2. go get -u github.com/swaggo/files

Import following in your code:

  1. import "github.com/swaggo/gin-swagger" // gin-swagger middleware
  2. import "github.com/swaggo/files" // swagger embed files

Canonical example:

Now assume you have implemented a simple api as following:

  1. // A get function which returns a hello world string by json
  2. func Helloworld(g *gin.Context) {
  3. g.JSON(http.StatusOK,"helloworld")
  4. }

So how to use gin-swagger on api above? Just follow the following guide.

  1. Add Comments for apis and main function with gin-swagger rules like following:
  1. // @BasePath /api/v1
  2. // PingExample godoc
  3. // @Summary ping example
  4. // @Schemes
  5. // @Description do ping
  6. // @Tags example
  7. // @Accept json
  8. // @Produce json
  9. // @Success 200 {string} Helloworld
  10. // @Router /example/helloworld [get]
  11. func Helloworld(g *gin.Context) {
  12. g.JSON(http.StatusOK,"helloworld")
  13. }
  1. Use swag init command to generate a docs, docs generated will be stored at docs/.
  2. import the docs like this:
    I assume your project named github.com/go-project-name/docs.
  1. import (
  2. docs "github.com/go-project-name/docs"
  3. )
  1. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.

  2. The full code and folder relatives here:

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. docs "github.com/go-project-name/docs"
  5. swaggerfiles "github.com/swaggo/files"
  6. ginSwagger "github.com/swaggo/gin-swagger"
  7. "net/http"
  8. )
  9. // @BasePath /api/v1
  10. // PingExample godoc
  11. // @Summary ping example
  12. // @Schemes
  13. // @Description do ping
  14. // @Tags example
  15. // @Accept json
  16. // @Produce json
  17. // @Success 200 {string} Helloworld
  18. // @Router /example/helloworld [get]
  19. func Helloworld(g *gin.Context) {
  20. g.JSON(http.StatusOK,"helloworld")
  21. }
  22. func main() {
  23. r := gin.Default()
  24. docs.SwaggerInfo.BasePath = "/api/v1"
  25. v1 := r.Group("/api/v1")
  26. {
  27. eg := v1.Group("/example")
  28. {
  29. eg.GET("/helloworld",Helloworld)
  30. }
  31. }
  32. r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
  33. r.Run(":8080")
  34. }

Demo project tree, swag init is run at relative .

  1. .
  2. ├── docs
  3. ├── docs.go
  4. ├── swagger.json
  5. └── swagger.yaml
  6. ├── go.mod
  7. ├── go.sum
  8. └── main.go

Project with Nested Directory

  1. .
  2. ├── cmd
  3. └── ginsimple
  4. └── main.go
  5. ├── docs
  6. ├── docs.go
  7. ├── swagger.json
  8. └── swagger.yaml
  9. ├── go.mod
  10. ├── go.sum
  11. └── internal
  12. ├── handlers
  13. ├── helloWorld.go
  14. └── userHandler.go
  15. └── models
  16. ├── profile.go
  17. └── user.go

Inorder generate swagger docs for projects with nested directories run the following command

  1. swag init -g ./cmd/ginsimple/main.go -o cmd/docs

-o will set the auto generated file to the specified path

Multiple APIs

This feature was introduced in swag v1.7.9

Configuration

You can configure Swagger using different configuration options

  1. func main() {
  2. r := gin.New()
  3. ginSwagger.WrapHandler(swaggerfiles.Handler,
  4. ginSwagger.URL("http://localhost:8080/swagger/doc.json"),
  5. ginSwagger.DefaultModelsExpandDepth(-1))
  6. r.Run()
  7. }
Option Type Default Description
URL string “doc.json” URL pointing to API definition
DocExpansion string “list” Controls the default expansion setting for the operations and tags. It can be ‘list’ (expands only the tags), ‘full’ (expands the tags and operations) or ‘none’ (expands nothing).
DeepLinking bool true If set to true, enables deep linking for tags and operations. See the Deep Linking documentation for more information.
DefaultModelsExpandDepth int 1 Default expansion depth for models (set to -1 completely hide the models).
InstanceName string “swagger” The instance name of the swagger document. If multiple different swagger instances should be deployed on one gin router, ensure that each instance has a unique name (use the —instanceName parameter to generate swagger documents with swag init).
PersistAuthorization bool false If set to true, it persists authorization data and it would not be lost on browser close/refresh.
Oauth2DefaultClientID string “” If set, it’s used to prepopulate the client_id field of the OAuth2 Authorization dialog.
Oauth2UsePkce bool false If set to true, it enables Proof Key for Code Exchange to enhance security for OAuth public clients.