-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
105 lines (84 loc) · 2.03 KB
/
api.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"encoding/json"
"errors"
"io"
"log"
"net/http"
"strings"
)
type route struct {
method string
pattern string
handler http.Handler
}
func (r *route) match(method, path string) bool {
if r.method != method {
return false
}
splitPattern := strings.Split(r.pattern, "/")
splitPath := strings.Split(path, "/")
if len(splitPattern) != len(splitPath) {
return false
}
for i, p1 := range splitPattern {
p2 := splitPath[i]
if !strings.HasPrefix(":", p1) && p1 != p2 {
return false
}
}
return true
}
type API struct {
routes []route
}
func (api *API) Handle(method string, pattern string, handler http.Handler) {
r := route{
method: method,
pattern: pattern,
handler: handler,
}
api.routes = append(api.routes, r)
}
func (api *API) HandleFunc(method string, pattern string, handler http.HandlerFunc) {
api.Handle(method, pattern, handler)
}
func (api *API) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
method := request.Method
path := request.URL.Path
for _, r := range api.routes {
if r.match(method, path) {
r.handler.ServeHTTP(writer, request)
return
}
}
api.RespondError(writer, http.StatusNotFound, errors.New("404 page not found"))
return
}
func (api *API) BodyMapping(request *http.Request, v interface{}) error {
body, err := io.ReadAll(request.Body)
if err != nil {
return err
}
err = json.Unmarshal(body, v)
return err
}
func (api *API) RespondError(writer http.ResponseWriter, status int, err error) {
log.Printf("error: %v\n", err)
body := map[string]string{"message": err.Error()}
api.RespondJSON(writer, status, body)
}
func (api *API) RespondJSON(writer http.ResponseWriter, status int, body interface{}) {
res, err := json.Marshal(body)
if err != nil {
api.RespondError(writer, http.StatusInternalServerError, err)
return
}
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(status)
_, err = writer.Write(res)
if err != nil {
api.RespondError(writer, http.StatusInternalServerError, err)
return
}
}