/
http.go
71 lines (64 loc) · 1.7 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
package api
import (
"bytes"
"encoding/json"
"net/http"
"strings"
"golang.org/x/net/context"
"google.golang.org/appengine/log"
)
// decode decodes the request body into v and calls
// its OK() function to validate the object.
func decode(r *http.Request, v interface{}) error {
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
return err
}
if valid, ok := v.(interface {
OK() error
}); ok {
err = valid.OK()
if err != nil {
return err
}
}
return nil
}
func respond(ctx context.Context, w http.ResponseWriter, r *http.Request, v interface{}, code int) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(v)
if err != nil {
respondErr(ctx, w, r, err, http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
_, err = buf.WriteTo(w)
if err != nil {
log.Errorf(ctx, "respond: %s", err)
}
}
func respondErr(ctx context.Context, w http.ResponseWriter, r *http.Request, err error, code int) {
errObj := struct {
Error string `json:"error"`
}{Error: err.Error()}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
err = json.NewEncoder(w).Encode(errObj)
if err != nil {
log.Errorf(ctx, "respondErr: %s", err)
}
}
// pathParams parses the URL.Path of the Request with the given
// pattern, and extracts the value for each segment into a map.
func pathParams(r *http.Request, pattern string) map[string]string {
params := map[string]string{}
pathSegs := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
for i, seg := range strings.Split(strings.Trim(pattern, "/"), "/") {
if i > len(pathSegs)-1 {
return params
}
params[seg] = pathSegs[i]
}
return params
}