Go to file
Xieyuschen 92cfa4c8ef
Edit readme to makes it more friendly to newer (#132)
* edit readme to makes it more friendly to newer

* Fix for reviewing

* Fix comment format

Co-authored-by: Xieyuschen <Xieyuschen@users.noreply.github.com>
2021-09-25 00:25:27 +03:00
.github/workflows Update ci.yml (#161) 2021-09-23 23:07:29 +03:00
example GitHub actions workflow (#159) 2021-09-23 22:20:10 +03:00
swaggerFiles feat: updated swagger ui to 3.5.0. (#4) 2017-11-27 12:35:18 +08:00
.gitignore feat: support for gzip middleware (#53) 2019-04-03 10:59:18 +08:00
LICENSE Create LICENSE 2017-10-15 21:52:07 -05:00
README.md Edit readme to makes it more friendly to newer (#132) 2021-09-25 00:25:27 +03:00
b0x.yml Update readme and b0x.yml 2017-06-25 17:49:42 +08:00
go.mod GitHub actions workflow (#159) 2021-09-23 22:20:10 +03:00
go.sum GitHub actions workflow (#159) 2021-09-23 22:20:10 +03:00
swagger.go Solve the integration swagger into the gin framework, access the Chinese garbled problem in the json returned by swagger/doc.json. (#78) 2021-08-11 23:26:37 +03:00
swagger_test.go Solve the integration swagger into the gin framework, access the Chinese garbled problem in the json returned by swagger/doc.json. (#78) 2021-08-11 23:26:37 +03:00

README.md

gin-swagger

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

Build Status Codecov branch Go Report Card GoDoc

Usage

Start using it

  1. Add comments to your API source code, See Declarative Comments Format.
  2. Download Swag for Go by using:
go get -u github.com/swaggo/swag/cmd/swag
  1. Run the Swag at your Go project root path(for instance ~/root/go-peoject-name), Swag will parse comments and generate required files(docs folder and docs/doc.go) at ~/root/go-peoject-name/docs.
swag init
  1. Download gin-swagger by using:
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files

Import following in your code:

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

Canonical example:

Now assume you have implemented a simple api as following:

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

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:
// @BasePath /api/v1

// PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context)  {
	g.JSON(http.StatusOK,"helloworld")
}
  1. Use swag init command to generate a docs, docs generated will be stored at
  2. import the docs like this: I assume your project named github.com/go-project-name/docs.
import (
   docs "github.com/go-project-name/docs"
)
  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:

package main

import (
   "github.com/gin-gonic/gin"
   docs "github.com/go-project-name/docs"
   swaggerfiles "github.com/swaggo/files"
   ginSwagger "github.com/swaggo/gin-swagger"
   "net/http"
)
// @BasePath /api/v1

// PingExample godoc
// @Summary ping example
// @Schemes
// @Description do ping
// @Tags example
// @Accept json
// @Produce json
// @Success 200 {string} Helloworld
// @Router /example/helloworld [get]
func Helloworld(g *gin.Context)  {
   g.JSON(http.StatusOK,"helloworld")
}

func main()  {
   r := gin.Default()
   docs.SwaggerInfo.BasePath = "/api/v1"
   v1 := r.Group("/api/v1")
   {
      eg := v1.Group("/example")
      {
         eg.GET("/helloworld",Helloworld)
      }
   }
   r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
   r.Run(":8080")

}

Demo project tree, swag init is run at relative .

.
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
└── main.go