-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.go
More file actions
66 lines (53 loc) · 2.05 KB
/
request.go
File metadata and controls
66 lines (53 loc) · 2.05 KB
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
package rest
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/thepkg/rest/request"
)
var (
handlerRegistry = request.HandlerRegistry{}
)
func init() {
handlerRegistry = request.NewHandlerRegistry()
}
// HEAD registers the HEAD HTTP method handler function for the given pattern.
func HEAD(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("HEAD", pattern, handler)
}
// GET registers the GET HTTP method handler function for the given pattern.
func GET(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("GET", pattern, handler)
}
// POST registers the POST HTTP method handler function for the given pattern.
func POST(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("POST", pattern, handler)
}
// PATCH registers the PATCH HTTP method handler function for the given pattern.
func PATCH(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("PATCH", pattern, handler)
}
// PUT registers the PUT HTTP method handler function for the given pattern.
func PUT(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("PUT", pattern, handler)
}
// DELETE registers the DELETE HTTP method handler function for the given pattern.
func DELETE(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("DELETE", pattern, handler)
}
// OPTIONS registers the OPTIONS HTTP method handler function for the given pattern.
func OPTIONS(pattern string, handler func(http.ResponseWriter, *http.Request)) {
handlerRegistry.Handle("OPTIONS", pattern, handler)
}
// MustUnmarshalBody performs read and json.Unmarshal body into data.
func MustUnmarshalBody(r *http.Request, data interface{}) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(fmt.Errorf("failed to read request body, error: %s", err))
}
err = json.Unmarshal(body, data)
if err != nil {
panic(fmt.Errorf("failed to unmarshal request body, error: %s", err))
}
}