buffaloSwagger

package module
v1.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2023 License: MIT Imports: 9 Imported by: 1

README

buffalo-swagger

Buffalo 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 github.com/swaggo/swag/cmd/swag
  1. The General API annotation lives in actions/app.go, run Swag in your Buffalo project root folder with the flag -g actions/app.go. Swag will parse comments and generate required files(docs folder and docs/doc.go).
$ swag init -g actions/app.go

4.Download buffalo-swagger by using:

$ go get -u github.com/swaggo/buffalo-swagger

And import following in your actions/app.go code, making sure to modify the last package name properly:

import(
    buffaloSwagger "github.com/swaggo/buffalo-swagger"
    "github.com/swaggo/buffalo-swagger/swaggerFiles"
    _ "github.com/<github_name>/<project_name>/docs"
)
Canonical example:

For a complete example take a look at the example directory Below you can find an extract from actions/app.go

package actions

import(
    buffaloSwagger "github.com/swaggo/buffalo-swagger"
    "github.com/swaggo/buffalo-swagger/swaggerFiles"
    _ "github.com/swaggo/buffalo-swagger/example/docs"
)

[...]
var app *buffalo.App

// @title Swagger Example API
// @version 1.0
// @description This is a sample server Petstore server.
// @termsOfService http://swagger.io/terms/

// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @contact.email support@swagger.io

// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @host petstore.swagger.io
// @BasePath /v2
func App() *buffalo.App {
    if app == nil {
        app = buffalo.New(buffalo.Options{
            Env:          ENV,
            SessionStore: sessions.Null{},
            PreWares: []buffalo.PreWare{
                    cors.Default().Handler,
            },
            SessionName: "_example_session",
        })
    app.GET("/", HomeHandler)
    app.GET("/swagger/{doc:.*}", buffaloSwagger.WrapHandler(swaggerFiles.Handler))

}

return app
  1. Run it, and browse to http://localhost:3000/swagger/index.html, you can see Swagger 2.0 Api documents.

swagger_index.html

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepLinking added in v1.3.0

func DeepLinking(deepLinking bool) func(*Config)

DeepLinking set the swagger deep linking configuration.

func DefaultModelsExpandDepth added in v1.3.0

func DefaultModelsExpandDepth(depth int) func(*Config)

DefaultModelsExpandDepth set the default expansion depth for models (set to -1 completely hide the models).

func DocExpansion added in v1.3.0

func DocExpansion(docExpansion string) func(*Config)

DocExpansion list, full, none.

func InstanceName added in v1.3.0

func InstanceName(name string) func(*Config)

InstanceName set the instance name that was used to generate the swagger documents Defaults to swag.Name ("swagger").

func PersistAuthorization added in v1.3.0

func PersistAuthorization(persistAuthorization bool) func(*Config)

PersistAuthorization Persist authorization information over browser close/refresh. Defaults to false.

func URL added in v1.3.0

func URL(url string) func(*Config)

URL presents the url pointing to API definition (normally swagger.json or swagger.yaml).

func WrapHandler

func WrapHandler(handler *webdav.Handler, options ...func(*Config)) buffalo.Handler

WrapHandler wraps `http.Handler` into `gin.HandlerFunc`.

Types

type Config added in v1.3.0

type Config struct {
	// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `doc.json`.
	URL                      string
	DocExpansion             string
	InstanceName             string
	Title                    string
	DefaultModelsExpandDepth int
	DeepLinking              bool
	PersistAuthorization     bool
}

Config stores ginSwagger configuration variables.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL