2017-06-22 08:39:12 +00:00
# gin-swagger
2017-06-25 09:49:42 +00:00
2021-09-24 21:25:27 +00:00
gin middleware to automatically generate RESTFUL API documentation with Swagger 2.0.
2017-06-25 09:49:42 +00:00
2021-09-23 19:20:10 +00:00
[](https://github.com/features/actions)
2017-07-06 16:57:54 +00:00
[](https://codecov.io/gh/swaggo/gin-swagger)
[](https://goreportcard.com/report/github.com/swaggo/gin-swagger)
2018-01-12 15:34:00 +00:00
[](https://godoc.org/github.com/swaggo/gin-swagger)
2021-09-25 12:24:06 +00:00
[](https://github.com/swaggo/gin-swagger/releases)
2017-06-25 09:49:42 +00:00
## Usage
### Start using it
2021-11-04 20:25:29 +00:00
2018-04-13 18:04:08 +00:00
1. Add comments to your API source code, [See Declarative Comments Format ](https://swaggo.github.io/swaggo.io/declarative_comments_format/ ).
2017-07-06 16:57:54 +00:00
2. Download [Swag ](https://github.com/swaggo/swag ) for Go by using:
2021-11-04 20:25:29 +00:00
2017-06-25 09:49:42 +00:00
```sh
2021-09-24 21:25:27 +00:00
go get -u github.com/swaggo/swag/cmd/swag
2017-06-25 09:49:42 +00:00
```
2021-09-24 21:25:27 +00:00
3. Run the [Swag ](https://github.com/swaggo/swag ) at your Go project root path(for instance `~/root/go-peoject-name` ),
[Swag ](https://github.com/swaggo/swag ) will parse comments and generate required files(`docs` folder and `docs/doc.go` )
at `~/root/go-peoject-name/docs` .
2021-11-04 20:25:29 +00:00
2017-06-25 09:49:42 +00:00
```sh
2021-09-24 21:25:27 +00:00
swag init
2017-06-25 09:49:42 +00:00
```
2021-11-04 20:25:29 +00:00
2019-06-13 07:24:37 +00:00
4. Download [gin-swagger ](https://github.com/swaggo/gin-swagger ) by using:
2021-11-04 20:25:29 +00:00
2017-12-19 10:24:52 +00:00
```sh
2021-09-24 21:25:27 +00:00
go get -u github.com/swaggo/gin-swagger
go get -u github.com/swaggo/files
2017-12-19 10:24:52 +00:00
```
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
Import following in your code:
2017-06-25 09:49:42 +00:00
```go
2017-07-06 16:57:54 +00:00
import "github.com/swaggo/gin-swagger" // gin-swagger middleware
2019-07-05 07:56:58 +00:00
import "github.com/swaggo/files" // swagger embed files
2017-06-25 09:49:42 +00:00
```
### Canonical example:
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
Now assume you have implemented a simple api as following:
2021-11-04 20:25:29 +00:00
2017-06-25 09:49:42 +00:00
```go
2021-11-04 20:25:29 +00:00
// A get function which returns a hello world string by json
2021-09-24 21:25:27 +00:00
func Helloworld(g *gin.Context) {
g.JSON(http.StatusOK,"helloworld")
}
2019-07-05 07:56:58 +00:00
2021-09-24 21:25:27 +00:00
```
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
So how to use gin-swagger on api above? Just follow the following guide.
2017-06-25 09:49:42 +00:00
2021-09-24 21:25:27 +00:00
1. Add Comments for apis and main function with gin-swagger rules like following:
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
```go
// @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")
2017-06-25 09:49:42 +00:00
}
2017-06-25 14:27:03 +00:00
```
2021-09-24 21:25:27 +00:00
2. Use `swag init` command to generate a docs, docs generated will be stored at
3. import the docs like this:
I assume your project named `github.com/go-project-name/docs` .
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
```go
import (
docs "github.com/go-project-name/docs"
)
```
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
4. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.
2018-09-26 02:28:12 +00:00
2021-09-24 21:25:27 +00:00
5. The full code and folder relatives here:
2021-11-04 20:25:29 +00:00
2018-09-26 02:28:12 +00:00
```go
package main
import (
2021-09-24 21:25:27 +00:00
"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"
2018-09-26 02:28:12 +00:00
)
2021-09-24 21:25:27 +00:00
// @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")
}
2018-09-26 02:28:12 +00:00
2021-09-24 21:25:27 +00:00
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")
2018-09-26 02:28:12 +00:00
}
```
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
Demo project tree, `swag init` is run at relative `.`
2021-11-04 20:25:29 +00:00
2021-09-24 21:25:27 +00:00
```
.
├── docs
│ ├── docs.go
│ ├── swagger.json
│ └── swagger.yaml
├── go.mod
├── go.sum
└── main.go
2021-09-24 22:45:34 +00:00
```
## Configuration
You can configure Swagger using different configuration options
```go
func main() {
r := gin.New()
2021-11-04 20:25:29 +00:00
ginSwagger.WrapHandler(swaggerFiles.Handler,
ginSwagger.URL("http://localhost:8080/swagger/doc.json"),
2021-11-02 20:10:35 +00:00
ginSwagger.DefaultModelsExpandDepth(-1))
2021-09-24 22:45:34 +00:00
r.Run()
}
```
2021-11-04 22:49:34 +00:00
| Option | Type | Default | Description |
| ------------------------ | ------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| URL | string | "doc.json" | URL pointing to API definition |
| DocExpantion | 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_ ). |