mw

package module
v0.0.0-...-083e4e8 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2015 License: MIT Imports: 1 Imported by: 3

README

mw Coverage Status GoDoc

mw is a middleware decorator for go servers.

Getting started

To start using mw, install Go and run go get:

$ go get github.com/collinglass/mw
Using it with 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,
)
Building your own 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 Header "Content-Type" to JSON
		w.Header().Set("Content-Type", "application/json")

		h.ServeHTTP(w, r)
	})
}
Using 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)
	}
Using it with 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)
	}

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decorate

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

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

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware takes an http.Handler interface and decorates it.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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