Skip to content

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

License

Notifications You must be signed in to change notification settings

swaggo/fasthttp-swagger

Repository files navigation

fasthttp-swagger

fasthttp middleware to automatically generate RESTFUL API documentation with Swagger 2.0.

Build Status Codecov branch Go Report Card GoDoc Release

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 at your Go project root path(for instance ~/root/go-peoject-name), Swag will parse comments and generate required files(docs folder and docs/doc.go) at ~/root/go-peoject-name/docs.
swag init
  1. Download fasthttp-swagger by using:
go get -u github.com/swaggo/fasthttp-swagger
go get -u github.com/swaggo/files

Import following in your code:

import "github.com/swaggo/fasthttp-swagger" // fasthttp-swagger middleware

Canonical example:

Now assume you have implemented a simple api as following:

// A get function which returns a hello world string by json
func Helloworld(ctx *fasthttp.RequestCtx)  {
    ctx.SetStatusCode(http.StatusOK)
    ctx.Write([]byte("helloworld"))
}

So how to use fasthttp-swagger on api above? Just follow the following guide.

  1. Add Comments for apis and main function with fasthttp-swagger rules like following:
// @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(ctx *fasthttp.RequestCtx)  {
    ctx.SetStatusCode(http.StatusOK)
    ctx.Write([]byte("helloworld"))
}
  1. Use swag init command to generate a docs, docs generated will be stored at
  2. import the docs like this: I assume your project named github.com/go-project-name/docs.
import (
   docs "github.com/go-project-name/docs"
)
  1. build your application and after that, go to http://localhost:8080/swagger/index.html ,you to see your Swagger UI.

  2. The full code and folder relatives here:

package main

import (
   _ "github.com/go-project-name/docs"
   fasthttpSwagger "github.com/swaggo/fasthttp-swagger"
)
// @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(ctx *fasthttp.RequestCtx)  {
   ctx.SetStatusCode(http.StatusOK)
   ctx.Write([]byte("helloworld"))
}

func main()  {
   fasthttp.ListenAndServe(":8080", func(ctx *fasthttp.RequestCtx) {
      path := string(ctx.RequestURI())
      switch {
      case strings.HasPrefix(path, "/swagger"):
         fastHttpSwagger.WrapHandler(fastHttpSwagger.InstanceName("swagger"))(ctx)
      case path == "/api/v1/example/helloworld":
         Helloworld(ctx)
      }
   })
}

Demo project tree, swag init is run at relative .

.
├── docs
│   ├── docs.go
│   ├── swagger.json
│   └── swagger.yaml
├── go.mod
├── go.sum
└── main.go

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages