middlewarex

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 30, 2026 License: MIT Imports: 8 Imported by: 0

README

Middlewarex

CircleCI cover.run Go Report Card GoDoc License

Bunch of middlewares for Labstack Echo.

Requirements

  • Echo 4.x.x

Usage

CRUD

Used for creating RESTful API entrypoints according the given struct.

Versioning

This middleware must be set as a pre middleware.

Doing the following request:

X-Application-Version: vnd.github.v3
GET /toto

will be rewritten as:

GET /v3/toto

Bechmarks

[middlewarex]>> go test -bench .
goos: darwin
goarch: amd64
pkg: github.com/mdouchement/middlewarex
BenchmarkVersioningRW-8     	  300000	     50608 ns/op   // With header rewriting
BenchmarkVersioningVRwM-8   	 5000000	       362 ns/op   // Just versioned routes with the Versioning middleware present
BenchmarkVersioningVR-8     	10000000	       215 ns/op   // Just versioned routes without the Versioning middleware
PASS
ok  	github.com/mdouchement/middlewarex	19.850s

If the header is not specified, no rewrittes are applied.

License

MIT

Contributing

All PRs are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

As possible, run the following commands to format and lint the code:

# Format
find . -name '*.go' -not -path './vendor*' -exec gofmt -s -w {} \;

# Lint
golangci-lint run -c .golangci.yml

Documentation

Index

Constants

View Source
const (
	// XApplicationVersion is the header for the asked API version (e.g. vnd.github.v1)
	XApplicationVersion = "X-Application-Version"
	// XApplicationStableVersion is the header for the stable API version (e.g. vnd.github.v3)
	XApplicationStableVersion = "X-Application-Stable-Version"
)

Variables

View Source
var (
	ErrPASETOMissing     = echo.NewHTTPError(http.StatusBadRequest, "missing or malformed paseto")
	ErrPASETOUnsupported = echo.NewHTTPError(http.StatusBadRequest, "unsupported paseto version/purpose")
)

Errors

View Source
var DefaultPASETOConfig = PASETOConfig{
	Skipper:     middleware.DefaultSkipper,
	ContextKey:  "paseto",
	TokenLookup: "header:" + echo.HeaderAuthorization,
	AuthScheme:  "Bearer",
	Validators:  []paseto.Validator{},
}

DefaultPASETOConfig is the default PASETO auth middleware config.

Functions

func CRUD

func CRUD(group *echo.Group, path string, resource interface{})

CRUD defines the folowwing resources:

POST:   /path
GET:    /path
GET:    /path/:id
PATCH:  /path/:id
DEL:    /path/:id

func PASETO added in v0.3.0

func PASETO(key []byte) echo.MiddlewareFunc

PASETO returns a JSON Platform-Agnostic SEcurity TOkens (PASETO) auth middleware.

For valid token, it sets the user in context and calls next handler. For invalid token, it returns "401 - Unauthorized" error. For missing token, it returns "400 - Bad Request" error.

func PASETOWithConfig added in v0.3.0

func PASETOWithConfig(config PASETOConfig) echo.MiddlewareFunc

PASETOWithConfig returns a PASETO auth middleware with config.

func Versioning

func Versioning(stable string, supported ...string) echo.MiddlewareFunc

Versioning rewrites routes to match the last part of the version header. e.g. `X-Application-Version: vnd.github.v3' header will prefix the request's path by `/v3'. The stable API version will be returned in the response's headers.

Types

type CreateSupported

type CreateSupported interface {
	Create(*echo.Context) error
}

CreateSupported interface

type DeleteSupported

type DeleteSupported interface {
	Delete(*echo.Context) error
}

DeleteSupported interface

type ListSupported

type ListSupported interface {
	List(*echo.Context) error
}

ListSupported interface

type PASETOConfig added in v0.3.0

type PASETOConfig struct {
	// Skipper defines a function to skip middleware.
	Skipper middleware.Skipper

	// BeforeFunc defines a function which is executed just before the middleware.
	BeforeFunc middleware.BeforeFunc

	// SuccessHandler defines a function which is executed for a valid token.
	SuccessHandler PASETOSuccessHandler

	// ErrorHandler defines a function which is executed for an invalid token.
	// It may be used to define a custom PASETO error.
	ErrorHandler PASETOErrorHandler

	// ErrorHandlerWithContext is almost identical to ErrorHandler, but it's passed the current context.
	ErrorHandlerWithContext PASETOErrorHandlerWithContext

	// Signing key to validate token.
	// Required.
	SigningKey []byte

	// Validators is the list of custom validators.
	// Time validation is enforced.
	Validators []paseto.Validator

	// Context key to store user information from the token into context.
	// Optional. Default value "user".
	ContextKey string

	// TokenLookup is a string in the form of "<source>:<name>" that is used
	// to extract token from the request.
	// Optional. Default value "header:Authorization".
	// Possible values:
	// - "header:<name>"
	// - "query:<name>"
	// - "param:<name>"
	// - "cookie:<name>"
	TokenLookup string

	// AuthScheme to be used in the Authorization header.
	// Optional. Default value "Bearer".
	AuthScheme string
}

PASETOConfig defines the config for PASETO middleware.

type PASETOErrorHandler added in v0.3.0

type PASETOErrorHandler func(error) error

PASETOErrorHandler defines a function which is executed for an invalid token.

type PASETOErrorHandlerWithContext added in v0.3.0

type PASETOErrorHandlerWithContext func(error, *echo.Context) error

PASETOErrorHandlerWithContext is almost identical to PASETOErrorHandler, but it's passed the current context.

type PASETOSuccessHandler added in v0.3.0

type PASETOSuccessHandler func(*echo.Context)

PASETOSuccessHandler defines a function which is executed for a valid token.

type ShowSupported

type ShowSupported interface {
	Show(*echo.Context) error
}

ShowSupported interface

type Token added in v0.3.0

type Token struct {
	paseto.JSONToken
	Footer string
}

Token represents a PASETO JSONToken with its footer.

type UpdateSupported

type UpdateSupported interface {
	Update(*echo.Context) error
}

UpdateSupported interface

Directories

Path Synopsis
examples
crud command
versioning command

Jump to

Keyboard shortcuts

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