|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import AWSLambdaRuntime |
| 16 | +import NIO |
| 17 | + |
| 18 | +// MARK: - Run Lambda |
| 19 | + |
| 20 | +Lambda.run(APIGatewayProxyLambda()) |
| 21 | + |
| 22 | +// MARK: - Handler, Request and Response |
| 23 | + |
| 24 | +// FIXME: Use proper Event abstractions once added to AWSLambdaRuntime |
| 25 | +struct APIGatewayProxyLambda: EventLoopLambdaHandler { |
| 26 | + public typealias In = APIGatewayRequest |
| 27 | + public typealias Out = APIGatewayResponse |
| 28 | + |
| 29 | + public func handle(context: Lambda.Context, payload: APIGatewayRequest) -> EventLoopFuture<APIGatewayResponse> { |
| 30 | + context.logger.debug("hello, api gateway!") |
| 31 | + return context.eventLoop.makeSucceededFuture(APIGatewayResponse(statusCode: 200, |
| 32 | + headers: nil, |
| 33 | + multiValueHeaders: nil, |
| 34 | + body: "hello, world!", |
| 35 | + isBase64Encoded: false)) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +struct APIGatewayRequest: Codable { |
| 40 | + let resource: String |
| 41 | + let path: String |
| 42 | + let httpMethod: String? |
| 43 | + let headers: [String: String]? |
| 44 | + let multiValueHeaders: [String: [String]]? |
| 45 | + let queryStringParameters: [String: String]? |
| 46 | + let multiValueQueryStringParameters: [String: [String]]? |
| 47 | + let pathParameters: [String: String]? |
| 48 | + let stageVariables: [String: String]? |
| 49 | + let requestContext: Context? |
| 50 | + let body: String? |
| 51 | + let isBase64Encoded: Bool? |
| 52 | + |
| 53 | + struct Context: Codable { |
| 54 | + let accountId: String? |
| 55 | + let resourceId: String? |
| 56 | + let stage: String? |
| 57 | + let requestId: String? |
| 58 | + let identity: Identity? |
| 59 | + let resourcePath: String? |
| 60 | + let httpMethod: String? |
| 61 | + let apiId: String |
| 62 | + } |
| 63 | + |
| 64 | + struct Identity: Codable { |
| 65 | + let cognitoIdentityPoolId: String? |
| 66 | + let accountId: String? |
| 67 | + let cognitoIdentityId: String? |
| 68 | + let caller: String? |
| 69 | + let apiKey: String? |
| 70 | + let sourceIp: String? |
| 71 | + let cognitoAuthenticationType: String? |
| 72 | + let cognitoAuthenticationProvider: String? |
| 73 | + let userArn: String? |
| 74 | + let userAgent: String? |
| 75 | + let user: String? |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +struct APIGatewayResponse: Codable { |
| 80 | + let statusCode: Int |
| 81 | + let headers: [String: String]? |
| 82 | + let multiValueHeaders: [String: [String]]? |
| 83 | + let body: String? |
| 84 | + let isBase64Encoded: Bool? |
| 85 | +} |
0 commit comments