-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
40 lines (34 loc) · 1.38 KB
/
context.go
File metadata and controls
40 lines (34 loc) · 1.38 KB
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
package lambada
import (
"context"
"net/http"
)
type contextKeyType struct{}
var contextKey = contextKeyType{}
// GetRequest returns the original API Gateway request which issued the http.Request.
// The returned Request value contains both API Gateway V1 and V2 data, but the used fields depend on the actual
// API Gateway version used.
//
// When no API Gateway request is attached to the http.Request, this function returns nil.
func GetRequest(r *http.Request) *Request {
return GetRequestFromContext(r.Context())
}
// GetRequestFromContext returns the original API Gateway request stored in ctx.
// The returned Request value contains both API Gateway V1 and V2 data, but the used fields depend on the actual
// API Gateway version used.
//
// When no API Gateway request is attached to the http.Request, this function returns nil.
func GetRequestFromContext(ctx context.Context) *Request {
if res, ok := ctx.Value(contextKey).(*Request); ok {
return res
}
return nil
}
// WithRequest returns a new context.Context with the given Request attached.
// The returned context should then be attached to an http.Request.
//
// There's usually no need to call this function directly, as all the work is done by lambada itself. However, it may
// be useful for testing purposes.
func WithRequest(ctx context.Context, req *Request) context.Context {
return context.WithValue(ctx, contextKey, req)
}