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 characters
| type logResponseWriter struct { | |
| http.ResponseWriter | |
| status int | |
| } | |
| func (w *logResponseWriter) WriteHeader(status int) { | |
| w.status = status | |
| w.ResponseWriter.WriteHeader(status) | |
| } |
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 characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "math/rand" | |
| "net/http" | |
| ) | |
| type middlewareFunc func(next http.HandlerFunc) http.HandlerFunc |
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 characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "math/rand" | |
| "net/http" | |
| ) | |
| type middlewareFunc func(next http.HandlerFunc) http.HandlerFunc |
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 characters
| func main() { | |
| http.Handle("/api/v1", chain(businessLogic("12345"), logger, shortCircuitLogic(deciderFunc))) | |
| log.Print("Listening (finalhandler) ...") | |
| err := http.ListenAndServe(":8080", nil) | |
| if err != nil { | |
| log.Fatal(err.Error()) | |
| } | |
| } | |
| func businessLogic(p1 string) http.HandlerFunc { |
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 characters
| type middlewareFunc func(next http.HandlerFunc) http.HandlerFunc | |
| func chain(handler http.HandlerFunc, chain ...middlewareFunc) http.Handler { | |
| return http.HandlerFunc(recurseChain(handler, chain)) | |
| } | |
| func recurseChain(handler http.HandlerFunc, chain []middlewareFunc) http.HandlerFunc { | |
| if len(chain) <= 0 { | |
| return handler | |
| } |
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 characters
| package main | |
| import ( | |
| "fmt" | |
| "log" | |
| "math/rand" | |
| "net/http" | |
| ) | |
| type middlewareFunc func(next http.HandlerFunc) http.HandlerFunc |
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 characters
| func deciderFunc() bool { | |
| return rand.Intn(2) == 1 | |
| } | |
| func shortCircuitLogic(deciderFunc func() bool) middlewareFunc { | |
| return func(next http.HandlerFunc) http.HandlerFunc { | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| d := deciderFunc() | |
| var decision string |
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 characters
| func logger(next http.HandlerFunc) http.HandlerFunc { | |
| return func(w http.ResponseWriter, r *http.Request) { | |
| log.Printf("Request: %v", r.URL.Path) | |
| next(w, r) | |
| log.Printf("Request complete: %v", r.URL.Path) | |
| } | |
| } |
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 characters
| func main() { | |
| http.Handle("/api/v1", chain(businessLogic("12345"))) | |
| log.Print("Listening (recursivechain) ...") | |
| err := http.ListenAndServe(":8080", nil) | |
| if err != nil { | |
| log.Fatal(err.Error()) | |
| } | |
| } | |
| func businessLogic(p1 string) middlewareFunc { |
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 characters
| type middlewareFunc func(next http.HandlerFunc) http.HandlerFunc | |
| func chain(chain ...middlewareFunc) http.Handler { | |
| return http.HandlerFunc(recurseChain(chain)) | |
| } | |
| func recurseChain(chain []middlewareFunc) http.HandlerFunc { | |
| if len(chain) <= 0 { | |
| return func(_ http.ResponseWriter, _ *http.Request) {} | |
| } |