middleware

package module
v0.0.0-...-ece509c Latest Latest
Warning

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

Go to latest
Published: May 31, 2014 License: Apache-2.0 Imports: 2 Imported by: 0

README

Middleware

Simple middleware on top of container/list. Middleware interface is func(rw http.ResponseWriter, r *http.Response, next http.HandlerFunc).

Example Use


router := mux.NewRouter()
router.HandleFunc("/", SomeHandler)

s := middleware.NewStack()
s.Use(Middleware1)
element2 := s.Use(Middleware2)
s.Use(Middleware4)
s.UseHandler(router)

// You can modify the stack because it is a container/list
s.InsertAfter(Middleware3, element2)

// Or compose some set of middleware and use it
s2 := middleware.NewStack()
s2.Use(MiddlewareA)
s2.Use(MiddlewareB)
// Add this middleware stack to the front of the other one.
s.PushFrontList(s2.List)

// Compose converts a Middleware into a func(http.HandlerFunc)http.HandlerFunc
// so it can be called with Alice or just composing(functions(like(this))).
m1 := middleware.Compose(MiddlewareA)
m2 := middleware.Compose(MiddlewareB)
m3 := middleware.Compose(MiddlewareC)

// Stack 3
s3 := m1(m2(m3(http.DefaultServeMux)))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose(m Middleware) func(http.Handler) http.Handler

Compose converts a Middleware into a func(http.Handler)http.Handler so it can be called with Alice or just composing(functions(like(this))).

Types

type Middleware

type Middleware interface {
	ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)
}

Middleware handler is an interface that objects can implement to be registered to serve as middleware in the stack. ServeHTTP should yield to the next middleware in the chain by invoking the next MiddlewareFunc. passed in.

func Wrap

func Wrap(handler http.HandlerFunc) Middleware

Wrap converts a Handler into a Middleware so it can be used as a middleware. The next HandlerFunc is automatically called after the Middleware is executed.

type MiddlewareFunc

type MiddlewareFunc func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

MiddlewareFunc is an adapter to allow the use of ordinary functions as middleware handlers. If f is a function with the appropriate signature, MiddlewareFunc(f) is a Middleware object that calls f.

func (MiddlewareFunc) ServeHTTP

func (h MiddlewareFunc) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc)

type Stack

type Stack struct {
	*list.List
}

Stack is a linked list stack of middleware

func NewStack

func NewStack() *Stack

NewStack returns a new linked list Stack of middlware

func (*Stack) ServeHTTP

func (s *Stack) ServeHTTP(rw http.ResponseWriter, r *http.Request)

func (*Stack) Use

func (s *Stack) Use(handler Middleware) *list.Element

Use adds a Middleware onto the middleware stack. Middlewares are invoked in the order they are added unless otherwise specified.

func (*Stack) UseHandler

func (s *Stack) UseHandler(handler http.HandlerFunc) *list.Element

UseHandler adds a Handler onto the middleware stack. Handlers are invoked in the order they are added unless otherwise specified.

Jump to

Keyboard shortcuts

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