echoSwagger

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: 8 Imported by: 540

README

echo-swagger

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

Build Status Codecov branch Go Report Card 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 -d github.com/swaggo/swag/cmd/swag

# 1.16 or newer
$ go install github.com/swaggo/swag/cmd/swag@latest
  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 echo-swagger by using:
$ go get -u github.com/swaggo/echo-swagger

And import following in your code:

import "github.com/swaggo/echo-swagger" // echo-swagger middleware
Canonical example:
package main

import (
	"github.com/labstack/echo/v4"
	"github.com/swaggo/echo-swagger"

	_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.
)

// @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 main() {
	e := echo.New()

	e.GET("/swagger/*", echoSwagger.WrapHandler)

	e.Logger.Fatal(e.Start(":1323"))
}

  1. Run it, and browser to http://localhost:1323/swagger/index.html, you can see Swagger 2.0 Api documents.

swagger_index.html

Note: If you are using Gzip middleware you should add the swagger endpoint to skipper

Example
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
		Skipper: func(c echo.Context) bool {
			if strings.Contains(c.Request().URL.Path, "swagger") {
				return true
			}
			return false
		},
	}))

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WrapHandler = EchoWrapHandler()

WrapHandler wraps swaggerFiles.Handler and returns echo.HandlerFunc

Functions

func DeepLinking added in v1.1.2

func DeepLinking(deepLinking bool) func(*Config)

DeepLinking true, false.

func DocExpansion added in v1.1.2

func DocExpansion(docExpansion string) func(*Config)

DocExpansion list, full, none.

func DomID added in v1.1.2

func DomID(domID string) func(*Config)

DomID #swagger-ui.

func EchoWrapHandler

func EchoWrapHandler(options ...func(*Config)) echo.HandlerFunc

EchoWrapHandler wraps `http.Handler` into `echo.HandlerFunc`.

func InstanceName added in v1.1.5

func InstanceName(instanceName string) func(*Config)

InstanceName specified swag instance name.

func OAuth added in v1.3.3

func OAuth(config *OAuthConfig) func(*Config)

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 SyntaxHighlight added in v1.3.5

func SyntaxHighlight(syntaxHighlight bool) func(*Config)

SyntaxHighlight true, false.

func URL

func URL(url string) func(*Config)

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

Types

type Config

type Config struct {
	// The url pointing to API definition (normally swagger.json or swagger.yaml). Default is `mockedSwag.json`.
	URLs                 []string
	DocExpansion         string
	DomID                string
	InstanceName         string
	DeepLinking          bool
	PersistAuthorization bool
	SyntaxHighlight      bool

	// The information for OAuth2 integration, if any.
	OAuth *OAuthConfig
}

Config stores echoSwagger configuration variables.

type OAuthConfig added in v1.3.3

type OAuthConfig struct {
	// The ID of the client sent to the OAuth2 IAM provider.
	ClientId string

	// The OAuth2 realm that the client should operate in. If not applicable, use empty string.
	Realm string

	// The name to display for the application in the authentication popup.
	AppName string
}

OAuthConfig stores configuration for Swagger UI OAuth2 integration. See https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/ for further details.

Directories

Path Synopsis
docs
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag
Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT This file was generated by swaggo/swag

Jump to

Keyboard shortcuts

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