Skip to content

Instantly share code, notes, and snippets.

@orbitalmedia
Forked from pagreczner/negroniGorillaMux.go
Created June 23, 2016 05:06
Show Gist options
  • Select an option

  • Save orbitalmedia/618e9218b5f0a66fe495d1b77d49d9ed to your computer and use it in GitHub Desktop.

Select an option

Save orbitalmedia/618e9218b5f0a66fe495d1b77d49d9ed to your computer and use it in GitHub Desktop.

Revisions

  1. Peter Greczner revised this gist Aug 24, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions negroniGorillaMux.go
    Original file line number Diff line number Diff line change
    @@ -28,8 +28,8 @@ func NegroniRoute(m *mux.Router,
    base string,
    path string,
    pathType string,
    f func(http.ResponseWriter, *http.Request),
    mids ...func(http.ResponseWriter, *http.Request, http.HandlerFunc)
    f func(http.ResponseWriter, *http.Request), // Your Route Handler
    mids ...func(http.ResponseWriter, *http.Request, http.HandlerFunc) // Middlewares
    ) {
    _routes := mux.NewRouter()
    _routes.HandleFunc(base+path, f).Methods(pathType)
  2. Peter Greczner revised this gist Aug 24, 2014. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions negroniGorillaMux.go
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,14 @@ import (
    )

    func main() {
    router := mux.NewRouter()
    apiV1 := router.PathPrefix("/api/v1").Subrouter()

    NegroniRouter(apiV1, "/api/v1", "/users/create", "POST", externalLib.CreateUser, externalAuthLib.Authorize)

    n := negroni.Classic()
    n.UseHandler(router)
    n.Run(":3000")
    }

    /* Generates a negroni handler for the route:
  3. Peter Greczner revised this gist Aug 24, 2014. 1 changed file with 36 additions and 1 deletion.
    37 changes: 36 additions & 1 deletion negroniGorillaMux.go
    Original file line number Diff line number Diff line change
    @@ -1 +1,36 @@
    package main
    package main

    import (
    "github.com/codegangsta/negroni"
    "github.com/gorilla/mux"
    )

    func main() {

    }

    /* Generates a negroni handler for the route:
    pathType (base+string)
    which is handled by `f` and passed through
    middleware `mids` sequentially.
    ex:
    NegroniRoute(router, "/api/v1", "/users/update", "POST", UserUpdateHandler, LoggingMiddleware, AuthorizationMiddleware)
    */
    func NegroniRoute(m *mux.Router,
    base string,
    path string,
    pathType string,
    f func(http.ResponseWriter, *http.Request),
    mids ...func(http.ResponseWriter, *http.Request, http.HandlerFunc)
    ) {
    _routes := mux.NewRouter()
    _routes.HandleFunc(base+path, f).Methods(pathType)

    _n := negroni.New()
    for i: range(mids) {
    _n.Use(negroni.HandlerFunc(mids[i]))
    }
    _n.UseHandler(_routes)
    m.Handle(path, _n)
    }
  4. Peter Greczner created this gist Aug 24, 2014.
    1 change: 1 addition & 0 deletions negroniGorillaMux.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    package main