Skip to content

Instantly share code, notes, and snippets.

@luooooob
Forked from elithrar/use.go
Created April 28, 2018 17:27
Show Gist options
  • Save luooooob/c0c7f4d7d3062cf12d711d781b254a06 to your computer and use it in GitHub Desktop.
Save luooooob/c0c7f4d7d3062cf12d711d781b254a06 to your computer and use it in GitHub Desktop.

Revisions

  1. @elithrar elithrar revised this gist May 25, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use.go
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    r := mux.NewRouter()

    // Single handler
    r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging,)
    r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging)

    // All handlers
    http.Handle("/", recovery(r))
  2. @elithrar elithrar revised this gist May 22, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions use.go
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,25 @@
    r := mux.NewRouter()

    // Single handler
    r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging,)

    // All handlers
    http.Handle("/", recovery(r))

    // Sub-routers
    apiMiddleware := []func(http.Handler) http.Handler{logging, apiAuth, json}
    api := router.PathPrefix("/api").SubRouter()
    api.Handle("/users", use(usersHandler, apiMiddleware...))

    // Middleware chainer
    func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
    for _, m := range middleware {
    h = m(h)
    }
    return h
    }

    // Middleware (just a http.Handler)
    func csrf(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // do stuff
    @@ -23,6 +28,7 @@ func csrf(h http.Handler) http.Handler {
    })
    }

    // Basic handler at the end of it all.
    func formHandler(w http.ResponseWriter, r *http.Request) {
    // do stuff
    }
  3. @elithrar elithrar revised this gist May 22, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion use.go
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ http.Handle("/", recovery(r))
    // Sub-routers
    apiMiddleware := []func(http.Handler) http.Handler{logging, apiAuth, json}
    api := router.PathPrefix("/api").SubRouter()
    api.Handle("/users", use(usersHandler, apiMiddleware))
    api.Handle("/users", use(usersHandler, apiMiddleware...))

    func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
    for _, m := range middleware {
  4. @elithrar elithrar created this gist May 22, 2014.
    28 changes: 28 additions & 0 deletions use.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    r := mux.NewRouter()
    // Single handler
    r.HandleFunc("/form", use(http.HandlerFunc(formHandler), csrf, logging,)
    // All handlers
    http.Handle("/", recovery(r))
    // Sub-routers
    apiMiddleware := []func(http.Handler) http.Handler{logging, apiAuth, json}
    api := router.PathPrefix("/api").SubRouter()
    api.Handle("/users", use(usersHandler, apiMiddleware))

    func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler {
    for _, m := range middleware {
    h = m(h)
    }
    return h
    }

    func csrf(h http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    // do stuff
    h.ServeHTTP(w, r)
    // do stuff after
    })
    }

    func formHandler(w http.ResponseWriter, r *http.Request) {
    // do stuff
    }