-
Notifications
You must be signed in to change notification settings - Fork 15
/
ctx.go
37 lines (30 loc) · 1.31 KB
/
ctx.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
// Copyright 2021-2024 Nokia
// Licensed under the BSD 3-Clause License.
// SPDX-License-Identifier: BSD-3-Clause
/* There are numerous Lambda context functions to query request data and set response.
* It would be easy to expose http.ResponseWriter and *http.Request.
* Here, instead, separate functions are defined. That might help if a serverless environment is to be supported later.
*/
package restful
import (
"context"
"net/http"
"github.com/nokia/restful/lambda"
)
// NewRequestCtx adds request related data to r.Context().
// You may use this at traditional http handler functions, and that is what happens at Lambda functions automatically.
// Returns new derived context. That can be used at client functions, silently propagating tracing headers.
//
// E.g. ctx := NewRequestCtx(w, r)
func NewRequestCtx(w http.ResponseWriter, r *http.Request) context.Context {
return lambda.NewRequestCtx(w, r)
}
// L returns lambda-related data from context.
func L(ctx context.Context) *lambda.Lambda {
return lambda.L(ctx)
}
// AddLambdaToContext returns the context extended with value of the Lambda.
// Useful if a new goroutine is created with new context, but it is related to the original Context.
func AddLambdaToContext(ctx context.Context, l *lambda.Lambda) context.Context {
return lambda.AddLambdaToContext(ctx, l)
}