-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
151 lines (136 loc) · 3.87 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// TODO: Add SSL support
// Reference:
// https://gist.github.com/michaljemala/d6f4e01c4834bf47a9c4
package gogoapi
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
)
type API struct {
router *mux.Router
wrapperFuncs []WrapperFunc
methodNotAllowed HandlerFunc
}
func NewAPI(wrapperFuncs []WrapperFunc) *API {
router := mux.NewRouter().StrictSlash(true)
api := API{router, wrapperFuncs, nil}
api.methodNotAllowed = api.WrapHandler(MethodNotAllowed, wrapperFuncs)
wrappedPageNotFound := api.WrapHandler(PageNotFound, wrapperFuncs)
router.NotFoundHandler = api.HttpRequestHandler(wrappedPageNotFound)
return &api
}
func (api *API) HttpRequestHandler(
inner HandlerFunc,
) http.HandlerFunc {
if nil == inner {
inner = MethodNotAllowed
}
handler := func(
httpResponse http.ResponseWriter,
httpRequest *http.Request,
) {
status, responseObject, header := inner(httpRequest)
if _, supported := responseObject.(StatusResponse); !supported {
responseObject = JSONData{status, responseObject}
}
jsonResponse, err := json.MarshalIndent(responseObject, "", " ")
if nil != err {
header = nil
status = http.StatusInternalServerError
message := fmt.Sprintf(
"{\n \"status\": %d,\n \"error\": \"Internal server error. %s\"\n}",
status,
err.Error(),
)
jsonResponse = []byte(message)
}
for key, values := range header {
for _, value := range values {
httpResponse.Header().Add(key, value)
}
}
httpResponse.Header().Set(
"Content-Type", "application/json; charset=UTF-8",
)
httpResponse.WriteHeader(status)
httpResponse.Write(jsonResponse)
httpResponse.Write([]byte("\n"))
}
return handler
}
func (api *API) AddRoute(
method, path string,
inner HandlerFunc,
) {
handler := api.HttpRequestHandler(inner)
api.router.Methods(method).Path(path).Handler(handler)
}
func (api *API) WrapHandler(
handler HandlerFunc,
wrapperFuncs []WrapperFunc,
) HandlerFunc {
if nil == wrapperFuncs || 0 == len(wrapperFuncs) {
return handler
}
// every layer is a HandlerFunc that calls a WrapperFunc
for _, nextWrapperFunc := range wrapperFuncs {
wrapperFunc := nextWrapperFunc
innerHandler := handler
handler = func(
httpRequest *http.Request,
) (int, interface{}, http.Header) {
return wrapperFunc(httpRequest, innerHandler)
}
}
return handler
}
func (api *API) AddResource(
resource interface{},
path string,
resourceWrapperFuncs []WrapperFunc,
) {
wrapperFuncs := append(resourceWrapperFuncs, api.wrapperFuncs...)
var handler HandlerFunc
if resource, supported := resource.(GetResource); supported {
handler = api.WrapHandler(resource.Get, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(GET, path, handler)
if resource, supported := resource.(PostResource); supported {
handler = api.WrapHandler(resource.Post, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(POST, path, handler)
if resource, supported := resource.(PutResource); supported {
handler = api.WrapHandler(resource.Put, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(PUT, path, handler)
if resource, supported := resource.(DeleteResource); supported {
handler = api.WrapHandler(resource.Delete, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(DELETE, path, handler)
if resource, supported := resource.(HeadResource); supported {
handler = api.WrapHandler(resource.Head, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(HEAD, path, handler)
if resource, supported := resource.(PatchResource); supported {
handler = api.WrapHandler(resource.Patch, wrapperFuncs)
} else {
handler = api.methodNotAllowed
}
api.AddRoute(PATCH, path, handler)
}
func (api *API) Start(host string, port int) error {
networkAddress := fmt.Sprintf("%s:%d", host, port)
return http.ListenAndServe(networkAddress, api.router)
}