Chapter 6.
Middleware
When you’re building a web application there’s probably some shared functionality that you want to use for many (or even all) HTTP requests. For example, you might want to log every request, compress every response, or check a cache before passing the request to your handlers.
A common way of organizing this shared functionality is to set it up as middleware. This is essentially some self-contained code which independently acts on a request before or after your normal application handlers.
In this section of the book you’ll learn:
- An idiomatic pattern for building and using custom middleware which is compatible with
net/http
and many third-party packages. - How to create middleware which sets common HTTP headers on every HTTP response.
- How to create middleware which logs the requests received by your application.
- How to create middleware which recovers panics so that they are gracefully handled by your application.
- How to create and use composable middleware chains to help manage and organize your middleware.