mw: github.com/collinglass/mw Index | Files | Directories

package mw

import "github.com/collinglass/mw"

Package mw provides a Decorate function to cleanly decorate http.Handler interfaces with multiple middlewares.

Go http middleware uses the following form

func Middleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// middleware logic

		h.ServeHTTP(w, r)
	})
}

You can use it with any existing middleware.

import (
	// ...
	"github.com/collinglass/mw"
	"github.com/justinas/nosurf"
)

// decorate router
server := mw.Decorate(
	http.NewServeMux(),
	// add middleware from existing packages
	nosurf.NewPure,
)

Or you can build your own custom middleware.

import (
	// ...
	"github.com/collinglass/mw"
)

// Create middleware from scratch
func JSONMiddleware(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Set Response Content-Type to application/json
		w.Header().Set("Content-Type", "application/json")

		h.ServeHTTP(w, r)
	})
}

You can use it with gorilla mux.

// new router
r := mux.NewRouter()

r.HandleFunc("/api/data", DataHandler).Methods("GET")

// decorate router
server := mw.Decorate(
	r,
	nosurf.NewPure,
	JSONMiddleware,
)

http.Handle("/api/", server)
err := http.ListenAndServe(":8080", nil)
if err != nil {
	panic(err)
}

Or with the standard library http.ServeMux.

// new router
r := http.NewServeMux()

r.HandleFunc("/api/data", DataHandler)

// decorate router
server := mw.Decorate(
	r,
	nosurf.NewPure,
	JSONMiddleware,
)

http.Handle("/api/", server)
err := http.ListenAndServe(":8080", nil)
if err != nil {
	panic(err)
}

Index

Package Files

mw.go

func Decorate Uses

func Decorate(h http.Handler, ds ...Middleware) http.Handler

Decorate ranges over a variadic number of middleware and decorates the http.Handler with them.

type Middleware Uses

type Middleware func(http.Handler) http.Handler

Middleware takes an http.Handler interface and decorates it.

Directories

PathSynopsis
example

Package mw imports 1 packages (graph) and is imported by 1 packages. Updated 2016-07-16. Refresh now. Tools for package owners.