route.go 644 B

12345678910111213141516171819202122
  1. package module
  2. import (
  3. "net/http"
  4. "strings"
  5. "github.com/gorilla/mux"
  6. "github.com/urfave/negroni"
  7. )
  8. // NegroniRoute helps with using middlewares, found on the interwebz; called by modules when registering APIs
  9. func NegroniRoute(m *mux.Router, base string, path string, pathTypes string, f func(http.ResponseWriter, *http.Request), mids ...func(http.ResponseWriter, *http.Request, http.HandlerFunc)) {
  10. _routes := mux.NewRouter()
  11. _routes.HandleFunc(base+path, f).Methods(strings.Split(pathTypes, " ")...)
  12. _n := negroni.New()
  13. for i := range mids {
  14. _n.Use(negroni.HandlerFunc(mids[i]))
  15. }
  16. _n.UseHandler(_routes)
  17. m.Handle(path, _n)
  18. }