This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolam.go
343 lines (284 loc) · 7.72 KB
/
golam.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package golam
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"net/http"
"net/url"
"strconv"
"strings"
)
func New() (g *Golam) {
g = &Golam{
isLambdaRuntime: isLambdaRuntime(),
NotFoundHandler: DefaultNotFound,
}
if g.isLambdaRuntime {
g.router = newLambdaRouter()
g.LambdaHandler = &defaultLambdaHandler{golam: g}
g.start = func() error {
lambda.Start(g.LambdaHandler)
return nil
}
} else {
g.router = newLocalRouter()
g.LocalHandler = &defaultHttpHandler{golam: g}
g.start = func() error {
return http.ListenAndServe(g.LocalAddr, g.LocalHandler)
}
}
return
}
var (
_ http.Handler = (*defaultHttpHandler)(nil)
_ lambda.Handler = (*defaultLambdaHandler)(nil)
)
type (
defaultHttpHandler struct {
golam *Golam
}
defaultLambdaHandler struct {
golam *Golam
}
HandlerFunc func(c Context) error
Golam struct {
LocalAddr string
LocalHandler http.Handler
LambdaHandler lambda.Handler
NotFoundHandler HandlerFunc
// TODO
//Logger log.Logger
isLambdaRuntime bool
start func() error
preMiddleware []MiddlewareFunc
middleware []MiddlewareFunc
router Router
}
)
func (d *defaultHttpHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
method := request.Method
reqPath := request.URL.Path
ctxImpl := &contextImpl{
request: request,
response: NewResponse(
NewResponseHTTPAdapter(writer),
),
path: reqPath,
query: request.URL.Query(),
golam: d.golam,
primalRequestHTTP: request,
}
r := d.golam.router.FindRoute(reqPath)
if r != nil {
var params *[]routeParam
handler := r.handlers.getHandler(method)
if handler != nil {
ctxImpl.handler = handler
params = r.params.getParamsInfo(method)
} else {
ctxImpl.handler = r.handlers.getHandler("")
params = r.params.getParamsInfo("")
}
if params != nil && len(*params) > 0 {
ctxImpl.pathParams = make(PathParams)
parts := strings.Split(reqPath, "/")
for _, p := range *params {
if p.index >= len(parts) {
// error?
continue
}
if p.isGreedy {
ctxImpl.pathParams[p.key] = PathParam{
Key: p.key,
Value: strings.Join(parts[p.index:], "/"),
}
break
}
ctxImpl.pathParams[p.key] = PathParam{
Key: p.key,
Value: parts[p.index],
}
}
}
}
if ctxImpl.handler == nil {
ctxImpl.handler = d.golam.NotFoundHandler
} else {
ctxImpl.handler = wrapMiddleware(ctxImpl.handler, append(d.golam.preMiddleware, d.golam.middleware...)...)
}
// TODO error handle
switch err := ctxImpl.handler(ctxImpl); err {
case nil:
err = ctxImpl.Response().Commit()
if err != nil {
panic(err) // unreachable code
}
default:
// TODO error handler
}
}
func (d *defaultLambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
var lReq events.APIGatewayV2HTTPRequest
err := json.Unmarshal(payload, &lReq)
if err != nil {
//TODO: logging
return nil, err
}
req, err := newHTTPRequestFromAPIGatewayV2HTTPRequest(ctx, &lReq)
if err != nil {
//TODO: logging
return nil, err
}
var response events.APIGatewayV2HTTPResponse
ctxImpl := &contextImpl{
request: req,
response: NewResponse(
NewResponseLambdaAdapter(&response),
),
path: lReq.RequestContext.HTTP.Path,
golam: d.golam,
primalRequestLambda: &lReq,
}
ctxImpl.query, _ = url.ParseQuery(lReq.RawQueryString)
pathKey := lReq.RouteKey[strings.Index(lReq.RouteKey, " ")+1:]
r := d.golam.Router().FindRoute(pathKey)
if r != nil {
handler := r.handlers.getHandler(req.Method)
if handler != nil {
ctxImpl.handler = handler
} else {
ctxImpl.handler = r.handlers.getHandler("")
}
if len(lReq.PathParameters) > 0 {
ctxImpl.pathParams = make(PathParams)
for k, v := range lReq.PathParameters {
ctxImpl.pathParams[k] = PathParam{
Key: k,
Value: v,
}
}
}
}
if ctxImpl.handler == nil {
ctxImpl.handler = d.golam.NotFoundHandler
} else {
ctxImpl.handler = wrapMiddleware(ctxImpl.handler, append(d.golam.preMiddleware, d.golam.middleware...)...)
}
switch err = ctxImpl.handler(ctxImpl); err {
case nil:
err = ctxImpl.Response().Commit()
if err != nil {
//TODO: logging
return nil, err
}
default:
// TODO error handler
}
return json.Marshal(response)
}
func (g *Golam) StartWithLocalAddr(localAddr string) error {
g.LocalAddr = localAddr
return g.Start()
}
func (g *Golam) Start() error {
if g.start == nil {
return errors.New("must setup start")
}
return g.start()
}
func (g *Golam) Router() Router {
return g.router
}
func (g *Golam) Pre(middleware ...MiddlewareFunc) {
g.preMiddleware = append(g.preMiddleware, middleware...)
}
func (g *Golam) Use(middleware ...MiddlewareFunc) {
g.middleware = append(g.middleware, middleware...)
}
func (g *Golam) Any(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().Any(path, handler, middleware...)
}
func (g *Golam) GET(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().GET(path, handler, middleware...)
}
func (g *Golam) HEAD(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().HEAD(path, handler, middleware...)
}
func (g *Golam) POST(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().POST(path, handler, middleware...)
}
func (g *Golam) PUT(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().PUT(path, handler, middleware...)
}
func (g *Golam) PATCH(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().PATCH(path, handler, middleware...)
}
func (g *Golam) DELETE(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().DELETE(path, handler, middleware...)
}
func (g *Golam) CONNECT(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().CONNECT(path, handler, middleware...)
}
func (g *Golam) OPTIONS(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().OPTIONS(path, handler, middleware...)
}
func (g *Golam) TRACE(path string, handler HandlerFunc, middleware ...MiddlewareFunc) {
g.Router().TRACE(path, handler, middleware...)
}
const defaultNotFoundResponseData = "{\"message\":\"Not Found\"}"
func DefaultNotFound(ctx Context) error {
return ctx.JSONBytes(http.StatusNotFound, []byte(defaultNotFoundResponseData))
}
func newHTTPRequestFromAPIGatewayV2HTTPRequest(ctx context.Context, from *events.APIGatewayV2HTTPRequest) (req *http.Request, err error) {
rawProtocol := from.RequestContext.HTTP.Protocol
protoDiv := strings.Index(rawProtocol, "/")
if protoDiv == -1 {
return
}
proto := rawProtocol[protoDiv+1:]
protoDiv = strings.Index(proto, ".")
if protoDiv == -1 {
return
}
major, err := strconv.Atoi(proto[:protoDiv])
if err != nil {
return
}
minor, err := strconv.Atoi(proto[protoDiv+1:])
if err != nil {
return
}
var body []byte
if from.IsBase64Encoded {
body, err = base64.StdEncoding.DecodeString(from.Body)
if err != nil {
return
}
} else {
body = []byte(from.Body)
}
header := getHeaderFromAPIGatewayV2HTTPRequest(from)
var rawURL strings.Builder
rawURL.WriteString(getSchemeFromHeader(header))
rawURL.WriteString("://")
rawURL.WriteString(header.Get(HeaderHost))
rawURL.WriteString(from.RawPath)
if len(from.RawQueryString) > 0 {
rawURL.WriteRune('?')
rawURL.WriteString(from.RawQueryString)
}
req, err = http.NewRequestWithContext(ctx, from.RequestContext.HTTP.Method, rawURL.String(), bytes.NewReader(body))
if err != nil {
return
}
req.Proto = rawProtocol
req.ProtoMajor = major
req.ProtoMinor = minor
req.RemoteAddr = from.RequestContext.HTTP.SourceIP
req.Header = header
return
}