5
5
"encoding/json"
6
6
"fmt"
7
7
"github.com/aws/aws-lambda-go/events"
8
- "github.com/aws/aws-lambda-go/lambda"
9
- "github.com/aws/aws-lambda-go/lambda/handlertrace"
8
+ "github.com/aws/aws-lambda-go/lambdaurl"
10
9
"github.com/aws/aws-sdk-go-v2/aws"
11
10
"github.com/aws/aws-sdk-go-v2/config"
12
11
"github.com/aws/aws-sdk-go/aws/session"
@@ -46,24 +45,28 @@ func WithoutNonHTTPEventPassThrough() LambdaHandlerOption {
46
45
}
47
46
48
47
type LambdaHandler struct {
49
- httpHandler http.Handler
50
- sessProv SDKSessionProvider
51
- sess * session.Session
52
- confProv SDKConfigProvider
53
- conf * aws.Config
54
- apiGW APIGatewayManagementAPI
55
- wsPathPrefix string
56
- nonHTTPEventPath string
48
+ httpHandler http.Handler
49
+ sessProv SDKSessionProvider
50
+ sess * session.Session
51
+ confProv SDKConfigProvider
52
+ conf * aws.Config
53
+ apiGW APIGatewayManagementAPI
54
+ wsPathPrefix string
55
+ nonHTTPEventPath string
56
+ invokeLambdaWithStream func (ctx context.Context , request * events.LambdaFunctionURLRequest ) (* events.LambdaFunctionURLStreamingResponse , error )
57
57
}
58
58
59
- func NewLambdaHandlerWithOption (h http.Handler , options []interface {}) lambda.Handler {
59
+ type HandlerFunc func (ctx context.Context , payload json.RawMessage ) (res any , err error )
60
+
61
+ func NewLambdaHandlerWithOption (h http.Handler , options []interface {}) * LambdaHandler {
60
62
handler := & LambdaHandler {
61
63
httpHandler : h ,
62
64
confProv : func (ctx context.Context ) (aws.Config , error ) {
63
65
return config .LoadDefaultConfig (ctx )
64
66
},
65
- wsPathPrefix : DefaultWebsocketPathPrefix ,
66
- nonHTTPEventPath : DefaultNonHTTPEventPath ,
67
+ wsPathPrefix : DefaultWebsocketPathPrefix ,
68
+ nonHTTPEventPath : DefaultNonHTTPEventPath ,
69
+ invokeLambdaWithStream : lambdaurl .Wrap (h ),
67
70
}
68
71
69
72
for _ , opt := range options {
@@ -75,7 +78,7 @@ func NewLambdaHandlerWithOption(h http.Handler, options []interface{}) lambda.Ha
75
78
return handler
76
79
}
77
80
78
- func NewLambdaHandler (h http.Handler ) lambda. Handler {
81
+ func NewLambdaHandler (h http.Handler ) * LambdaHandler {
79
82
return NewLambdaHandlerWithOption (h , nil )
80
83
}
81
84
@@ -163,7 +166,7 @@ func (l *LambdaHandler) InvokeWebsocketAPI(ctx context.Context, request *events.
163
166
164
167
routeKey := request .RequestContext .RouteKey
165
168
166
- if routeKey == "$connect" || routeKey == "$disconnect" {
169
+ if routeKey == "$connect" || routeKey == "$disconnect" || WebsocketResponseMode == "return" {
167
170
w := NewResponseWriter ()
168
171
l .httpHandler .ServeHTTP (w , req )
169
172
return RESTAPITargetResponse (w , multiValue )
@@ -178,13 +181,9 @@ func (l *LambdaHandler) InvokeWebsocketAPI(ctx context.Context, request *events.
178
181
}
179
182
}
180
183
181
- func (l * LambdaHandler ) Invoke (ctx context.Context , payload []byte ) ([]byte , error ) {
182
- trace := handlertrace .FromContext (ctx )
183
-
184
+ func (l * LambdaHandler ) Invoke (ctx context.Context , payload json.RawMessage ) (res any , err error ) {
184
185
var (
185
186
checker integrationTypeChecker
186
- res interface {}
187
- err error
188
187
)
189
188
190
189
if err = json .Unmarshal (payload , & checker ); err != nil {
@@ -196,54 +195,43 @@ func (l *LambdaHandler) Invoke(ctx context.Context, payload []byte) ([]byte, err
196
195
if err := json .Unmarshal (payload , event ); err != nil {
197
196
return nil , err
198
197
}
199
- if trace .RequestEvent != nil {
200
- trace .RequestEvent (ctx , payload )
201
- }
202
198
res , err = l .InvokeRESTAPI (ctx , event )
203
199
case APIGatewayHTTPIntegration :
204
200
event := & events.APIGatewayV2HTTPRequest {}
205
201
if err := json .Unmarshal (payload , event ); err != nil {
206
202
return nil , err
207
203
}
208
- if trace .RequestEvent != nil {
209
- trace .RequestEvent (ctx , payload )
210
- }
211
204
res , err = l .InvokeHTTPAPI (ctx , event )
212
205
case ALBTargetGroupIntegration :
213
206
event := & events.ALBTargetGroupRequest {}
214
207
if err := json .Unmarshal (payload , event ); err != nil {
215
208
return nil , err
216
209
}
217
- if trace .RequestEvent != nil {
218
- trace .RequestEvent (ctx , payload )
219
- }
220
210
res , err = l .InvokeALBTargetGroup (ctx , event )
221
211
case APIGatewayWebsocketIntegration :
222
212
event := & events.APIGatewayWebsocketProxyRequest {}
223
213
if err := json .Unmarshal (payload , event ); err != nil {
224
214
return nil , err
225
215
}
226
216
res , err = l .InvokeWebsocketAPI (ctx , event )
217
+ case LambdaFunctionURLIntegration :
218
+ if LambdaInvokeMode == "response_stream" {
219
+ event := & events.LambdaFunctionURLRequest {}
220
+ if err := json .Unmarshal (payload , event ); err != nil {
221
+ return nil , err
222
+ }
223
+ res , err = l .invokeLambdaWithStream (ctx , event )
224
+ } else {
225
+ event := & events.APIGatewayV2HTTPRequest {}
226
+ if err := json .Unmarshal (payload , event ); err != nil {
227
+ return nil , err
228
+ }
229
+ res , err = l .InvokeHTTPAPI (ctx , event )
230
+ }
227
231
default :
228
232
res , err = l .HandleNonHTTPEvent (ctx , payload , "application/json" )
229
233
}
230
234
}
231
235
232
- if err != nil {
233
- return nil , err
234
- }
235
-
236
- if trace .ResponseEvent != nil {
237
- trace .ResponseEvent (ctx , res )
238
- }
239
-
240
- if b , ok := res .([]byte ); ok {
241
- return b , nil
242
- } else {
243
- if responseBytes , err := json .Marshal (res ); err != nil {
244
- return nil , err
245
- } else {
246
- return responseBytes , nil
247
- }
248
- }
236
+ return res , err
249
237
}
0 commit comments