middlewares

package
v0.0.0-...-2a3cdc3 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2020 License: MIT, MIT Imports: 3 Imported by: 0

README

Middleware Stack

Manage Golang HTTP middlewares

Usage

func main() {
	Stack := &MiddlewareStack{}

	// Add middleware `auth` to stack
	Stack.Use(&middlewares.Middleware{
		Name: "auth",
		// Insert middleware `auth` after middleware `session` if it exists
		InsertAfter: []string{"session"},
		// Insert middleware `auth` before middleare `authorization` if it exists
		InsertBefore: []string{"authorization"},
	})

	// Remove middleware `cookie` from stack
	Stack.Remove("cookie")

	mux := http.NewServeMux()
	http.ListenAndServe(":9000", Stack.Apply(mux))
}

Default Middleware Stack

DefaultMiddlewareStack is an initialized middleware stack, It is defined like this:

var DefaultMiddlewareStack = &MiddlewareStack{}

There are some global methods could be used to manage its middlewares, e.g:

func main() {
	// Add middleware `auth` to default stack
	middlewares.Use(&middlewares.Middleware{
		Name: "auth",
		// Insert middleware `auth` after middleware `session` if it exists
		InsertAfter: []string{"session"},
		// Insert middleware `auth` before middleare `authorization` if it exists
		InsertBefore: []string{"authorization"},
	})

	// Remove middleware `cookie` from default stack
	middlewares.Remove("cookie")

	mux := http.NewServeMux()
	http.ListenAndServe(":9000", middlewares.Apply(mux))
}
QOR Integration

There are many QOR libraries that requires to regsiter middlewares, it will register its middleware to the DefaultMiddlewareStack, so if you don't want to manage those libraries's middlewares by yourself, you could just used the DefaultMiddlewareStack for your application.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultMiddlewareStack = &MiddlewareStack{}

DefaultMiddlewareStack default middleware stack

Functions

func Apply

func Apply(handler http.Handler) http.Handler

Apply apply DefaultMiddlewareStack's middlewares to handler

func Remove

func Remove(name string)

Remove remove middleware by name with DefaultMiddlewareStack

func Use

func Use(middleware Middleware)

Use use middleware with DefaultMiddlewareStack

Types

type Middleware

type Middleware struct {
	Name         string
	Handler      func(http.Handler) http.Handler
	InsertAfter  []string
	InsertBefore []string
	Requires     []string
}

Middleware middleware struct

type MiddlewareStack

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

MiddlewareStack middlewares stack

func (*MiddlewareStack) Apply

func (stack *MiddlewareStack) Apply(handler http.Handler) http.Handler

Apply apply middlewares to handler

func (*MiddlewareStack) Remove

func (stack *MiddlewareStack) Remove(name string)

Remove remove middleware by name

func (*MiddlewareStack) String

func (stack *MiddlewareStack) String() string

func (*MiddlewareStack) Use

func (stack *MiddlewareStack) Use(middleware Middleware)

Use use middleware

Jump to

Keyboard shortcuts

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