swagger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2023 License: MIT Imports: 5 Imported by: 3

README

swagger

License Coverage GitHub Workflow Status Go Report Card Go PKG

This is for swaggo extension library to set extra variables without touching the generated file.

go get github.com/worldline-go/swagger

Usage

Create a file in the /docs/ folder, add to that folder to not include again generated file of swaggo.

Example /docs/info.go:

package docs

import (
	"github.com/worldline-go/auth"
	"github.com/worldline-go/swagger"
)

func Info(version string, provider auth.InfProvider) error {
	return swagger.SetInfo(
		swagger.WithVersion(version),
		swagger.WithCustom(map[string]interface{}{
			"tokenUrl": provider.GetTokenURLExternal(),
			"authUrl":  provider.GetAuthURLExternal(),
		}),
	)
}

OAuth2Application for service authentication.
OAuth2Implicit for user authentication but need to set implicit flow!
OAuth2Password for user authentication with directly with username and password.
OAuth2AccessCode for user authentication with code, it will redirect to the authUrl to login.

Inside of the general information of server:

// Echo Server
//
// @title Auth Test API
// @description This is a sample server for out Auth library.
//
// @contant.name worldline-go
// @contant.url https://github.com/worldline-go
//
// @securityDefinitions.apikey	ApiKeyAuth
// @in							header
// @name						Authorization
// @description				Description for what is this security definition being used
//
// @securitydefinitions.oauth2.accessCode	OAuth2AccessCode
// @tokenUrl								[[ .Custom.tokenUrl ]]
// @authorizationUrl						[[ .Custom.authUrl ]]
func echoServer(ctx context.Context) error {
    // ...
}

Use OAuth2 tags in the requests

// PostValue return the body
//
// @Summary Post value
// @Description Post value
// @Tags restricted
// @Accept  json
// @Produce  json
// @Success 200 {object} map[string]interface{}
// @Failure 403 {object} map[string]interface{}
// @Failure 404 {object} map[string]interface{}
// @Router /value [post]
// @Security ApiKeyAuth || OAuth2AccessCode
func (API) PostValue(c echo.Context) error {
    // ...
}

All definations

// @securitydefinitions.oauth2.application	OAuth2Application
// @tokenUrl								[[ .Custom.tokenUrl ]]
// @securitydefinitions.oauth2.implicit	OAuth2Implicit
// @authorizationUrl						[[ .Custom.authUrl ]]
// @securitydefinitions.oauth2.password	OAuth2Password
// @tokenUrl								[[ .Custom.tokenUrl ]]
// @securitydefinitions.oauth2.accessCode	OAuth2AccessCode
// @tokenUrl								[[ .Custom.tokenUrl ]]
// @authorizationUrl						[[ .Custom.authUrl ]]

Register Echo

e.GET("/swagger/*", echoSwagger.EchoWrapHandler(func(c *echoSwagger.Config) {
	c.OAuth = &echoSwagger.OAuthConfig{
		ClientId: provider.GetClientIDExternal(),
	}
}))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetInfo

func SetInfo(opts ...Option) error

Types

type Option

type Option func(*options)

func WithBasePath

func WithBasePath(basePath string) Option

WithBasePath sets the base path of the API.

func WithCustom

func WithCustom(custom map[string]interface{}) Option

WithCustom sets the custom data to be used in the swagger template.

Access custom data in the template with {{.Custom.key}}.

func WithDelims

func WithDelims(delims string) Option

WithDelims sets the delimiters to be used in the SetInfo function.

The default delimiters are "[[ ]]".

func WithDescription

func WithDescription(description string) Option

WithDescription sets the description of the API.

func WithHost

func WithHost(host string) Option

WithHost sets the host of the API.

func WithInfoInstanceName

func WithInfoInstanceName(infoInstanceName string) Option

WithInfoInstanceName sets the name of the swagger instance to be used.

The default name is "swagger".

func WithSchemes

func WithSchemes(schemes ...string) Option

WithSchemes sets the schemes of the API.

func WithTitle

func WithTitle(title string) Option

WithTitle sets the title of the API.

func WithVersion

func WithVersion(version string) Option

WithVersion sets the version of the API.

Jump to

Keyboard shortcuts

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