From 45f967cec50c821306e612937dc9564837d041f7 Mon Sep 17 00:00:00 2001 From: prabirshrestha Date: Sat, 3 Jan 2015 16:59:14 -0800 Subject: [PATCH] Added response.Format(request, handlers) --- format.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 format.go 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() + } +}