-
Notifications
You must be signed in to change notification settings - Fork 12
/
wrapper.go
49 lines (41 loc) · 1.28 KB
/
wrapper.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
package main
import (
"fmt"
"net/http"
)
// HTTPHandlerWithMethod only allows the given
// method to a handler, else returning an
// http.StatusMethodNotAllowed status code as
// well as an error
func HTTPHandlerWithMethod(method string, h http.HandlerFunc) http.HandlerFunc {
return func(r http.ResponseWriter, req *http.Request) {
if req.Method != method {
r.Header().Add("Content-Type", "application/json")
r.WriteHeader(http.StatusMethodNotAllowed)
r.Write([]byte(fmt.Sprintf(`{"message": "Given method not allowed", "method": %v}`, req.Method)))
return
}
h(r, req)
}
}
// Post only allows POST requests to a
// handler
func Post(h http.HandlerFunc) http.HandlerFunc {
return HTTPHandlerWithMethod("POST", h)
}
// Get only allows GET requests to a handler
func Get(h http.HandlerFunc) http.HandlerFunc {
return HTTPHandlerWithMethod("GET", h)
}
// Patch only allows PATCH requests to a handler
func Patch(h http.HandlerFunc) http.HandlerFunc {
return HTTPHandlerWithMethod("PATCH", h)
}
// Delete only allows DELETE requests to a handler
func Delete(h http.HandlerFunc) http.HandlerFunc {
return HTTPHandlerWithMethod("DELETE", h)
}
// Put only allows PUT requests to a handler
func Put(h http.HandlerFunc) http.HandlerFunc {
return HTTPHandlerWithMethod("PUT", h)
}