-
Notifications
You must be signed in to change notification settings - Fork 1
/
middlewares.go
43 lines (38 loc) · 1.3 KB
/
middlewares.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package gojison
import (
"net/http"
"strings"
"encoding/json"
"github.com/ndyakov/whatever"
"github.com/zenazn/goji/web"
)
// Response will set the Content-Type of the http response to:
// "application/json"
func Response(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
}
return http.HandlerFunc(fn)
}
// Request will parse the request body to a whatever.Params structure and
// then add this structure to the goji context map with the key "Params".
// The error (or nil) of the decoding will be available in the context with
// the key "GojisonDecodeError".
//
// The parsing of the body will happen only if the Content-Type of the request
// is application/json.
//
// For more information about how to work with the whatever.Params type, please refer to:
// http://godoc.org/github.com/ndyakov/whatever
func Request(c *web.C, h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
contentTypeSlice := strings.Split(r.Header.Get("Content-Type"), ";")
if contentTypeSlice[0] == "application/json" {
var params whatever.Params
c.Env["GojisonDecodeError"] = json.NewDecoder(r.Body).Decode(¶ms)
c.Env["Params"] = params
}
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}