Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions Hummingbird.docc/HummingbirdLambda/HummingbirdLambda.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,40 @@

Run Hummingbird inside an AWS Lambda.

Package that allows you to use the Hummingbird Router and Middleware inside an AWS Lambda.

## Usage

Create struct conforming to `LambdaFunction`. Setup your router in the `buildResponder` function: add routes, middleware etc and then return its responder.
Creating a Hummingbird Lambda is very similar to how you create a Hummingbird server application. You create your
router, create a ``LambdaFunction`` using the router and then run the lambda function.

```swift
@main
struct MyHandler: LambdaFunction {
typealias Event = APIGatewayRequest
typealias Output = APIGatewayResponse
typealias Context = BasicLambdaRequestContext<APIGatewayRequest>

init(context: LambdaInitializationContext) {}

/// build responder that will create a response from a request
func buildResponder() -> some Responder<Context> {
let router = Router(context: Context.self)
router.get("hello/{name}") { request, context in
let name = try context.parameters.require("name")
return "Hello \(name)"
}
return router.buildResponder()
}
typealias AppRequestContext = BasicLambdaRequestContext<APIGatewayV2Request>

// Create router and add a single route returning "Hello" and name in its body
let router = Router(context: AppRequestContext.self)
router.get("hello/{name}") { request, context in
let name = try context.parameters.require("name")
return "Hello \(name)"
}
// create APIGatewayV2 lambda using router and run.
let lambda = LambdaFunction(
router: router,
event: APIGatewayV2Request.self,
output: APIGatewayV2Response.self
)
try await lambda.runService()
```

The `Event` and `Output` types define your input and output objects. If you are using an `APIGateway` REST interface to invoke your Lambda then set these to `APIGateway.Request` and `APIGateway.Response` respectively. If you are using an `APIGateway` HTML interface then set these to `APIGateway.V2.Request` and `APIGateway.V2.Response`. The protocols ``APIGatewayLambdaFunction`` and ``APIGatewayV2LambdaFunction`` set these up for you.

If you are using any other `In`/`Out` types you will need to implement the `request(context:application:from:)` and `output(from:)` methods yourself.

## Topics

### Lambda protocols
### Lambda function

- ``LambdaFunction``
- ``LambdaFunctionProtocol``
- ``APIGatewayLambdaFunction``
- ``APIGatewayV2LambdaFunction``
- ``FunctionURLLambdaFunction``

### Request context

Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.package(url: "https://github.com/hummingbird-project/swift-jobs.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/swift-jobs-postgres.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/swift-jobs-redis.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/hummingbird-lambda.git", from: "2.0.0-rc.3"),
.package(url: "https://github.com/hummingbird-project/hummingbird-lambda.git", from: "2.0.0-rc.5"),
.package(url: "https://github.com/hummingbird-project/swift-mustache.git", from: "2.0.0"),
.package(url: "https://github.com/hummingbird-project/hummingbird-postgres.git", from: "1.0.0-rc"),
.package(url: "https://github.com/hummingbird-project/postgres-migrations.git", from: "1.0.0-rc"),
Expand Down