middleware

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

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

Go to latest
Published: Dec 8, 2013 License: BSD-3-Clause Imports: 3 Imported by: 0

README

go-middleware

Middleware is often described as software glue. go-middleware focuses specifically on glueing together functions related to serving HTTP requests (such http.Handler/http.HandlerFunc).

Middleware is used for authentication (including Openid/OAuth redirects), authorization, request logging, error handling, request scoping, request throttling, request/response buffering, compression/decompression, CORS, CSRF, data binding, routing, content-type inference (rendering the result as JSON, XML as required), handling protocol upgrades (ex. Web Sockets), data hydration (ex. loading data into the request context based on an identifier (header/cookie) such as sessions), pre/post filters and more and are generally considered the 'core' of a web framework.

Having investigated multiple web frameworks on how they implement middleware; I found that none of them implemented middleware in what I consider idiomatic Go:

  • Pluggability: Creating middleware or middleware-adapters shouldn't require a dependency on the middleware chaining implementation (ex. Go interfaces are satisfied implicitly) - See the context middleware
  • Usability: Middleware can have multiple forms and shouldn't require satisfying a specific interface (ex. http.Handler/middleware.Middleware) to be used as such - See middleware.go type switch that supports 9 common variants
  • Succinctness: The implementation should be concise and terse to reduce ambiguity and be considered self-documenting. middleware.go is 39 lines excluding white space and comments.

Go programming is different from traditional object oriented programming (OOP) and such existing implementations tend to translate poorly into Go.

Example Usage

import "net/http"
import "github.com/shelakel/go-middleware"
var handler http.HandlerFunc = middleware.Compose(...)

See example

Installation
go get -u github.com/shelakel/go-middleware 
Documentation

See GoDoc on Github

See LICENSE

Middleware

done packages may be used
work in progress (WIP) packages should not be used - only for inspiration
TODO packages currently lack implementation

  • context - based on context - done
  • compression - WIP
  • cors - WIP
  • panic/recover error handling - WIP
  • request logging - WIP
  • static file server - TODO
  • CSRF - nosurf - TODO
  • Request throttling - TODO
  • Request/Response buffering - TODO
  • Auth - TODO
  • Sessions - TODO
  • bind - binding / schema - TODO

Middleware/Web Frameworks (for inspiration)

Links indicate interesting implementations
* can be converted or may be useful in middleware

Web Frameworks

Author/s

  • Shelakel
  • Attribution for inspiration as per Middleware/Web Frameworks

Documentation

Overview

Package middleware provides support for composing on demand and reusable filters/middleware

See github.com/shelakel/go-middleware/example for an example on how Compose is used to create middleware and pre/post filters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compose

func Compose(middleware ...interface{}) http.HandlerFunc

Compose chains together the middleware and returns a http.HandlerFunc (which is compatible with http.Handler). Allowed middleware variants include: func(http.ResponseWriter, *http.Request, func(http.ResponseWriter, *http.Request)), func(http.ResponseWriter, *http.Request, http.HandlerFunc), func(http.ResponseWriter, *http.Request, http.Handler), Middleware, MiddlewareFunc, func(http.ResponseWriter, *http.Request), http.HandlerFunc, http.Handler and func(http.Handler) http.Handler.

Types

type Middleware

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

Middleware allows for creating reusable middleware that require initial configuration

type MiddlewareFunc

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

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

func (MiddlewareFunc) Process

func (fn MiddlewareFunc) Process(w http.ResponseWriter, r *http.Request, next http.HandlerFunc)

Process calls f(w, r, next).

Directories

Path Synopsis
Package context allows for storing values on a *Request and is a middleware implementation of github.com/gorilla/context.
Package context allows for storing values on a *Request and is a middleware implementation of github.com/gorilla/context.

Jump to

Keyboard shortcuts

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