在 AWS Lambda 中运行 Hummingbird
创建一个符合 LambdaFunction
协议的结构体。在 init
函数中设置你的应用程序:添加你的中间件,添加路由处理程序等等。
@main
struct MyHandler: LambdaFunction {
// define input and output
typealias Event = APIGatewayRequest
typealias Output = APIGatewayResponse
typealias Context = BasicLambdaRequestContext<APIGatewayRequest>
init(_ app: Application) {
app.middleware.add(LogRequestsMiddleware(.debug))
app.router.get("hello") { _, _ in
return "Hello"
}
}
}
Event
和 Output
类型定义你的输入和输出对象。 如果你使用 APIGateway
REST 接口来调用你的 Lambda,那么将它们分别设置为 APIGatewayRequest
和 APIGatewayResponse
。 如果你使用 APIGateway
HTML 接口,那么将它们分别设置为 APIGatewayV2Request
和 APIGatewayV2Response
。 如果你使用任何其他的 Event
/Output
类型,你将需要自己实现 request(context:application:from:)
和 output(from:)
方法。