Created
October 12, 2012 03:41
-
-
Save JCallicoat/3877163 to your computer and use it in GitHub Desktop.
Go thing
This file contains 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 "net/http" | |
import "fmt" | |
func hello(w http.ResponseWriter, r *http.Request) { | |
contentType := w.Header().Get("Content-Type") | |
if contentType == "application/json" { | |
fmt.Fprintln(w, `{"Hello": "JSON"}`) | |
} else { | |
w.Header().Set("Content-Type", "text/plain") | |
fmt.Fprintln(w, "Hello wrapped world") | |
} | |
} | |
func wrapMiddleware(f http.HandlerFunc) http.HandlerFunc { | |
return func(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("X-Wrapped", "true") | |
contentType := r.Header.Get("Content-Type") | |
if contentType != "" { | |
w.Header().Set("Content-Type", contentType) | |
} | |
f(w, r) | |
} | |
} | |
func main() { | |
handler := wrapMiddleware(hello) | |
http.HandleFunc("/", handler) | |
http.ListenAndServe(":5000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment