-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
79 lines (65 loc) · 2.34 KB
/
http.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package requiem
import (
"encoding/json"
"io"
"net/http"
"reflect"
"github.com/gorilla/mux"
)
// HTTPContext provides utility functions for HTTP requests/responses
type HTTPContext struct {
Response http.ResponseWriter
Request *http.Request
Body interface{}
attributes map[string]interface{}
}
// SendJSON converts the given interface into JSON and writes to the response.
func (ctx *HTTPContext) SendJSON(v interface{}) {
SendJSON(ctx.Response, v)
}
// SendStatus writes the given HTTP status code into the response.
func (ctx *HTTPContext) SendStatus(s int) {
SendStatus(ctx.Response, s)
}
// SendJSONWithStatus writes a JSON response body to the response, along with the specified status code.
func (ctx *HTTPContext) SendJSONWithStatus(v interface{}, s int) {
SendJSONWithStatus(ctx.Response, v, s)
}
// GetParam obtains the given parameter key from the request parameters.
func (ctx *HTTPContext) GetParam(p string) string {
return mux.Vars(ctx.Request)[p]
}
// GetAttribute returns the context-scoped value for the given key
func (ctx *HTTPContext) GetAttribute(key string) interface{} {
return ctx.attributes[key]
}
// SetAttribute sets the context-scoped value for the given key
func (ctx *HTTPContext) SetAttribute(key string, attr interface{}) {
ctx.attributes[key] = attr
}
// ReadJSON decodes the provided stream into the given interface.
func ReadJSON(r io.Reader, v interface{}) interface{} {
t := reflect.TypeOf(v)
o := reflect.New(t).Interface()
json.NewDecoder(r).Decode(o)
return o
}
// SendJSON converts the given interface into JSON and writes to the provided stream.
func SendJSON(w http.ResponseWriter, v interface{}) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(v)
}
// SendStatus writes the given HTTP status code into the provided response stream.
func SendStatus(w http.ResponseWriter, s int) {
w.WriteHeader(s)
}
// SendJSONWithStatus writes a JSON response body to the provided stream, along with the specified status code.
func SendJSONWithStatus(w http.ResponseWriter, v interface{}, s int) {
w.Header().Add("Content-Type", "application/json")
json, _ := json.Marshal(v)
w.WriteHeader(s)
w.Write(json)
}
// HTTPInterceptor allows for pre-processing request handlers
// ex. an authentication interceptor could verify a user session
type HTTPInterceptor func(ctx HTTPContext) bool