Skip to content

Instantly share code, notes, and snippets.

@JCallicoat
Created October 12, 2012 03:41
Show Gist options
  • Save JCallicoat/3877163 to your computer and use it in GitHub Desktop.
Save JCallicoat/3877163 to your computer and use it in GitHub Desktop.
Go thing
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