Middleware

If you have some code that needs to be run for every request, regardless ofthe route that it will eventually end up invoking, you need some way to stackhttp.Handlers on top of each other and run them in sequence. This problem issolved elegantly through middleware packages. Negroni is a popular middlewarepackage that makes building and stacking middleware very easy while keeping thecomposable nature of the Go web ecosystem intact.

Negroni comes with some default middleware such as Logging, Error Recovery, andStatic file serving. So out of the box Negroni will provide you with a lot ofvalue without a lot of overhead.

The example below shows how to use a Negroni stack with the built in middlewareand how to create your own custom middleware.

  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "github.com/codegangsta/negroni"
  6. )
  7. func main() {
  8. // Middleware stack
  9. n := negroni.New(
  10. negroni.NewRecovery(),
  11. negroni.HandlerFunc(MyMiddleware),
  12. negroni.NewLogger(),
  13. negroni.NewStatic(http.Dir("public")),
  14. )
  15. n.Run(":8080")
  16. }
  17. func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  18. log.Println("Logging on the way there...")
  19. if r.URL.Query().Get("password") == "secret123" {
  20. next(rw, r)
  21. } else {
  22. http.Error(rw, "Not Authorized", 401)
  23. }
  24. log.Println("Logging on the way back...")
  25. }

Exercises

  • Think of some cool middleware ideas and try to implement them using Negroni.
  • Explore how Negroni can be composed with github.com/gorilla/mux using the http.Handler interface.
  • Play with creating Negroni stacks for certain groups of routes instead of the entire application.

原文: https://codegangsta.gitbooks.io/building-web-apps-with-go/content/middleware/index.html