|
| 1 | +# fasthttp-swagger |
| 2 | + |
| 3 | +gin middleware to automatically generate RESTFUL API documentation with Swagger 2.0. |
| 4 | + |
| 5 | +[](https://github.com/features/actions) |
| 6 | +[](https://codecov.io/gh/swaggo/fasthttp-swagger) |
| 7 | +[](https://goreportcard.com/report/github.com/swaggo/fasthttp-swagger) |
| 8 | +[](https://godoc.org/github.com/swaggo/fasthttp-swagger) |
| 9 | +[](https://github.com/swaggo/fasthttp-swagger/releases) |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Start using it |
| 14 | + |
| 15 | +1. Add comments to your API source code, [See Declarative Comments Format](https://swaggo.github.io/swaggo.io/declarative_comments_format/). |
| 16 | +2. Download [Swag](https://github.com/swaggo/swag) for Go by using: |
| 17 | + |
| 18 | +```sh |
| 19 | +go get -u github.com/swaggo/swag/cmd/swag |
| 20 | +``` |
| 21 | + |
| 22 | +3. Run the [Swag](https://github.com/swaggo/swag) at your Go project root path(for instance `~/root/go-peoject-name`), |
| 23 | + [Swag](https://github.com/swaggo/swag) will parse comments and generate required files(`docs` folder and `docs/doc.go`) |
| 24 | + at `~/root/go-peoject-name/docs`. |
| 25 | + |
| 26 | +```sh |
| 27 | +swag init |
| 28 | +``` |
| 29 | + |
| 30 | +4. Download [fasthttp-swagger](https://github.com/swaggo/fasthttp-swagger) by using: |
| 31 | + |
| 32 | +```sh |
| 33 | +go get -u github.com/swaggo/fasthttp-swagger |
| 34 | +go get -u github.com/swaggo/files |
| 35 | +``` |
| 36 | + |
| 37 | +Import following in your code: |
| 38 | + |
| 39 | +```go |
| 40 | +import "github.com/swaggo/fasthttp-swagger" // fasthttp-swagger middleware |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +### Canonical example: |
| 45 | + |
| 46 | +Now assume you have implemented a simple api as following: |
| 47 | + |
| 48 | +```go |
| 49 | +// A get function which returns a hello world string by json |
| 50 | +func Helloworld(ctx *fasthttp.RequestCtx) { |
| 51 | + ctx.SetStatusCode(http.StatusOK) |
| 52 | + ctx.Write([]byte("helloworld")) |
| 53 | +} |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | +So how to use fasthttp-swagger on api above? Just follow the following guide. |
| 58 | + |
| 59 | +1. Add Comments for apis and main function with fasthttp-swagger rules like following: |
| 60 | + |
| 61 | +```go |
| 62 | +// @BasePath /api/v1 |
| 63 | + |
| 64 | +// PingExample godoc |
| 65 | +// @Summary ping example |
| 66 | +// @Schemes |
| 67 | +// @Description do ping |
| 68 | +// @Tags example |
| 69 | +// @Accept json |
| 70 | +// @Produce json |
| 71 | +// @Success 200 {string} Helloworld |
| 72 | +// @Router /example/helloworld [get] |
| 73 | +func Helloworld(ctx *fasthttp.RequestCtx) { |
| 74 | + ctx.SetStatusCode(http.StatusOK) |
| 75 | + ctx.Write([]byte("helloworld")) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +2. Use `swag init` command to generate a docs, docs generated will be stored at |
| 80 | +3. import the docs like this: |
| 81 | + I assume your project named `github.com/go-project-name/docs`. |
| 82 | + |
| 83 | +```go |
| 84 | +import ( |
| 85 | + docs "github.com/go-project-name/docs" |
| 86 | +) |
| 87 | +``` |
| 88 | + |
| 89 | +4. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI. |
| 90 | + |
| 91 | +5. The full code and folder relatives here: |
| 92 | + |
| 93 | +```go |
| 94 | +package main |
| 95 | + |
| 96 | +import ( |
| 97 | + _ "github.com/go-project-name/docs" |
| 98 | + fasthttpSwagger "github.com/swaggo/fasthttp-swagger" |
| 99 | +) |
| 100 | +// @BasePath /api/v1 |
| 101 | + |
| 102 | +// PingExample godoc |
| 103 | +// @Summary ping example |
| 104 | +// @Schemes |
| 105 | +// @Description do ping |
| 106 | +// @Tags example |
| 107 | +// @Accept json |
| 108 | +// @Produce json |
| 109 | +// @Success 200 {string} Helloworld |
| 110 | +// @Router /example/helloworld [get] |
| 111 | +func Helloworld(ctx *fasthttp.RequestCtx) { |
| 112 | + ctx.SetStatusCode(http.StatusOK) |
| 113 | + ctx.Write([]byte("helloworld")) |
| 114 | +} |
| 115 | + |
| 116 | +func main() { |
| 117 | + fasthttp.ListenAndServe(":8080", func(ctx *fasthttp.RequestCtx) { |
| 118 | + path := string(ctx.RequestURI()) |
| 119 | + switch { |
| 120 | + case strings.HasPrefix(path, "/swagger"): |
| 121 | + fastHttpSwagger.WrapHandler(fastHttpSwagger.InstanceName("swagger"))(ctx) |
| 122 | + case path == "/api/v1/example/helloworld": |
| 123 | + Helloworld(ctx) |
| 124 | + } |
| 125 | + }) |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Demo project tree, `swag init` is run at relative `.` |
| 130 | + |
| 131 | +``` |
| 132 | +. |
| 133 | +├── docs |
| 134 | +│ ├── docs.go |
| 135 | +│ ├── swagger.json |
| 136 | +│ └── swagger.yaml |
| 137 | +├── go.mod |
| 138 | +├── go.sum |
| 139 | +└── main.go |
| 140 | +``` |
0 commit comments