-
Notifications
You must be signed in to change notification settings - Fork 1
/
decorator.go
33 lines (28 loc) · 928 Bytes
/
decorator.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
)
func IDShouldBeInt(h httprouter.Handle, name string) httprouter.Handle {
return CommonHeaders(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
idParam := ps.ByName("yamaId")
_, err := strconv.Atoi(idParam)
if err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(500)
if err := json.NewEncoder(w).Encode(err); err != nil {
return
}
return
}
h(w, r, ps)
}, name)
}
func CommonHeaders(h httprouter.Handle, name string) httprouter.Handle {
return Logging(func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
h(w, r, ps)
}, name)
}