Simple Swagger Implementation for Fiber
This example use fiber-swagger and swaggo (Go converter to Swagger Documentation 2.0)
-
Download swaggo by using:
$ go get -u github.com/swaggo/swag/cmd/swag # 1.16 or newer $ go install github.com/swaggo/swag/cmd/swag@latest
-
Add comments to your API source code (example:
handlers/book.go
) or more details: swaggo documentation. -
Download fiber-swagger by using:
$ go get -u github.com/arsmn/fiber-swagger/v2
-
Import to your routes file (example:
routes/routes.go
):import swagger "github.com/arsmn/fiber-swagger/v2"
-
Add swagger handler to routes as a middleware (example:
routes/routes.go
):app.Get("/docs/*", swagger.Handler) // default app.Get("/docs/*", swagger.New(swagger.Config{ // custom URL: "http://example.com/doc.json", DeepLinking: false, }))
-
Run the swaggo command in your Go project root directory which contain
main.go
file. This command will generate required files (docs
anddocs/doc.go
)$ swag init
-
Import docs directory that generated by Swag CLI to your
main.go
file:import _ "swagger/docs"
-
Horray! You're ready to Go 🚀