layer

package
v0.0.0-...-3f1871c Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2017 License: MIT, MIT Imports: 3 Imported by: 0

README

layer Build Status GoDoc Coverage Status Go Report Card API

layer implements a simple phase-oriented HTTP middleware layer that behavies likes a first-in first-out queue with built-in error handling and interface overload for better interpolarity with standard and framework-specific interfaces.

Installation

go get -u gopkg.in/vinxi/vinxi.v0

API

See godoc reference.

Supported interaces

License

MIT

Documentation

Overview

Package layer implements a simple HTTP server middleware layer used internally by vinxi to compose and trigger the middleware chain.

Index

Constants

View Source
const (
	// ErrorPhase defines error middleware phase idenfitier.
	ErrorPhase = "error"
	// RequestPhase defines the default middleware phase for request.
	RequestPhase = "request"
)

Variables

View Source
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.

View Source
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

type Handler interface {
	HandleHTTP(http.ResponseWriter, *http.Request, http.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

type HandlerFuncNext func(http.ResponseWriter, *http.Request, http.Handler)

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 New

func New() *Layer

New creates a new middleware layer.

func (*Layer) Flush

func (s *Layer) Flush()

Flush flushes the middleware pool.

func (*Layer) Run

func (s *Layer) Run(phase string, w http.ResponseWriter, r *http.Request, h http.Handler)

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) Use

func (s *Layer) Use(phase string, handler ...interface{})

Use registers new handlers for the given phase in the middleware stack.

func (*Layer) UseFinalHandler

func (s *Layer) UseFinalHandler(fn http.Handler)

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

func (s *Layer) UsePriority(phase string, priority Priority, handler ...interface{})

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

type MiddlewareFunc func(http.Handler) http.Handler

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

type MiddlewareHandlerFunc func(http.Handler) func(http.ResponseWriter, *http.Request)

MiddlewareHandlerFunc represents the http.Handler -> http.HandlerFunc capable interface.

type PartialHandler

type PartialHandler interface {
	HandleHTTP(http.Handler) func(http.ResponseWriter, *http.Request)
}

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 Pool

type Pool map[string]*Stack

Pool represents the phase-specific stack to store middleware functions.

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 Runnable

type Runnable interface {
	Run(string, http.ResponseWriter, *http.Request, http.Handler)
}

Runnable represents the required interface for a runnable

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) Len

func (s *Stack) Len() int

Len returns the middleware stack length.

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.

Jump to

Keyboard shortcuts

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