• Stars
    star
    294
  • Rank 141,303 (Top 3 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Minimalist net/http middleware for golang

interpose

Interpose is a minimalist net/http middleware framework for golang. It uses http.Handler as its core unit of functionality, minimizing complexity and maximizing inter-operability with other middleware frameworks.

All that it does is manage middleware. It comes with nothing baked in. You bring your own router, etc. See below for some well-baked examples.

Because of its reliance on the net/http standard, Interpose is out-of-the-box compatible with the Gorilla framework, goji, nosurf, and many other frameworks and standalone middleware.

Many projects claim to be http.Handler-compliant but actually just use http.Handlers to create a more complicated/less compatible abstraction. Therefore, a goal of the project is also to create adaptors so that non-http.Handler compliant middleware can still be used. As an example of this, an adaptor for Negroni middleware is available, meaning that any middleware that is Negroni compliant is also Interpose compliant. The same is true for Martini middleware.

API change

Please note that the API has recently changed. Previously, the framework applied middleware in LIFO order. Now it applies middleware in FIFO order.

Usage

To use, first:

go get github.com/carbocation/interpose

Basic usage example

Here is one example of using Interpose to execute middleware that adds the HTTP header "X-Server-Name" to every response.

Create a file main.go with the following:

package main

import (
	"fmt"
	"net/http"

	"github.com/carbocation/interpose"
)

func main() {
	middle := interpose.New()

	// Send a header telling the world this is coming from an Interpose Test Server
	// You could imagine setting Content-type application/json or other useful
	// headers in other circumstances.
	middle.UseHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("X-Server-Name", "Interpose Test Server")
	}))

	// In this example, we use a basic router with a catchall route that
	// matches any path. In other examples in this project, we use a
	// more advanced router.
	// The last middleware added is the last executed, so by adding the router
	// last, our other middleware get a chance to modify HTTP headers before our
	// router writes to the HTTP Body
	router := http.NewServeMux()
	middle.UseHandler(router)

	router.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to %s!", req.URL.Path)
	}))

	http.ListenAndServe(":3001", middle)
}

In the same path as that file, type go run main.go

Now launch your browser and point it to http://localhost:3001/world to see output.

Additional examples can be found below.

Philosophy

Interpose is a minimalist Golang middleware that uses only http.Handler and func(http.Handler)http.Handler. Interpose takes advantage of closures to create a stack of native net/http middleware. Unlike other middleware libraries which create their own net/http-like signatures, interpose uses literal net/http signatures, thus minimizing package lock-in and maximizing inter-compatibility.

Middleware is called in nested FIFO fashion, which means that the first middleware to be added will be the first middleware to be called. Because the middleware is nested, it actually means that the first middleware to be added gets the opportunity to make the first and the last calls in the stack. For example, if there are 3 middlewares added in order (0, 1, 2), the calls look like so:

//0 START
	//1 START
		//2 START
		//2 END
	//1 END
//0 END

Middleware

Here is a current list of Interpose compatible middleware that have pre-built examples working with Interpose. Any middleware that yields an http.Handler or a func(http.Handler)http.Handler should be compliant. Pull requests linking to other middleware are encouraged.

Middleware Usage example Author Description
Graceful Graceful example Tyler Bunnell Graceful HTTP Shutdown
secure Secure example Cory Jacobsen Middleware that implements a few quick security wins
Gorilla logger Gorilla log example Gorilla team Gorilla Apache CombinedLogger
Logrus Logrus example Dan Buch Logrus-based logger, also demonstrating how Negroni packages can be used in Interpose
Buffered output Buffer example zeebo Output buffering demonstrating how headers can be written after HTTP body is sent
nosurf nosurf example justinas A CSRF protection middleware for Go.
BasicAuth BasicAuth example Jeremy Saenz & Brendon Murphy HTTP BasicAuth - based on martini's auth middleware
Martini Auth Martini Auth example Jeremy Saenz & Brendon Murphy A basic HTTP Auth implementation that also demonstrates how Martini middleware packages can be used directly in Interpose with a simple wrapper.

Adaptors

Some frameworks that are not strictly http.Handler compliant use middleware that can be readily converted into Interpose-compliant middleware. So far, adaptors for Martini and Negroni have been created.

For example, to use github.com/urfave/negroni middleware in Interpose, you can use adaptors.FromNegroni:

	middle := interpose.New()

	// has signature `negroni.Handler`
	negroniMiddleware := negronilogrus.NewMiddleware()

	// Use the Negroni middleware within Interpose
	middle.Use(adaptors.FromNegroni(negroniMiddleware))

More examples

Routing, graceful shutdown, and headers

In this example, we use the graceful package to gracefully release connections after the shutdown signal is encountered. A more powerful router than in our prior example, Gorilla mux, is used. Also, we send the browser headers indicating that this came from a server named "Interpose Test Server."

package main

import (
	"fmt"
	"net/http"
	"time"

	"github.com/carbocation/interpose"
	"github.com/gorilla/mux"
	"github.com/stretchr/graceful"
)

func main() {
	middle := interpose.New()

	// Tell the browser which server this came from.
	// This modifies headers, so we want this to be called before
	// any middleware which might modify the body (in HTTP, the headers cannot be
	// modified after the body is modified)
	middle.Use(func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
			rw.Header().Set("X-Server-Name", "Interpose Test Server")
			next.ServeHTTP(rw, req)
		})
	})

	// Apply the router. By adding it last, all of our other middleware will be
	// executed before the router, allowing us to modify headers before any
	// output has been generated.
	router := mux.NewRouter()
	middle.UseHandler(router)

	router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"])
	})

	// Launch and permit graceful shutdown, allowing up to 10 seconds for existing
	// connections to end
	graceful.Run(":3001", 10*time.Second, middle)
}

Combined logging and gzipping

Print an Apache CombinedLog-compatible log statement to StdOut and gzip the HTTP response it if the client has gzip capabilities:

package main

import (
	"compress/gzip"
	"fmt"
	"net/http"

	"github.com/carbocation/interpose"
	"github.com/carbocation/interpose/middleware"
	"github.com/gorilla/mux"
)

func main() {
	middle := interpose.New()

	// First apply any middleware that will not write output to http body

	// Log to stdout. Taken from Gorilla
	middle.Use(middleware.GorillaLog())

	// Gzip output that follows. Taken from Negroni
	middle.Use(middleware.NegroniGzip(gzip.DefaultCompression))

	// Now apply any middleware that modify the http body.
	router := mux.NewRouter()
	middle.UseHandler(router)

	router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"])
	})

	http.ListenAndServe(":3001", middle)
}

Wrapped middleware

Middleware can be wrapped around other middleware. In this example, we greet people who arrive at /{user}, but we offer a special greeting to those who arrive at /green/{user}:

package main

import (
	"fmt"
	"net/http"

	"github.com/carbocation/interpose"
	"github.com/carbocation/interpose/middleware"
	"github.com/gorilla/mux"
)

func main() {
	middle := interpose.New()

	// Invoke the Gorilla framework's combined logger
	middle.Use(middleware.GorillaLog())

	// Create a router to serve HTTP content at two paths
	// and tell our middleware about the router
	router := mux.NewRouter()
	middle.UseHandler(router)

	router.PathPrefix("/green").Subrouter().Handle("/{name}", Green(http.HandlerFunc(welcomeHandler)))
	router.Handle("/{name}", http.HandlerFunc(welcomeHandler))

	http.ListenAndServe(":3001", middle)
}

func welcomeHandler(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(rw, "Welcome to the home page, %s", mux.Vars(req)["name"])
}

func Green(next http.Handler) http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("X-Favorite-Color", "green")
		next.ServeHTTP(rw, req)
		fmt.Fprint(rw, " who likes green")
	})
}

Nested middleware: adding headers for only some routes

In the last example, we applied different middleware to different routes. Here we will expand this idea to created fully nested middleware stacks within different routes. This approach, while more verbose than the last example, is arbitrarily powerful. In this example, routes starting with /green are given a special HTTP header X-Favorite-Color: green, but you can also imagine using this same approach to automatically apply the JSON content header for JSON requests, putting authentication in front of protected paths, etc.

package main

import (
	"compress/gzip"
	"fmt"
	"net/http"

	"github.com/carbocation/interpose"
	"github.com/carbocation/interpose/middleware"
	"github.com/gorilla/mux"
)

func main() {
	middle := interpose.New()

	// First call middleware that may manipulate HTTP headers, since
	// they must be called before the HTTP body is manipulated

	// Using Gorilla framework's combined logger
	middle.Use(middleware.GorillaLog())

	//Using Negroni's Gzip functionality
	middle.Use(middleware.NegroniGzip(gzip.DefaultCompression))

	// Now call middleware that can manipulate the HTTP body

	// Define the router. Note that we can add the router to our
	// middleware stack before we define the routes, if we want.
	router := mux.NewRouter()
	middle.UseHandler(router)

	// Configure our router
	router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"])
	})

	// Define middleware that will apply to only some routes
	greenMiddle := interpose.New()

	// Tell the main router to send /green requests to our subrouter.
	// Again, we can do this before defining the full middleware stack.
	router.Methods("GET").PathPrefix("/green").Handler(greenMiddle)

	// Within the secondary middleware, just like above, we want to call anything that
	// will modify the HTTP headers before anything that will modify the body
	greenMiddle.UseHandler(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
		rw.Header().Set("X-Favorite-Color", "green")
	}))

	// Finally, define a sub-router based on our love of the color green
	// When you call any url such as http://localhost:3001/green/man , you will
	// also see an HTTP header sent called X-Favorite-Color with value "green"
	greenRouter := mux.NewRouter().Methods("GET").PathPrefix("/green").Subrouter() //Headers("Accept", "application/json")
	greenMiddle.UseHandler(greenRouter)

	greenRouter.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Welcome to the home page, green %s!", mux.Vars(req)["user"])
	})

	http.ListenAndServe(":3001", middle)
}

For more examples, please look at the examples folder as well as its subfolder, the menagerie folder

Authors

Originally developed by carbocation. Please see the contributors file for an expanded list of contributors.

More Repositories

1

gotogether

Tool permitting static assets to be attached to compiled go binaries, permitting single-file project deployment
Go
22
star
2

PHP-Multivariate-Regression

A multivariate regression package for PHP 5.3+
PHP
18
star
3

genomisc

Miscellaneous genomics tools and data structures
Go
15
star
4

runningvariance

A package for accurately computing running (online) mean, variance, and standard deviation in golang
Go
11
star
5

matriximage

Golang utility to convert between (grayscale) images and their Fourier transforms, and to manipulate them in frequency space
Go
10
star
6

go.graf

GRAF Recursively Arranged Forum
Go
7
star
7

go.user

A user object and methods, used for a threaded forum
Go
7
star
8

go.forum

Bits and pieces that I needed for building a forum
Go
6
star
9

pagerank

A Monte Carlo implementation of the PageRank algorithm
Go
5
star
10

go.websocket-chat

A chat library using websockets and golang
Go
5
star
11

fda

Golang bindings into the OpenFDA API
Go
4
star
12

traceoverlay

Image annotation
Go
3
star
13

CarbocationStatisticsBundle

A Symfony2-compatible linear regression (statistics) bundle written for PHP 5.3+
PHP
3
star
14

falsediscovery

Go
2
star
15

csvmutex

Wraps the CSV library with a mutex for the CSV writer
Go
2
star
16

combigram

Lets you pull down instagram photos having multiple hashtags
Go
2
star
17

aberrant

Aberrant R package from https://www.well.ox.ac.uk/~spencer/Aberrant/
R
2
star
18

throwaway

1
star
19

matrixutils

Utilities for working with github.com/mjibson/go-dsp/dsputils
Go
1
star
20

go.util

Things that don't deserve their own repo
Go
1
star
21

go.sudoku

A go implementation of Norvig's Sudoku solver
Go
1
star
22

pfx

Prefix error messages with the name of the calling function in golang
Go
1
star
23

jupyter

Jupyter notebooks
Jupyter Notebook
1
star
24

colormasked

A go package containing a color-masked image which satisfies the image.Image interface
Go
1
star
25

grd-histogram

Fork of the github.com/grd/histogram repo
Go
1
star
26

bgen

BGEN file format reader for parsing UK Biobank and other genotype data in Go
Go
1
star