middleware

package
v0.50.0 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package middleware defines a http handler middleware.

Index

Constants

This section is empty.

Variables

View Source
var DefaultMiddlewares = Middlewares{
	New("requestid", 10, requestid.RequestId),
	New("context", 20, context.Context),
	New("logger", 30, logger.Logger),
	New("recover", 40, recover.Recover),
}

DefaultMiddlewares is a set of the default middlewares.

Functions

func GetPriority added in v0.49.0

func GetPriority(m Middleware) int

GetPriority returns the priority of the middleware if it has implemented

interface{ Priority() int }

Or, return 0 instead.

NOTICE: the smaller the value, the high the priority.

func Sort added in v0.43.0

func Sort(ms []Middleware)

Sort sorts a set of middlewares by the priority from high to low.

Types

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager is used to manage a set of the middlewares.

func NewManager

func NewManager(handler http.Handler) *Manager

NewManager returns a new middleware manager.

func (*Manager) Append added in v0.42.0

func (m *Manager) Append(ms ...Middleware)

Append appends the new middlewares.

func (*Manager) AppendFunc added in v0.43.0

func (m *Manager) AppendFunc(ms ...MiddlewareFunc)

AppendFunc appends the new function middlewares.

func (*Manager) Handler added in v0.42.0

func (m *Manager) Handler(handler http.Handler) http.Handler

Handler wraps the http handler with the added middlewares, and return a new http handler.

func (*Manager) Insert added in v0.43.0

func (m *Manager) Insert(ms ...Middleware)

Insert inserts the new middlewares to the front.

func (*Manager) InsertFunc added in v0.43.0

func (m *Manager) InsertFunc(ms ...MiddlewareFunc)

InsertFunc inserts the new function middlewares to the front.

func (*Manager) Middlewares added in v0.42.0

func (m *Manager) Middlewares() Middlewares

Middlewares returns the added middlewares.

func (*Manager) Reset

func (m *Manager) Reset(ms ...Middleware)

Reset resets the middlewares to ms.

func (*Manager) ServeHTTP

func (m *Manager) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the interface http.Handler.

func (*Manager) SetHandler

func (m *Manager) SetHandler(handler http.Handler)

SetHandler resets the http handler.

type Middleware

type Middleware interface {
	Handler(next http.Handler) http.Handler
}

Middleware is a http handler middleware.

func New added in v0.42.0

func New(name string, priority int, mfunc MiddlewareFunc) Middleware

New returns a new named priority middleware, which has the methods as follows:

Name() string
Priority() int

For the priority, the smaller the value, the higher the priority.

type MiddlewareFunc added in v0.42.0

type MiddlewareFunc func(next http.Handler) http.Handler

MiddlewareFunc is the middleware function.

func (MiddlewareFunc) Handler added in v0.42.0

func (f MiddlewareFunc) Handler(next http.Handler) http.Handler

Handler implements the interface Middleware.

type Middlewares

type Middlewares []Middleware

Middlewares is a set of middlewares.

func (Middlewares) Append added in v0.42.0

func (ms Middlewares) Append(m ...Middleware) Middlewares

Append appends a set of middlewares and return a new middleware slice.

func (Middlewares) AppendFunc added in v0.43.0

func (ms Middlewares) AppendFunc(m ...MiddlewareFunc) Middlewares

AppendFunc appends a set of function middlewares and return a new middleware slice.

func (Middlewares) Clone added in v0.4.0

func (ms Middlewares) Clone() Middlewares

Clone clones itself to a new middlewares.

func (Middlewares) Handler

func (ms Middlewares) Handler(next http.Handler) http.Handler

Handler implements the interface Middleware.

func (Middlewares) Insert added in v0.43.0

func (ms Middlewares) Insert(m ...Middleware) Middlewares

Insert inserts a set of middlewares into the front and return a new middleware slice.

func (Middlewares) InsertFunc added in v0.43.0

func (ms Middlewares) InsertFunc(m ...MiddlewareFunc) Middlewares

InsertFunc inserts a set of function middlewares into the front and return a new middleware slice.

func (Middlewares) Sort added in v0.49.0

func (ms Middlewares) Sort()

Sort sorts itself by the priority from high to low.

Directories

Path Synopsis
Package context provides a context middleware to set the Context into the request.
Package context provides a context middleware to set the Context into the request.
Package cors provides a CORS middleware.
Package cors provides a CORS middleware.
Package logger provides a logger middleware to log the http request.
Package logger provides a logger middleware to log the http request.
Package path provides a middleware to respond to the given path.
Package path provides a middleware to respond to the given path.
Package recover provides a recover middleware to wrap and log the panic.
Package recover provides a recover middleware to wrap and log the panic.
Package requestid provides a request id middleware based on the request header "X-Request-Id".
Package requestid provides a request id middleware based on the request header "X-Request-Id".

Jump to

Keyboard shortcuts

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