Skip to content

Commit a53f8a5

Browse files
authored
Piaf migration (#56)
1 parent 7869c29 commit a53f8a5

25 files changed

Lines changed: 140 additions & 339 deletions

.github/workflows/test.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ jobs:
99
runs-on: ubuntu-latest
1010
strategy:
1111
matrix:
12-
ocamlVersion: [4_08, 4_09, 4_10, 4_11]
12+
ocamlVersion: [4_08, 4_09, 4_10, 4_11, 4_12]
1313
steps:
1414
- uses: actions/checkout@v2
15-
- uses: cachix/install-nix-action@v8
16-
- uses: cachix/cachix-action@v5
15+
- uses: cachix/install-nix-action@v12
16+
with:
17+
skip_adding_nixpkgs_channel: true
18+
- uses: cachix/cachix-action@v8
1719
with:
1820
name: anmonteiro
19-
file: ./nix/ci/test.nix
20-
nixBuildArgs: --argstr ocamlVersion ${{ matrix.ocamlVersion }}
2121
signingKey: '${{ secrets.CACHIX_SIGNING_KEY }}'
22-
22+
- name: "Run nix-build"
23+
run: nix-build ./nix/ci/test.nix --argstr ocamlVersion ${{ matrix.ocamlVersion }}

examples/now-lambda-reason/basic.re

Lines changed: 30 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,41 @@
11
open Lwt.Infix;
2+
module Client = Piaf.Client.Oneshot;
23

3-
let send_request = (~meth=`GET, ~additional_headers=[], ~body=?, uri) => {
4-
open Httpaf;
5-
open Httpaf_lwt_unix;
6-
let response_handler = (notify_response_received, response, response_body) =>
7-
Lwt.wakeup_later(
8-
notify_response_received,
9-
Ok((response, response_body)),
10-
);
11-
12-
let error_handler = (notify_response_received, error) =>
13-
Lwt.wakeup_later(notify_response_received, Error(error));
14-
15-
let host = Uri.host_with_default(uri);
16-
let port =
17-
switch (Uri.port(uri)) {
18-
| None => "80"
19-
| Some(p) => string_of_int(p)
20-
};
21-
22-
Lwt_unix.getaddrinfo(host, port, [Unix.(AI_FAMILY(PF_INET))])
23-
>>= (
24-
addresses => {
25-
let socket = Lwt_unix.socket(Unix.PF_INET, Unix.SOCK_STREAM, 0);
26-
Lwt_unix.connect(socket, List.hd(addresses).Unix.ai_addr)
27-
>>= (
28-
() => {
29-
Client.create_connection(socket)
30-
>>= (
31-
connection => {
32-
let content_length =
33-
switch (body) {
34-
| None => "0"
35-
| Some(body) => string_of_int(String.length(body))
36-
};
37-
38-
let request_headers =
39-
Request.create(
40-
meth,
41-
Uri.path_and_query(uri),
42-
~headers=
43-
Headers.of_list(
44-
[("Host", host), ("Content-Length", content_length)]
45-
@ additional_headers,
46-
),
47-
);
48-
49-
let (response_received, notify_response_received) = Lwt.wait();
50-
let response_handler =
51-
response_handler(notify_response_received);
52-
let error_handler = error_handler(notify_response_received);
53-
54-
let request_body =
55-
Client.request(
56-
connection,
57-
request_headers,
58-
~error_handler,
59-
~response_handler,
60-
);
61-
62-
switch (body) {
63-
| Some(body) => Body.write_string(request_body, body)
64-
| None => ()
65-
};
66-
Body.flush(request_body, () => Body.close_writer(request_body));
67-
response_received;
68-
}
69-
);
70-
}
71-
);
72-
}
73-
);
74-
};
75-
76-
let read_response = response_body => {
77-
let buf = Buffer.create(1024);
78-
let (body_read, notify_body_read) = Lwt.wait();
79-
let rec read_fn = () =>
80-
Httpaf.Body.schedule_read(
81-
response_body,
82-
~on_eof=() => Lwt.wakeup_later(notify_body_read, Buffer.contents(buf)),
83-
~on_read=
84-
(response_fragment, ~off, ~len) => {
85-
let response_fragment_bytes = Bytes.create(len);
86-
Lwt_bytes.blit_to_bytes(
87-
response_fragment,
88-
off,
89-
response_fragment_bytes,
90-
0,
91-
len,
92-
);
93-
Buffer.add_bytes(buf, response_fragment_bytes);
94-
read_fn();
95-
},
96-
);
97-
98-
read_fn();
99-
body_read;
100-
};
101-
102-
let my_handler = (reqd, _context) => {
4+
let my_handler = (_request, _context) => {
1035
let uri =
1046
Uri.of_string(
1057
"http://api.giphy.com/v1/gifs/random?tag=cat&api_key=hamBGlVDz0XI5tYtxTuPgudCVhHSNX8q&limit=1",
1068
);
107-
send_request(uri)
9+
Client.get(uri)
10810
>>= (
10911
fun
110-
| Ok((_response, body)) =>
111-
read_response(body)
112-
>>= (
113-
body_str => {
114-
open Yojson.Safe;
115-
let body_json = Yojson.Safe.from_string(body_str);
116-
let img_url =
117-
body_json
118-
|> Util.member("data")
119-
|> Util.member("images")
120-
|> Util.member("original")
121-
|> Util.member("url")
122-
|> Util.to_string;
123-
let body = Printf.sprintf("<img src=\"%s\">", img_url);
124-
let response =
125-
Httpaf.Response.create(
126-
~headers=
127-
Httpaf.Headers.of_list([("content-type", "text/html")]),
128-
`OK,
129-
);
130-
Lwt.return(Now.Reqd.respond_with_string(reqd, response, body));
131-
}
132-
)
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"))
37+
);
38+
}
13339
| Error(_) => Lwt.return(Error("Failed for some reason"))
13440
);
13541
};

examples/now-lambda-reason/dune

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(executable
22
(name basic)
3-
(libraries now logs.fmt fmt.tty httpaf httpaf-lwt-unix lwt lwt.unix uri
4-
bigstringaf bigarray-compat yojson))
3+
(libraries now logs.fmt fmt.tty piaf lwt lwt.unix uri bigstringaf
4+
bigarray-compat result yojson))
55

66
(env
77
(static

examples/now-lambda/basic.ml

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1+
open Lwt.Syntax
12
open Now
23

3-
let my_handler reqd _context =
4-
let { Request.headers; _ } = Reqd.request reqd in
5-
let body = Reqd.request_body reqd in
4+
let my_handler request _context =
5+
let { Request.headers; body; _ } = request in
66
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
79
let body =
8-
match body with
9-
| Some body ->
10+
if String.length body > 0 then
1011
body
11-
| None ->
12-
Printf.sprintf "Didn't get an HTTP body from %s" host
12+
else
13+
Format.asprintf "Didn't get an HTTP body from %s" host
1314
in
1415
let response =
15-
Response.create
16+
Response.of_string
17+
~body
1618
~headers:(Headers.of_list [ "Content-Type", "application/json" ])
1719
`OK
1820
in
19-
Reqd.respond_with_string reqd response body
21+
Ok response
2022

2123
let setup_log ?style_renderer level =
2224
Fmt_tty.setup_std_outputs ?style_renderer ();
@@ -26,4 +28,4 @@ let setup_log ?style_renderer level =
2628

2729
let () =
2830
setup_log (Some Logs.Debug);
29-
Now.lambda my_handler
31+
Now.io_lambda my_handler

examples/now-lambda/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(executable
22
(name basic)
3-
(libraries now logs.fmt fmt.tty httpaf))
3+
(libraries now logs.fmt fmt.tty piaf lwt result))
44

55
(env
66
(static

lambda-runtime.opam

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ depends: [
2121
"alcotest" {with-test}
2222
]
2323
pin-depends: [
24-
[ "httpaf.dev" "git+https://github.com/anmonteiro/httpaf.git#fork" ]
25-
[ "httpaf-lwt.dev" "git+https://github.com/anmonteiro/httpaf.git#fork" ]
26-
[ "httpaf-lwt-unix.dev" "git+https://github.com/anmonteiro/httpaf.git#fork" ]
27-
[ "h2.dev" "git+https://github.com/anmonteiro/ocaml-h2.git" ]
28-
[ "h2-lwt.dev" "git+https://github.com/anmonteiro/ocaml-h2.git" ]
29-
[ "h2-lwt-unix.dev" "git+https://github.com/anmonteiro/ocaml-h2.git" ]
30-
[ "piaf.dev" "git+https://github.com/anmonteiro/piaf.git" ]
3124
[ "ssl.dev" "git+https://github.com/savonet/ocaml-ssl.git" ]
3225
]
3326
synopsis:

lib/http.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ end
102102

103103
module API_gateway_response = struct
104104
type t = api_gateway_proxy_response [@@deriving to_yojson]
105+
106+
let to_yojson t = Lwt.return (to_yojson t)
105107
end
106108

107109
include Runtime.Make (API_gateway_request) (API_gateway_response)

lib/json.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
module Id = struct
3434
type t = Yojson.Safe.t [@@deriving yojson]
35+
36+
let to_yojson t = Lwt.return (to_yojson t)
3537
end
3638

3739
include Runtime.Make (Id) (Id)

lib/runtime.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct
112112
let request_id = ctx.aws_request_id in
113113
invoke runtime event ctx >>= function
114114
| Ok response ->
115-
let response_json = Response.to_yojson response in
115+
Response.to_yojson response >>= fun response_json ->
116116
Client.event_response runtime.client request_id response_json
117117
>>= ( function
118118
| Ok _ ->

lib/runtime_intf.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ end
3939
module type LambdaResponse = sig
4040
type t
4141

42-
val to_yojson : t -> Yojson.Safe.t
42+
val to_yojson : t -> Yojson.Safe.t Lwt.t
4343
end
4444

4545
module type LambdaRuntime = sig

0 commit comments

Comments
 (0)