-
-
Save luooooob/c0c7f4d7d3062cf12d711d781b254a06 to your computer and use it in GitHub Desktop.
Revisions
-
elithrar revised this gist
May 25, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) // All handlers http.Handle("/", recovery(r)) -
elithrar revised this gist
May 22, 2014 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } -
elithrar revised this gist
May 22, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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...)) func use(h http.Handler, middleware ...func(http.Handler) http.Handler) http.Handler { for _, m := range middleware { -
elithrar created this gist
May 22, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 }