Documentation ¶
Overview ¶
Package layer implements a simple HTTP server middleware layer used internally by vinxi to compose and trigger the middleware chain.
Index ¶
- Constants
- Variables
- type Handler
- type HandlerFunc
- type HandlerFuncNext
- type Layer
- func (s *Layer) Flush()
- func (s *Layer) Run(phase string, w http.ResponseWriter, r *http.Request, h http.Handler)
- func (s *Layer) SetParent(parent Middleware)
- func (s *Layer) Use(phase string, handler ...interface{})
- func (s *Layer) UseFinalHandler(fn http.Handler)
- func (s *Layer) UsePriority(phase string, priority Priority, handler ...interface{})
- type Middleware
- type MiddlewareFunc
- type MiddlewareHandlerFunc
- type PartialHandler
- type Pluggable
- type Pool
- type Priority
- type Registrable
- type Runnable
- type Stack
Constants ¶
const ( // ErrorPhase defines error middleware phase idenfitier. ErrorPhase = "error" // RequestPhase defines the default middleware phase for request. RequestPhase = "request" )
Variables ¶
var FinalErrorHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { msg := "unknown panic" err := context.GetError(r, "vinxi.error") if err != nil { msg = err.Error() } w.WriteHeader(500) w.Write([]byte("Proxy error: " + msg)) })
FinalErrorHandler stores the default http.Handler used as final middleware chain. You can customize this handler in order to reply with a default error response.
var FinalHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(502) w.Write([]byte("Bad Gateway")) })
FinalHandler stores the default http.Handler used as final middleware chain. You can customize this handler in order to reply with a default error response.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
Handler represents the vinxi specific supported interface that can be implemented by middleware handlers.
type HandlerFunc ¶
type HandlerFunc func(http.ResponseWriter, *http.Request)
HandlerFunc represents the required function interface for simple middleware handlers.
type HandlerFuncNext ¶
HandlerFuncNext represents a Negroni-like handler function notation.
type Layer ¶
type Layer struct { // RWMutex is used to provide thread-safe sync to the middleware store. sync.RWMutex // Pool stores the phase-specific middleware handlers stack. Pool Pool // contains filtered or unexported fields }
Layer type represent an HTTP domain specific middleware layer with hieritance support.
func (*Layer) Run ¶
Run triggers the middleware call chain for the given phase. In case of panic, it will be recovered transparently and trigger the error middleware chain.ç
func (*Layer) SetParent ¶
func (s *Layer) SetParent(parent Middleware)
SetParent sets a new middleware layer as parent layer, allowing to trigger ancestors layer from the current one.
func (*Layer) UseFinalHandler ¶
UseFinalHandler defines an http.Handler as final middleware call chain handler. This handler is tipically responsible of replying with a custom response or error (e.g: cannot route the request).
func (*Layer) UsePriority ¶
UsePriority registers new handlers for the given phase in the middleware stack with a custom priority.
type Middleware ¶
type Middleware interface { // Middleware is also a Runnable interface. Runnable // Middleware is also a Pluggable interface. Pluggable // Flush flushes the middleware handlers pool. Flush() }
Middleware especifies the required interface that must be implemented by third-party middleware capable interfaces.
type MiddlewareFunc ¶
MiddlewareFunc represents the http.Handler -> http.Handler capable interface.
func AdaptFunc ¶
func AdaptFunc(h interface{}) MiddlewareFunc
AdaptFunc adapts the given function polumorphic interface casting into a MiddlewareFunc capable interface.
Currently support five different interface notations, wrapping it accordingly to make homogeneus.
type MiddlewareHandlerFunc ¶
MiddlewareHandlerFunc represents the http.Handler -> http.HandlerFunc capable interface.
type PartialHandler ¶
PartialHandler represents the vinxi specific supported interface that applies partial function application and can be implemented by middleware handlers.
type Pluggable ¶
type Pluggable interface { // Use method is used to register a new middleware handler in the stack. Use(phase string, handler ...interface{}) // UsePriority method is used to register a new middleware handler in a specific phase. UsePriority(string, Priority, ...interface{}) // UseFinalHandler defines the middleware handler terminator UseFinalHandler(handler http.Handler) // SetParent allows hierarchical middleware inheritance. SetParent(Middleware) }
Pluggable represents a middleware pluggable interface implementing the required methods to plug in middleware handlers.
type Priority ¶
type Priority int
Priority represents the middleware priority.
const ( // TopHead priority defines the middleware handler // as first element of the stack head. TopHead Priority = iota // Head priority stores the middleware handler // in the head of the stack. Head // Normal priority defines the middleware handler // in the last stack available. Normal // TopTail priority defines the middleware handler // as fist element of the stack tail. TopTail // Tail priority defines the middleware handler // in the tail of the stack. Tail )
type Registrable ¶
type Registrable interface { // Register is designed to allow the plugin developers // to attach multiple middleware layers passing the current middleware layer. Register(Middleware) }
Registrable represents the required interface implemented by middleware capable handlers to register one or multiple middleware phases.
This is mostly used as inversion of control mecanish allowing to third-party middleware implementors the ability to register multiple middleware handlers transparently.
For instance, you can register request and error handlers:
func (s *MyStruct) Register(mw layer.Middleware) { mw.Use("request", s.requestHandler) mw.Use("error", s.errorHandler) }
type Stack ¶
type Stack struct { // Head stores the head priority handlers. Head []MiddlewareFunc // Stack stores the middleware normal priority handlers. Stack []MiddlewareFunc // Tail stores the middleware tail priority handlers. Tail []MiddlewareFunc // contains filtered or unexported fields }
Stack stores the data to show.
func (*Stack) Join ¶
func (s *Stack) Join() []MiddlewareFunc
Join joins the middleware functions into a unique slice.
func (*Stack) Push ¶
func (s *Stack) Push(order Priority, h MiddlewareFunc)
Push pushes a new middleware handler to the stack based on the given priority.