Negroni
Negroni is an idiomatic approach to web middleware in Go. It is tiny, non-intrusive, and encourages use of net/http
Handlers.
If you like the idea of Martini, but you think it contains too much magic, then Negroni is a great fit.
Language Translations:
Getting Started
After installing Go and setting up your GOPATH, create your first .go
file. We'll call it server.go
.
Then install the Negroni package (go 1.1 and greater is required):
Then run your server:
You will now have a Go net/http webserver running on localhost:3000
.
Need Help?
If you have a question or feature request, go ask the mailing list. The GitHub issues for Negroni will be used exclusively for bug reports and pull requests.
Is Negroni a Framework?
Negroni is not a framework. It is a library that is designed to work directly with net/http.
Routing?
Negroni is BYOR (Bring your own Router). The Go community already has a number of great http routers available, Negroni tries to play well with all of them by fully supporting net/http
. For instance, integrating with Gorilla Mux looks like so:
negroni.Classic()
negroni.Classic()
negroni.Classic()
provides some default middleware that is useful for most applications:
negroni.Recovery
- Panic Recovery Middleware.negroni.Logger
- Request/Response Logger Middleware.negroni.Static
- Static File serving under the "public" directory.
This makes it really easy to get started with some useful features from Negroni.
Handlers
Negroni provides a bidirectional middleware flow. This is done through the negroni.Handler
interface:
If a middleware hasn't already written to the ResponseWriter, it should call the next http.HandlerFunc
in the chain to yield to the next middleware handler. This can be used for great good:
And you can map it to the handler chain with the Use
function:
You can also map plain old http.Handler
s:
Run()
Run()
Negroni has a convenience function called Run
. Run
takes an addr string identical to http.ListenAndServe.
Route Specific Middleware
If you have a route group of routes that need specific middleware to be executed, you can simply create a new Negroni instance and use it as your route handler.
If you are using Gorilla Mux here is an example using a subrouter.
Third Party Middleware
Here is a current list of Negroni compatible middlware. Feel free to put up a PR linking your middleware if you have built one:
Middleware | Author | Description |
Secure authentication for REST API endpoints | ||
Graceful HTTP Shutdown | ||
Middleware that implements a few quick security wins | ||
Middleware checks for a JWT on the | ||
Data binding from HTTP requests into structs | ||
Logrus-based logger | ||
Render JSON, XML and HTML templates | ||
New Relic agent for Go runtime | ||
GZIP response compression | ||
oAuth2 middleware | ||
Session Management | ||
Cookies, users and permissions | ||
Generate TinySVG, HTML and CSS on the fly | ||
Cross Origin Resource Sharing (CORS) support | ||
Middleware that assigns a random X-Request-Id header to each request | ||
Configurable AWS-Style HMAC authentication middleware | ||
Store information about your web application (response time, etc.) | ||
Easily create metrics endpoint for the prometheus instrumentation tool | ||
Add delays/latency to endpoints. Useful when testing effects of high latency |
Examples
Alexander Rødseth created mooseware, a skeleton for writing a Negroni middleware handler.
Live code reload?
gin and fresh both live reload negroni apps.
Essential Reading for Beginners of Go & Negroni
About
Negroni is obsessively designed by none other than the Code Gangsta
Last updated