Golang HTTP Handlers as Middleware
Contents
Most modern web stacks allow the “filtering” of requests via stackable/composable middleware, allowing you to cleanly separate cross-cutting concerns from your web application. This weekend I needed to hook into go’s http.FileServer
and was pleasantly surprised how easy it was to do.
Let’s start with a basic file server for /tmp
:
|
|
This starts up a local file server at :8080. How can we hook into this so we can run some code before file requests are served? Let’s look at the method signature for http.ListenAndServe
:
|
|
So it looks like http.FileServer
returns a Handler
that knows how to serve files given a root directory. Now let’s look at the Handler
interface:
|
|
Because of go’s granular interfaces, any object can be a Handler
so long as it implements ServeHTTP
. It seems all we need to do is construct our own Handler
that wraps http.FileServer
's handler. There’s a built in helper for turning ordinary functions into handlers called http.HandlerFunc
:
|
|
Then we just wrap http.FileServer
like so:
|
|
Go has a bunch of other builtin handlers like TimeoutHandler and RedirectHandler that can be mixed and matched the same way.