@@ -18,35 +18,38 @@ import Foundation
18
18
19
19
@main
20
20
struct HttpApiLambda : LambdaHandler {
21
- init ( ) { }
22
- init ( context: LambdaInitializationContext ) async throws {
23
- context. logger. info (
24
- " Log Level env var : \( ProcessInfo . processInfo. environment [ " LOG_LEVEL " ] ?? " info " ) " )
25
- }
26
-
27
- // the return value must be either APIGatewayV2Response or any Encodable struct
28
- func handle( _ event: APIGatewayV2Request , context: AWSLambdaRuntimeCore . LambdaContext ) async throws -> APIGatewayV2Response {
29
-
30
- var header = HTTPHeaders ( )
31
- do {
32
- context. logger. debug ( " HTTP API Message received " )
33
-
34
- header [ " content-type " ] = " application/json "
35
-
36
- // echo the request in the response
37
- let data = try JSONEncoder ( ) . encode ( event)
38
- let response = String ( decoding: data, as: UTF8 . self)
39
-
40
- // if you want control on the status code and headers, return an APIGatewayV2Response
41
- // otherwise, just return any Encodable struct, the runtime will wrap it for you
42
- return APIGatewayV2Response ( statusCode: . ok, headers: header, body: response)
43
-
44
- } catch {
45
- // should never happen as the decoding was made by the runtime
46
- // when the input event is malformed, this function is not even called
47
- header [ " content-type " ] = " text/plain "
48
- return APIGatewayV2Response ( statusCode: . badRequest, headers: header, body: " \( error. localizedDescription) " )
21
+ init ( ) { }
22
+ init ( context: LambdaInitializationContext ) async throws {
23
+ context. logger. info (
24
+ " Log Level env var : \( ProcessInfo . processInfo. environment [ " LOG_LEVEL " ] ?? " info " ) " )
25
+ }
26
+
27
+ // the return value must be either APIGatewayV2Response or any Encodable struct
28
+ func handle( _ event: APIGatewayV2Request , context: AWSLambdaRuntimeCore . LambdaContext )
29
+ async throws -> APIGatewayV2Response
30
+ {
31
+
32
+ var header = HTTPHeaders ( )
33
+ do {
34
+ context. logger. debug ( " HTTP API Message received " )
35
+
36
+ header [ " content-type " ] = " application/json "
37
+
38
+ // echo the request in the response
39
+ let data = try JSONEncoder ( ) . encode ( event)
40
+ let response = String ( decoding: data, as: UTF8 . self)
41
+
42
+ // if you want control on the status code and headers, return an APIGatewayV2Response
43
+ // otherwise, just return any Encodable struct, the runtime will wrap it for you
44
+ return APIGatewayV2Response ( statusCode: . ok, headers: header, body: response)
45
+
46
+ } catch {
47
+ // should never happen as the decoding was made by the runtime
48
+ // when the input event is malformed, this function is not even called
49
+ header [ " content-type " ] = " text/plain "
50
+ return APIGatewayV2Response (
51
+ statusCode: . badRequest, headers: header, body: " \( error. localizedDescription) " )
49
52
50
- }
51
53
}
54
+ }
52
55
}
0 commit comments