diff --git a/format.go b/format.go new file mode 100644 index 0000000..431ef2b --- /dev/null +++ b/format.go @@ -0,0 +1,21 @@ +package response + +import "net/http" +import "bitbucket.org/ww/goautoneg" + +func Format(r *http.Request, handlers map[string]func()) { + contentTypes := []string{} + for k := range handlers { + contentTypes = append(contentTypes, k) + } + + accept := r.Header.Get("Accept") + + contentType := goautoneg.Negotiate(accept, contentTypes) + + if handler, ok := handlers[contentType]; ok { + handler() + } else if handler, ok := handlers["default"]; ok { + handler() + } +}