fiber middleware to automatically generate RESTful API documentation with Swagger
- Add comments to your API source code, See Declarative Comments Format.
- Download Swag for Go by using:
go get -u github.com/swaggo/swag/cmd/swag
# 1.16 or newer
go install github.com/swaggo/swag/cmd/swag@latest
- Run the Swag in your Go project root folder which contains
main.go
file, Swag will parse comments and generate required files(docs
folder anddocs/doc.go
).
swag init
- Download swagger by using:
go get -u github.com/gofiber/swagger
And import following in your code:
import "github.com/gofiber/swagger" // swagger handler
package main
import (
"github.com/gofiber/swagger"
"github.com/gofiber/fiber/v2"
// docs are generated by Swag CLI, you have to import them.
// replace with your own docs folder, usually "github.com/username/reponame/docs"
_ "github.com/gofiber/swagger/example/docs"
)
// @title Fiber Example API
// @version 1.0
// @description This is a sample swagger for Fiber
// @termsOfService http://swagger.io/terms/
// @contact.name API Support
// @contact.email fiber@swagger.io
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8080
// @BasePath /
func main() {
app := fiber.New()
app.Get("/swagger/*", swagger.HandlerDefault) // default
app.Get("/swagger/*", swagger.New(swagger.Config{ // custom
URL: "http://example.com/doc.json",
DeepLinking: false,
// Expand ("list") or Collapse ("none") tag groups by default
DocExpansion: "none",
// Prefill OAuth ClientId on Authorize popup
OAuth: &swagger.OAuthConfig{
AppName: "OAuth Provider",
ClientId: "21bb4edc-05a7-4afc-86f1-2e151e4ba6e2",
},
// Ability to change OAuth2 redirect uri location
OAuth2RedirectUrl: "http://localhost:8080/swagger/oauth2-redirect.html",
}))
app.Listen(":8080")
}
- Run it, and browser to http://localhost:8080/swagger, you can see Swagger 2.0 Api documents.