-
-
Save aletessier/0ffb97aaea1f201c3203 to your computer and use it in GitHub Desktop.
Example of chaining middlware in Go
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 ( | |
| "bytes" | |
| "github.com/goji/httpauth" | |
| "github.com/gorilla/handlers" | |
| "net/http" | |
| "os" | |
| ) | |
| func enforceXMLHandler(next http.Handler) http.Handler { | |
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | |
| if r.ContentLength == 0 { | |
| http.Error(w, http.StatusText(400), 400) | |
| return | |
| } | |
| buf := new(bytes.Buffer) | |
| buf.ReadFrom(r.Body) | |
| if http.DetectContentType(buf.Bytes()) != "text/xml; charset=utf-8" { | |
| http.Error(w, http.StatusText(415), 415) | |
| return | |
| } | |
| next.ServeHTTP(w, r) | |
| }) | |
| } | |
| func myLoggingHandler(h http.Handler) http.Handler { | |
| logFile, err := os.OpenFile("server.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) | |
| if err != nil { | |
| panic(err) | |
| } | |
| return handlers.LoggingHandler(logFile, h) | |
| } | |
| func main() { | |
| indexHandler := http.HandlerFunc(index) | |
| authHandler := httpauth.SimpleBasicAuth("username", "password") | |
| http.Handle("/", myLoggingHandler(authHandler(enforceXMLHandler(indexHandler)))) | |
| http.ListenAndServe(":3000", nil) | |
| } | |
| func index(w http.ResponseWriter, r *http.Request) { | |
| w.Write([]byte("OK")) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment