Skip to content

Commit 5d3210e

Browse files
authored
port to eio (#65)
* port to eio * fix gh actions * fix examples, pass sw/env to context, remove unneeded deps from dune files
1 parent cf779dc commit 5d3210e

39 files changed

+413
-552
lines changed

.github/workflows/test.yml

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,13 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
ocamlVersion: [4_12, 4_13, 4_14, 5_00]
12+
ocamlVersion: [5_00]
1313
steps:
1414
- uses: actions/checkout@v2
15-
- uses: cachix/install-nix-action@v16
15+
- uses: cachix/install-nix-action@v18
1616
with:
17-
skip_adding_nixpkgs_channel: true
18-
- uses: cachix/cachix-action@v10
19-
with:
20-
name: anmonteiro
21-
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
17+
extra_nix_config: |
18+
extra-substituters = https://anmonteiro.nix-cache.workers.dev
19+
extra-trusted-public-keys = ocaml.nix-cache.com-1:/xI2h2+56rwFfKyyFVbkJSeGqSIYMC/Je+7XXqGKDIY=
2220
- name: "Run nix-build"
2321
run: nix-build ./nix/ci/test.nix --argstr ocamlVersion ${{ matrix.ocamlVersion }}

.ocamlformat

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
profile = sparse
2-
break-cases = nested
3-
break-fun-decl = smart
4-
cases-exp-indent = 2
5-
if-then-else = fit-or-vertical
6-
parens-tuple = multi-line-only
7-
parens-tuple-patterns = multi-line-only
8-
parens-ite = false
9-
infix-precedence = parens
1+
break-infix = fit-or-vertical
102
break-infix-before-func = false
11-
sequence-style = terminator
12-
sequence-blank-line = compact
3+
break-fun-decl = fit-or-vertical
4+
break-separators = before
5+
break-sequences = true
6+
cases-exp-indent = 2
7+
dock-collection-brackets = false
8+
field-space = loose
9+
if-then-else = keyword-first
1310
indicate-multiline-delimiters = no
11+
infix-precedence = parens
12+
leading-nested-match-parens = true
13+
let-and = sparse
14+
let-module = sparse
1415
ocp-indent-compat = true
15-
wrap-comments = true
16+
parens-tuple = multi-line-only
1617
parse-docstrings = true
17-
let-binding-spacing = compact
18+
sequence-blank-line = preserve-one
19+
sequence-style = terminator
20+
single-case = sparse
21+
space-around-arrays= true
22+
space-around-lists= true
23+
space-around-records= true
24+
space-around-variants= true
1825
type-decl = sparse
26+
wrap-comments = true
27+
wrap-fun-args = false
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
open Lwt.Infix
2-
31
let my_handler evt _context =
4-
Logs_lwt.app (fun m -> m "Hello from the async handler") >>= fun () ->
5-
Lwt_result.return evt
2+
Logs.app (fun m -> m "Hello from the async handler");
3+
Ok evt
64

75
let setup_log ?style_renderer level =
86
Fmt_tty.setup_std_outputs ?style_renderer ();
@@ -12,4 +10,4 @@ let setup_log ?style_renderer level =
1210

1311
let () =
1412
setup_log (Some Logs.Debug);
15-
Lambda_runtime.Json.io_lambda my_handler
13+
Lambda_runtime.Json.lambda my_handler

examples/asynchronous-handler/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(executable
22
(name basic)
3-
(libraries lambda-runtime logs.lwt logs.fmt fmt.tty lwt lwt.unix result))
3+
(libraries lambda-runtime logs.fmt fmt.tty result))
44

55
(env
66
(static

examples/now-custom-runtime/basic.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ let my_handler evt _context =
77
; "body", `String (Yojson.Safe.prettify body)
88
; "headers", `Assoc []
99
])
10-
| _ ->
11-
Error "Body wasn't string"
10+
| _ -> Error "Body wasn't string"
1211

1312
let setup_log ?style_renderer level =
1413
Fmt_tty.setup_std_outputs ?style_renderer ();
+26-34
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,35 @@
1-
open Lwt.Infix;
21
module Client = Piaf.Client.Oneshot;
32

4-
let my_handler = (_request, _context) => {
3+
let my_handler = (_request, {Lambda_runtime.Context.sw, env, _}) => {
54
let uri =
65
Uri.of_string(
76
"http://api.giphy.com/v1/gifs/random?tag=cat&api_key=hamBGlVDz0XI5tYtxTuPgudCVhHSNX8q&limit=1",
87
);
9-
Client.get(uri)
10-
>>= (
11-
fun
12-
| Ok(response) => {
13-
Piaf.Body.to_string(response.body)
14-
>>= (
15-
fun
16-
| Ok(body_str) => {
17-
open Yojson.Safe;
18-
let body_json = Yojson.Safe.from_string(body_str);
19-
let img_url =
20-
body_json
21-
|> Util.member("data")
22-
|> Util.member("images")
23-
|> Util.member("original")
24-
|> Util.member("url")
25-
|> Util.to_string;
26-
let body = Printf.sprintf("<img src=\"%s\">", img_url);
27-
let response =
28-
Piaf.Response.of_string(
29-
~body,
30-
~headers=
31-
Piaf.Headers.of_list([("content-type", "text/html")]),
32-
`OK,
33-
);
34-
Lwt.return_ok(response);
35-
}
36-
| Error(_) => Lwt.return(Error("Failed for some reason"))
8+
switch (Client.get(~sw, env, uri)) {
9+
| Ok(response) =>
10+
switch (Piaf.Body.to_string(response.body)) {
11+
| Ok(body_str) =>
12+
open Yojson.Safe;
13+
let body_json = Yojson.Safe.from_string(body_str);
14+
let img_url =
15+
body_json
16+
|> Util.member("data")
17+
|> Util.member("images")
18+
|> Util.member("original")
19+
|> Util.member("url")
20+
|> Util.to_string;
21+
let body = Printf.sprintf("<img src=\"%s\">", img_url);
22+
let response =
23+
Piaf.Response.of_string(
24+
~body,
25+
~headers=Piaf.Headers.of_list([("content-type", "text/html")]),
26+
`OK,
3727
);
38-
}
39-
| Error(_) => Lwt.return(Error("Failed for some reason"))
40-
);
28+
Ok(response);
29+
| Error(_) => Error("Failed for some reason")
30+
}
31+
| Error(_) => Error("Failed for some reason")
32+
};
4133
};
4234

4335
let setup_log = (~style_renderer=?, level) => {
@@ -49,5 +41,5 @@ let setup_log = (~style_renderer=?, level) => {
4941

5042
let () = {
5143
setup_log(Some(Logs.Debug));
52-
Vercel.io_lambda(my_handler);
44+
Vercel.lambda(my_handler);
5345
};

examples/vercel-lambda-reason/dune

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(executable
22
(name basic)
3-
(libraries vercel logs.fmt fmt.tty piaf lwt lwt.unix uri bigstringaf
4-
bigarray-compat result yojson))
3+
(libraries vercel logs.fmt fmt.tty piaf uri bigstringaf yojson))
54

65
(env
76
(static

examples/vercel-lambda/basic.ml

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
open Lwt.Syntax
21
open Vercel
32

43
let my_handler request _context =
54
let { Request.headers; body; _ } = request in
65
let host = Headers.get_exn headers "host" in
7-
let+ body = Piaf.Body.to_string body in
8-
let body = Result.get_ok body in
6+
let body = Result.get_ok (Piaf.Body.to_string body) in
97
let body =
10-
if String.length body > 0 then
11-
body
12-
else
13-
Format.asprintf "Didn't get an HTTP body from %s" host
8+
if String.length body > 0
9+
then body
10+
else Format.asprintf "Didn't get an HTTP body from %s" host
1411
in
1512
let response =
1613
Response.of_string
@@ -28,4 +25,4 @@ let setup_log ?style_renderer level =
2825

2926
let () =
3027
setup_log (Some Logs.Debug);
31-
Vercel.io_lambda my_handler
28+
Vercel.lambda my_handler

examples/vercel-lambda/dune

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(executable
22
(name basic)
3-
(libraries vercel logs.fmt fmt.tty piaf lwt result))
3+
(libraries vercel logs.fmt fmt.tty piaf result))
44

55
(env
66
(static

flake.lock

+26-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
{
22
description = "Lambda Runtime Nix Flake";
33

4+
inputs.nix-filter.url = "github:numtide/nix-filter";
45
inputs.flake-utils.url = "github:numtide/flake-utils";
56
inputs.nixpkgs.inputs.flake-utils.follows = "flake-utils";
67
inputs.nixpkgs.url = "github:anmonteiro/nix-overlays";
78

8-
outputs = { self, nixpkgs, flake-utils }:
9+
outputs = { self, nixpkgs, flake-utils, nix-filter }:
910
flake-utils.lib.eachDefaultSystem (system:
1011
let
1112
pkgs = nixpkgs.legacyPackages."${system}".extend (self: super: {
12-
ocamlPackages = super.ocaml-ng.ocamlPackages_4_14;
13+
ocamlPackages = super.ocaml-ng.ocamlPackages_5_0;
1314
});
1415
in
1516
rec {
16-
packages = (pkgs.callPackage ./nix { });
17+
packages = (pkgs.callPackage ./nix { nix-filter = nix-filter.lib; });
1718
defaultPackage = packages.lambda-runtime;
18-
devShell = pkgs.callPackage ./shell.nix { };
19+
devShell = pkgs.callPackage ./shell.nix { inherit packages; };
1920
});
2021
}

lambda-runtime.opam

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ depends: [
1515
"yojson" {>= "1.6.0" & < "2.0.0"}
1616
"ppx_deriving_yojson"
1717
"piaf"
18+
"eio_main"
1819
"uri"
1920
"logs"
20-
"lwt"
2121
"alcotest" {with-test}
2222
]
2323
synopsis:

0 commit comments

Comments
 (0)