/fiber-swagger

fiber middleware to automatically generate RESTful API documentation with Swagger 2.0.

Primary LanguageGo

fiber-swagger

fiber middleware to automatically generate RESTful API documentation with Swagger 2.0.

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 in your Go project root folder which contains main.go file, Swag will parse comments and generate required files(docs folder and docs/doc.go).
$ swag init
  1. Download fiber-swagger by using:
$ go get -u github.com/arsmn/fiber-swagger/v2

And import following in your code:

import "github.com/arsmn/fiber-swagger/v2" // fiber-swagger middleware

Canonical example:

package main

import (
        "github.com/gofiber/fiber/v2"
	"github.com/arsmn/fiber-swagger/v2"
	_ "github.com/arsmn/fiber-swagger/v2/example/docs" // docs is generated by Swag CLI, you have to import it.
)

// @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.Handler) // default

	app.Get("/swagger/*", swagger.New(swagger.Config{ // custom
		URL: "http://example.com/doc.json",
		DeepLinking: false,
	}))

	app.Listen(":8080")
}
  1. Run it, and browser to http://localhost:8080/swagger, you can see Swagger 2.0 Api documents.