|
| 1 | +# ``BreezeLambdaWebHook`` |
| 2 | + |
| 3 | +@Metadata { |
| 4 | + @PageImage(purpose: icon, source: "wind") |
| 5 | +} |
| 6 | + |
| 7 | +## Overview |
| 8 | + |
| 9 | +BreezeLambdaWebHook is a Swift framework that simplifies the development of serverless webhook handlers for AWS Lambda. |
| 10 | + |
| 11 | +It provides a clean, type-safe interface for processing HTTP requests from API Gateway and returning appropriate responses. |
| 12 | + |
| 13 | +It allows you to define a handler that processes incoming webhook requests and returns appropriate responses using `AsyncHTTPClient` framework. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Key Features |
| 18 | + |
| 19 | +- Serverless Architecture - Built specifically for AWS Lambda with API Gateway integration |
| 20 | +- Webhook Handling: Processes incoming requests and returns responses |
| 21 | +- Swift Concurrency: Fully compatible with Swift's async/await model |
| 22 | +- Type Safety: Leverages Swift's type system with Codable support |
| 23 | +- It supports both GET and POST requests |
| 24 | +- Minimal Configuration - Focus on business logic rather than infrastructure plumbing |
| 25 | + |
| 26 | +## How it works |
| 27 | + |
| 28 | +The framework handles the underlying AWS Lambda event processing, allowing you to focus on implementing your webhook logic. When a request arrives through API Gateway: |
| 29 | + |
| 30 | +1. The Lambda function receives the API Gateway event |
| 31 | +2. BreezeLambdaWebHook deserializes the event into a Swift type |
| 32 | +3. The handler processes the request, performing any necessary business logic |
| 33 | +4. The handler returns a response, which is serialized back to the API Gateway format |
| 34 | + |
| 35 | +## Getting Started |
| 36 | + |
| 37 | +To create a webhook handler, implement the BreezeLambdaWebHookHandler protocol: |
| 38 | + |
| 39 | +```swift |
| 40 | +class MyWebhook: BreezeLambdaWebHookHandler { |
| 41 | + let handlerContext: HandlerContext |
| 42 | + |
| 43 | + required init(handlerContext: HandlerContext) { |
| 44 | + self.handlerContext = handlerContext |
| 45 | + } |
| 46 | + |
| 47 | + func handle(context: LambdaContext, event: APIGatewayV2Request) async -> APIGatewayV2Response { |
| 48 | + // Your webhook logic here |
| 49 | + return APIGatewayV2Response(with: "Success", statusCode: .ok) |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +Then, implement the `main.swift` file to run the Lambda function: |
| 55 | +```swift |
| 56 | +import BreezeLambdaWebHook |
| 57 | +import AWSLambdaEvents |
| 58 | +import AWSLambdaRuntime |
| 59 | +import AsyncHTTPClient |
| 60 | +import Logging |
| 61 | +import NIOCore |
| 62 | + |
| 63 | +@main |
| 64 | +struct BreezeDemoHTTPApplication { |
| 65 | + static func main() async throws { |
| 66 | + let app = BreezeLambdaWebHook<DemoLambdaHandler>(name: "BreezeDemoHTTPApplication") |
| 67 | + try await app.run() |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Example WebHook Handlers |
| 73 | + |
| 74 | +### GET WebHook example: |
| 75 | + |
| 76 | +If the parameter `github-user` is present in the query string, the value is extracted and used to get the content from GitHub, the content is returned to the response payload. |
| 77 | + |
| 78 | +```swift |
| 79 | +class GetWebHook: BreezeLambdaWebHookHandler { |
| 80 | + |
| 81 | + let handlerContext: HandlerContext |
| 82 | + |
| 83 | + required init(handlerContext: HandlerContext) { |
| 84 | + self.handlerContext = handlerContext |
| 85 | + } |
| 86 | + |
| 87 | + func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response { |
| 88 | + do { |
| 89 | + context.logger.info("event: \(event)") |
| 90 | + guard let params = event.queryStringParameters else { |
| 91 | + throw BreezeLambdaWebHookError.invalidRequest |
| 92 | + } |
| 93 | + if let user = params["github-user"] { |
| 94 | + let url = "https://github.com/\(user)" |
| 95 | + let request = HTTPClientRequest(url: url) |
| 96 | + let response = try await httpClient.execute(request, timeout: .seconds(3)) |
| 97 | + let bytes = try await response.body.collect(upTo: 1024 * 1024) // 1 MB Buffer |
| 98 | + let body = String(buffer: bytes) |
| 99 | + return APIGatewayV2Response(with: body, statusCode: .ok) |
| 100 | + } else { |
| 101 | + return APIGatewayV2Response(with: params, statusCode: .ok) |
| 102 | + } |
| 103 | + } catch { |
| 104 | + return APIGatewayV2Response(with: error, statusCode: .badRequest) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | +``` |
| 109 | + |
| 110 | +### PostWebHook example: |
| 111 | + |
| 112 | +If the parameter `github-user` is present in the JSON payload, the value is extracted and used to get the content from GitHub, the content is returned to the response payload. |
| 113 | + |
| 114 | +```swift |
| 115 | +struct PostWebHookRequest: Codable { |
| 116 | + let githubUser: String |
| 117 | + |
| 118 | + enum CodingKeys: String, CodingKey { |
| 119 | + case githubUser = "github-user" |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +class PostWebHook: BreezeLambdaWebHookHandler { |
| 124 | + |
| 125 | + let handlerContext: HandlerContext |
| 126 | + |
| 127 | + required init(handlerContext: HandlerContext) { |
| 128 | + self.handlerContext = handlerContext |
| 129 | + } |
| 130 | + |
| 131 | + func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response { |
| 132 | + do { |
| 133 | + context.logger.info("event: \(event)") |
| 134 | + let incomingRequest: PostWebHookRequest = try event.bodyObject() |
| 135 | + let url = "https://github.com/\(incomingRequest.githubUser)" |
| 136 | + let request = HTTPClientRequest(url: url) |
| 137 | + let response = try await httpClient.execute(request, timeout: .seconds(3)) |
| 138 | + let bytes = try await response.body.collect(upTo: 1024 * 1024) // 1 MB Buffer |
| 139 | + let body = String(buffer: bytes) |
| 140 | + return APIGatewayV2Response(with: body, statusCode: .ok) |
| 141 | + } catch { |
| 142 | + return APIGatewayV2Response(with: error, statusCode: .badRequest) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +## Deployment |
| 149 | + |
| 150 | +Deploy your Lambda function using AWS CDK, SAM, Serverless or Terraform. The Lambda requires: |
| 151 | + |
| 152 | +- API Gateway integration for HTTP requests |
| 153 | + |
| 154 | +For step-by-step deployment instructions and templates, see the [Breeze project repository](https://github.com/swift-serverless/Breeze) for more info on how to deploy it on AWS. |
| 155 | + |
0 commit comments