@@ -5,34 +5,68 @@ Write AWS Lambda functions in Gleam!
5
5
[ ![ Package Version] ( https://img.shields.io/hexpm/v/glambda )] ( https://hex.pm/packages/glambda )
6
6
[ ![ Hex Docs] ( https://img.shields.io/badge/hex-docs-ffaff3 )] ( https://hexdocs.pm/glambda/ )
7
7
8
+ Write your Lambda function as a [ wisp] ( https://github.com/gleam-wisp/wisp ) handler, or accept direct events as normal Gleam types.
9
+
10
+ Glambda works by compiling your Gleam code to JavaScript, and then using the AWS Lambda Node.js runtime to run your code.
11
+
8
12
``` sh
9
13
gleam add glambda
10
14
```
15
+
16
+ ## Wisp Handler
17
+
11
18
``` gleam
12
- // .src/handler.gleam
13
- import glambda.{
14
- type ApiGatewayProxyEventV2, type ApiGatewayProxyResultV2, type Context,
15
- type JsContext, type JsEvent, type JsResult, ApiGatewayProxyResultV2,
16
- }
17
- import gleam/javascript/promise.{type Promise}
18
-
19
- pub fn typed_handler(event: ApiGatewayProxyEventV2, ctx: Context) -> Promise(ApiGatewayProxyResultV2) {
20
- let response =
21
- ApiGatewayProxyResultV2(
22
- status_code: 200,
23
- headers: dict.from_list([#("Content-Type", "text/plain")]),
24
- body: Some("hello! from " <> ctx.function_name),
25
- is_base64_encoded: False,
26
- cookies: [],
27
- )
28
- promise.new(fn(resolve) { resolve(response) })
29
- }
30
-
31
- pub fn handler(event: JsEvent, ctx: JsContext) -> Promise(JsResult) {
32
- glambda.api_gateway_proxy_v2_handler(typed_handler)(event, ctx)
33
- }
19
+ import glambda.{type Context}
20
+ import gleam/javascript/promise.{type Promise}
21
+ import gleam/json
22
+ import wisp.{type Request, type Reponse}
23
+
24
+ fn handle_request(req: Request, ctx: Context) -> Promise(Response) {
25
+ string_builder.from_string(
26
+ "{\"functionName\": \"" <> ctx.function_name <> "\"}",
27
+ )
28
+ |> wisp.json_response(200)
29
+ |> promise.resolve
30
+ }
31
+
32
+ pub fn handler(event, ctx) {
33
+ glambda.wisp_handler(handle_request)(event, ctx)
34
+ }
34
35
```
35
36
37
+ ## Event Handler
38
+
39
+ ``` gleam
40
+ import glambda.{
41
+ type ApiGatewayProxyEventV2, type ApiGatewayProxyResultV2, type Context,
42
+ ApiGatewayProxyResultV2,
43
+ }
44
+ import gleam/dict
45
+ import gleam/javascript/promise.{type Promise}
46
+ import gleam/option.{Some}
47
+
48
+ fn event_handler(
49
+ _event: ApiGatewayProxyEventV2,
50
+ ctx: Context,
51
+ ) -> Promise(ApiGatewayProxyResultV2) {
52
+ ApiGatewayProxyResultV2(
53
+ status_code: 200,
54
+ headers: dict.from_list([#("content-type", "application/json")]),
55
+ body: Some("{\"functionName\": \"" <> ctx.function_name <> "\"}"),
56
+ is_base64_encoded: False,
57
+ cookies: [],
58
+ )
59
+ |> promise.resolve
60
+ }
61
+
62
+ pub fn handler(event, ctx) {
63
+ glambda.api_gateway_proxy_v2_handler(event_handler)(event, ctx)
64
+ }
65
+ ```
66
+
67
+ ## Supported Events
68
+ * ` ApiGatewayProxyEventV2 `
69
+
36
70
Further documentation can be found at < https://hexdocs.pm/glambda > .
37
71
38
72
## Development
0 commit comments