Skip to content

Commit 12640ae

Browse files
author
Ryan Miville
committed
no wisp
1 parent 0905488 commit 12640ae

12 files changed

+67
-86
lines changed

README.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Write AWS Lambda functions in Gleam!
55
[![Package Version](https://img.shields.io/hexpm/v/glambda)](https://hex.pm/packages/glambda)
66
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/glambda/)
77

8-
Write your Lambda function as a [wisp](https://github.com/gleam-wisp/wisp) handler, or accept direct events as normal Gleam types.
8+
Write your Lambda function as an [http](https://github.com/gleam-lang/http) handler, or accept direct events as normal Gleam types.
99

1010
Glambda works by compiling your Gleam code to JavaScript, and then using the AWS Lambda Node.js runtime to run your code.
1111

@@ -17,20 +17,26 @@ gleam add glambda
1717

1818
```gleam
1919
import glambda.{type Context}
20+
import gleam/http/request.{type Request}
21+
import gleam/http/response.{type Response, Response}
2022
import gleam/javascript/promise.{type Promise}
21-
import gleam/string_builder
22-
import wisp.{type Request, type Response}
23+
import gleam/option.{type Option, Some}
2324
24-
fn handle_request(_req: Request, ctx: Context) -> Promise(Response) {
25-
string_builder.from_string(
26-
"{\"functionName\": \"" <> ctx.function_name <> "\"}",
25+
fn handle_request(
26+
_req: Request(Option(String)),
27+
ctx: Context,
28+
) -> Promise(Response(Option(String))) {
29+
let json = "{\"functionName\": \"" <> ctx.function_name <> "\"}"
30+
Response(
31+
200,
32+
[#("content-type", "application/json; charset=utf-8")],
33+
Some(json),
2734
)
28-
|> wisp.json_response(200)
2935
|> promise.resolve
3036
}
3137
3238
pub fn handler(event, ctx) {
33-
glambda.wisp_handler(handle_request)(event, ctx)
39+
glambda.http_handler(handle_request)(event, ctx)
3440
}
3541
```
3642

examples/app/src/handler.gleam

-16
This file was deleted.
File renamed without changes.
File renamed without changes.

examples/app/gleam.toml examples/simple/gleam.toml

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ target = "javascript"
55
[dependencies]
66
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
77
glambda = { path = "../.." }
8-
wisp = ">= 1.2.0 and < 2.0.0"
98
gleam_http = ">= 3.7.0 and < 4.0.0"
109
gleam_javascript = ">= 0.13.0 and < 1.0.0"
1110

examples/app/manifest.toml examples/simple/manifest.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages = [
77
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
88
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
99
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
10-
{ name = "glambda", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_javascript", "gleam_stdlib", "wisp"], source = "local", path = "../.." },
10+
{ name = "glambda", version = "0.1.0", build_tools = ["gleam"], requirements = ["gleam_http", "gleam_javascript", "gleam_stdlib"], source = "local", path = "../.." },
1111
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
1212
{ name = "gleam_erlang", version = "0.27.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DE468F676D71B313C6C8C5334425CFCF827837333F8AB47B64D8A6D7AA40185D" },
1313
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
@@ -35,4 +35,3 @@ gleam_http = { version = ">= 3.7.0 and < 4.0.0" }
3535
gleam_javascript = { version = ">= 0.13.0 and < 1.0.0" }
3636
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
3737
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
38-
wisp = { version = ">= 1.2.0 and < 2.0.0" }

examples/simple/src/handler.gleam

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import glambda.{type Context}
2+
import gleam/http/request.{type Request}
3+
import gleam/http/response.{type Response, Response}
4+
import gleam/javascript/promise.{type Promise}
5+
import gleam/option.{type Option, Some}
6+
7+
pub fn handle_request(
8+
_req: Request(Option(String)),
9+
ctx: Context,
10+
) -> Promise(Response(Option(String))) {
11+
let json = "{\"functionName\": \"" <> ctx.function_name <> "\"}"
12+
Response(
13+
200,
14+
[#("content-type", "application/json; charset=utf-8")],
15+
Some(json),
16+
)
17+
|> promise.resolve
18+
}
19+
20+
pub fn handler(event, ctx) {
21+
glambda.http_handler(handle_request)(event, ctx)
22+
}
File renamed without changes.

gleam.toml

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ target = "javascript"
1818
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
1919
gleam_javascript = ">= 0.13.0 and < 1.0.0"
2020
gleam_http = ">= 3.7.0 and < 4.0.0"
21-
wisp = ">= 1.2.0 and < 2.0.0"
2221

2322
[dev-dependencies]
2423
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

-17
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,25 @@
44
packages = [
55
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
66
{ name = "birdie", version = "1.2.3", build_tools = ["gleam"], requirements = ["argv", "edit_distance", "filepath", "glance", "gleam_community_ansi", "gleam_erlang", "gleam_stdlib", "justin", "rank", "simplifile", "trie_again"], otp_app = "birdie", source = "hex", outer_checksum = "AE1207210E9CC8F4170BCE3FB3C23932F314C352C3FD1BCEA44CF4BF8CF60F93" },
7-
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
8-
{ name = "directories", version = "1.1.0", build_tools = ["gleam"], requirements = ["envoy", "gleam_stdlib", "platform", "simplifile"], otp_app = "directories", source = "hex", outer_checksum = "BDA521A4EB9EE3A7894F0DC863797878E91FF5C7826F7084B2E731E208BDB076" },
97
{ name = "edit_distance", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "edit_distance", source = "hex", outer_checksum = "A1E485C69A70210223E46E63985FA1008B8B2DDA9848B7897469171B29020C05" },
10-
{ name = "envoy", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "envoy", source = "hex", outer_checksum = "CFAACCCFC47654F7E8B75E614746ED924C65BD08B1DE21101548AC314A8B6A41" },
11-
{ name = "exception", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "F5580D584F16A20B7FCDCABF9E9BE9A2C1F6AC4F9176FA6DD0B63E3B20D450AA" },
128
{ name = "filepath", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "EFB6FF65C98B2A16378ABC3EE2B14124168C0CE5201553DE652E2644DCFDB594" },
139
{ name = "glam", version = "2.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glam", source = "hex", outer_checksum = "66EC3BCD632E51EED029678F8DF419659C1E57B1A93D874C5131FE220DFAD2B2" },
1410
{ name = "glance", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "glexer"], otp_app = "glance", source = "hex", outer_checksum = "8F3314D27773B7C3B9FB58D8C02C634290422CE531988C0394FA0DF8676B964D" },
1511
{ name = "gleam_community_ansi", version = "1.4.1", build_tools = ["gleam"], requirements = ["gleam_community_colour", "gleam_stdlib"], otp_app = "gleam_community_ansi", source = "hex", outer_checksum = "4CD513FC62523053E62ED7BAC2F36136EC17D6A8942728250A9A00A15E340E4B" },
1612
{ name = "gleam_community_colour", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_json", "gleam_stdlib"], otp_app = "gleam_community_colour", source = "hex", outer_checksum = "795964217EBEDB3DA656F5EB8F67D7AD22872EB95182042D3E7AFEF32D3FD2FE" },
17-
{ name = "gleam_crypto", version = "1.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "8AE56026B3E05EBB1F076778478A762E9EB62B31AEEB4285755452F397029D22" },
1813
{ name = "gleam_erlang", version = "0.27.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "DE468F676D71B313C6C8C5334425CFCF827837333F8AB47B64D8A6D7AA40185D" },
1914
{ name = "gleam_http", version = "3.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "EA66440C2269F7CED0F6845E5BD0DB68095775D627FA709A841CA78A398D6D56" },
2015
{ name = "gleam_javascript", version = "0.13.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_javascript", source = "hex", outer_checksum = "F98328FCF573DA6F3A35D7F6CB3F9FF19FD5224CCBA9151FCBEAA0B983AF2F58" },
2116
{ name = "gleam_json", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "9063D14D25406326C0255BDA0021541E797D8A7A12573D849462CAFED459F6EB" },
22-
{ name = "gleam_otp", version = "0.12.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "BFACC1513410DF5A1617169A9CD7EA334973AC71D860A17574BA7B2EADD89A6F" },
2317
{ name = "gleam_stdlib", version = "0.40.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "86606B75A600BBD05E539EB59FABC6E307EEEA7B1E5865AFB6D980A93BCB2181" },
2418
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
2519
{ name = "glexer", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "glexer", source = "hex", outer_checksum = "BD477AD657C2B637FEF75F2405FAEFFA533F277A74EF1A5E17B55B1178C228FB" },
26-
{ name = "glisten", version = "6.0.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib", "logging", "telemetry"], otp_app = "glisten", source = "hex", outer_checksum = "912132751031473CB38F454120124FFC96AF6B0EA33D92C9C90DB16327A2A972" },
27-
{ name = "gramps", version = "2.0.3", build_tools = ["gleam"], requirements = ["gleam_crypto", "gleam_erlang", "gleam_http", "gleam_stdlib"], otp_app = "gramps", source = "hex", outer_checksum = "3CCAA6E081225180D95C79679D383BBF51C8D1FDC1B84DA1DA444F628C373793" },
28-
{ name = "hpack_erl", version = "0.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "hpack", source = "hex", outer_checksum = "D6137D7079169D8C485C6962DFE261AF5B9EF60FBC557344511C1E65E3D95FB0" },
2920
{ name = "justin", version = "1.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "justin", source = "hex", outer_checksum = "7FA0C6DB78640C6DC5FBFD59BF3456009F3F8B485BF6825E97E1EB44E9A1E2CD" },
30-
{ name = "logging", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "logging", source = "hex", outer_checksum = "1098FBF10B54B44C2C7FDF0B01C1253CAFACDACABEFB4B0D027803246753E06D" },
31-
{ name = "marceau", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "marceau", source = "hex", outer_checksum = "2D1C27504BEF45005F5DFB18591F8610FB4BFA91744878210BDC464412EC44E9" },
32-
{ name = "mist", version = "3.0.0", build_tools = ["gleam"], requirements = ["birl", "gleam_erlang", "gleam_http", "gleam_otp", "gleam_stdlib", "glisten", "gramps", "hpack_erl", "logging"], otp_app = "mist", source = "hex", outer_checksum = "CDA1A74E768419235E16886463EC4722EFF4AB3F8D820A76EAD45D7C167D7282" },
33-
{ name = "platform", version = "1.0.0", build_tools = ["gleam"], requirements = [], otp_app = "platform", source = "hex", outer_checksum = "8339420A95AD89AAC0F82F4C3DB8DD401041742D6C3F46132A8739F6AEB75391" },
3421
{ name = "pprint", version = "1.0.3", build_tools = ["gleam"], requirements = ["glam", "gleam_stdlib"], otp_app = "pprint", source = "hex", outer_checksum = "76BBB92E23D12D954BD452686543F29EDE8EBEBB7FC0ACCBCA66EEF276EC3A06" },
35-
{ name = "ranger", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "1566C272B1D141B3BBA38B25CB761EF56E312E79EC0E2DFD4D3C19FB0CC1F98C" },
3622
{ name = "rank", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "rank", source = "hex", outer_checksum = "5660E361F0E49CBB714CC57CC4C89C63415D8986F05B2DA0C719D5642FAD91C9" },
3723
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
38-
{ name = "telemetry", version = "1.3.0", build_tools = ["rebar3"], requirements = [], otp_app = "telemetry", source = "hex", outer_checksum = "7015FC8919DBE63764F4B4B87A95B7C0996BD539E0D499BE6EC9D7F3875B79E6" },
3924
{ name = "thoas", version = "1.2.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "E38697EDFFD6E91BD12CEA41B155115282630075C2A727E7A6B2947F5408B86A" },
4025
{ name = "trie_again", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "trie_again", source = "hex", outer_checksum = "5B19176F52B1BD98831B57FDC97BD1F88C8A403D6D8C63471407E78598E27184" },
41-
{ name = "wisp", version = "1.2.0", build_tools = ["gleam"], requirements = ["directories", "exception", "gleam_crypto", "gleam_erlang", "gleam_http", "gleam_json", "gleam_stdlib", "logging", "marceau", "mist", "simplifile"], otp_app = "wisp", source = "hex", outer_checksum = "F71265D2F1DE11426535A2FA1DA3B11D2FFB783B116DF9496BC8C41983EBADB4" },
4226
]
4327

4428
[requirements]
@@ -49,4 +33,3 @@ gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
4933
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
5034
pprint = { version = ">= 1.0.3 and < 2.0.0" }
5135
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
52-
wisp = { version = ">= 1.2.0 and < 2.0.0" }

src/glambda.gleam

+30-41
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
import gleam/bit_array
2-
import gleam/bytes_builder
32
import gleam/dict.{type Dict}
43
import gleam/dynamic.{type Dynamic}
54
import gleam/http
6-
import gleam/http/request.{Request as HttpRequest}
7-
import gleam/http/response
5+
import gleam/http/request.{type Request, Request}
6+
import gleam/http/response.{type Response, Response}
87
import gleam/javascript/promise.{type Promise}
98
import gleam/list
109
import gleam/option.{type Option, None, Some}
1110
import gleam/regex
1211
import gleam/result
1312
import gleam/string
14-
import gleam/string_builder
15-
import wisp.{type Request, type Response}
16-
import wisp/internal
1713

1814
pub type JsEvent
1915

@@ -181,8 +177,9 @@ pub type ApiGatewayProxyResultV2 {
181177

182178
// --- Adapters ---------------------------------------------------------------
183179

184-
pub fn wisp_handler(
185-
handler: fn(Request, Context) -> Promise(Response),
180+
pub fn http_handler(
181+
handler: fn(Request(Option(String)), Context) ->
182+
Promise(Response(Option(String))),
186183
) -> JsHandler {
187184
api_gateway_proxy_v2_handler(fn(event, ctx) {
188185
event
@@ -192,17 +189,8 @@ pub fn wisp_handler(
192189
})
193190
}
194191

195-
pub fn create_request(event: ApiGatewayProxyEventV2) -> Request {
196-
let read = case event.body {
197-
Some(body) ->
198-
internal.Chunk(bit_array.from_string(body), fn(_size) {
199-
Ok(internal.ReadingFinished)
200-
})
201-
None -> internal.ReadingFinished
202-
}
203-
204-
let body =
205-
internal.make_connection(fn(_size) { Ok(read) }, wisp.random_string(64))
192+
pub fn create_request(event: ApiGatewayProxyEventV2) -> Request(Option(String)) {
193+
let body = event.body
206194
let method =
207195
event.request_context.http.method
208196
|> http.parse_method
@@ -219,7 +207,7 @@ pub fn create_request(event: ApiGatewayProxyEventV2) -> Request {
219207
let path = event.raw_path
220208
let query = string.to_option(event.raw_query_string)
221209

222-
HttpRequest(
210+
Request(
223211
method:,
224212
headers:,
225213
body:,
@@ -231,44 +219,45 @@ pub fn create_request(event: ApiGatewayProxyEventV2) -> Request {
231219
)
232220
}
233221

234-
fn from_response(response: Response) -> ApiGatewayProxyResultV2 {
222+
fn from_response(response: Response(Option(String))) -> ApiGatewayProxyResultV2 {
223+
let is_base64_encoded = is_base64_encoded(response)
235224
let body = case response.body {
236-
wisp.Empty -> <<>>
237-
wisp.Bytes(builder) -> bytes_builder.to_bit_array(builder)
238-
wisp.Text(builder) ->
239-
string_builder.to_string(builder) |> bit_array.from_string
240-
wisp.File(_path) -> panic as "file body not supported yet"
225+
Some(body) if is_base64_encoded -> Some(base64_encode(body))
226+
_ -> response.body
241227
}
228+
let cookies = get_cookies(response)
229+
230+
ApiGatewayProxyResultV2(
231+
status_code: response.status,
232+
headers: dict.from_list(response.headers),
233+
body: body,
234+
is_base64_encoded:,
235+
cookies:,
236+
)
237+
}
242238

239+
fn is_base64_encoded(response: Response(Option(String))) {
243240
let is_content_type_binary =
244241
response.get_header(response, "content-type")
245242
|> result.map(is_content_type_binary)
246243

247-
let is_base64_encoded = case is_content_type_binary {
244+
case is_content_type_binary {
248245
Ok(True) -> True
249246
_ -> {
250247
response.get_header(response, "content-encoding")
251248
|> result.map(is_content_encoding_binary)
252249
|> result.unwrap(False)
253250
}
254251
}
252+
}
255253

256-
let body = case is_base64_encoded {
257-
True -> bit_array.base64_encode(body, False)
258-
False -> bit_array.to_string(body) |> result.unwrap("")
259-
}
260-
let cookies = get_cookies(response)
261-
262-
ApiGatewayProxyResultV2(
263-
status_code: response.status,
264-
headers: dict.from_list(response.headers),
265-
body: string.to_option(body),
266-
is_base64_encoded:,
267-
cookies:,
268-
)
254+
fn base64_encode(body: String) -> String {
255+
body
256+
|> bit_array.from_string
257+
|> bit_array.base64_encode(False)
269258
}
270259

271-
fn get_cookies(response: Response) -> List(String) {
260+
fn get_cookies(response: Response(_)) -> List(String) {
272261
response.get_cookies(response)
273262
|> list.map(fn(cookie) { cookie.0 <> "=" <> cookie.1 })
274263
}

0 commit comments

Comments
 (0)