-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhandler.go
56 lines (44 loc) · 1.05 KB
/
handler.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
package main
import (
"log"
"net/http"
)
type router interface {
Get(string) (http.Handler, bool)
Set(string, *http.Request) error
Del(string) bool
}
func getHandler(resources router) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
path := req.URL.String()
e, ok := resources.Get(path)
if !ok {
rw.WriteHeader(http.StatusNotFound)
return
}
e.ServeHTTP(rw, req)
}
}
func putHandler(resources router) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
path := req.URL.String()
if err := resources.Set(path, req); err != nil {
log.Panic(err)
}
e, _ := resources.Get(path)
e.ServeHTTP(rw, req)
}
}
func deleteHandler(resources router) http.HandlerFunc {
return func(rw http.ResponseWriter, req *http.Request) {
path := req.URL.String()
if ok := resources.Del(path); !ok {
rw.WriteHeader(http.StatusNotFound)
return
}
rw.WriteHeader(http.StatusNoContent)
}
}
func optionsHandler(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusNoContent)
}