aboutsummaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2025-06-01 17:12:06 +0200
committerSébastien Dailly <sebastien@dailly.me>2025-06-14 10:58:01 +0200
commit2f18b8a33cabd0ea666781ba048d0174b4dc5031 (patch)
treeaacda421b89e8133e3c73942e9ede61283a5005c /bin
Initial commit
Diffstat (limited to 'bin')
-rw-r--r--bin/client.ml23
-rw-r--r--bin/dune22
-rw-r--r--bin/js_assets/dune3
-rw-r--r--bin/main.ml58
4 files changed, 106 insertions, 0 deletions
diff --git a/bin/client.ml b/bin/client.ml
new file mode 100644
index 0000000..17284e1
--- /dev/null
+++ b/bin/client.ml
@@ -0,0 +1,23 @@
+open Lwt.Syntax
+
+let root = "http://[::1]:8080"
+
+let request =
+ let* result =
+ Cohttp_handler.request ~root
+ (module Services_impl.Nb_car)
+ () { value = "foobar" }
+ in
+ match result with
+ | Error code ->
+ prerr_endline ("Got code " ^ code);
+ Lwt.return_unit
+ | Ok { value; nbcar } ->
+ print_endline
+ (String.concat " "
+ [
+ "The number of characters for"; value; "is"; Int64.to_string nbcar;
+ ]);
+ Lwt.return_unit
+
+let _ = Lwt_main.run request
diff --git a/bin/dune b/bin/dune
new file mode 100644
index 0000000..7044b82
--- /dev/null
+++ b/bin/dune
@@ -0,0 +1,22 @@
+(executables
+ (public_names dream_service client)
+ (names main client)
+ (libraries
+ lwt
+ lwt.unix
+ dream
+ dream_handler
+ cohttp_handler
+ uri
+ services
+ services_impl
+ )
+ )
+
+
+(rule
+ (target jsAssets.ml)
+ (deps (glob_files js_assets/**))
+ (action (with-stdout-to %{null}
+ (run ocaml-crunch -m plain js_assets/ -o %{target}))))
+
diff --git a/bin/js_assets/dune b/bin/js_assets/dune
new file mode 100644
index 0000000..155a383
--- /dev/null
+++ b/bin/js_assets/dune
@@ -0,0 +1,3 @@
+; Copy the files generated with js_of_ocaml into this directory.
+; We have to explicitaly name the targets we want to include here.
+(copy_files ../../js/content.js)
diff --git a/bin/main.ml b/bin/main.ml
new file mode 100644
index 0000000..f1f0cfd
--- /dev/null
+++ b/bin/main.ml
@@ -0,0 +1,58 @@
+(** Create the handler for the service *)
+let handler =
+ Dream_handler.handle
+ (module Services_impl.Nb_car)
+ (fun (() : Services_impl.Nb_car.placeholders) body ->
+ Lwt.return_ok
+ Services_impl.Nb_car.
+ {
+ value = body.value;
+ nbcar = Int64.of_int (String.length body.value);
+ })
+
+(* The handler and the route are not created at the same time because we may
+ want create a specific handler, for example one checking CRSF in the query
+ and can’t infer this from the service signature only *)
+
+(** And create the route. *)
+let route = Dream_handler.register (module Services_impl.Nb_car) handler
+
+(** Generate a default static page *)
+let hello : Dream.handler =
+ fun _ ->
+ Dream.html
+ {|<html>
+ <body>
+ <h1>Hello!</h1>
+ <div>
+ <noscript>Sorry, you need to enable JavaScript to see this page.</noscript>
+ <script id="lib" type="text/javascript" defer="defer" src="js/content.js"></script>
+ <script>
+ var script = document.getElementById('lib');
+ lib.addEventListener('load', function() {
+ client.start()
+ })
+ </script>
+ </div>
+ <div id="content" />
+ </body>
+</html>|}
+
+let js_assets _root path _request =
+ (* This module is automatically generated — see the dune file to see the rule *)
+ match JsAssets.read path with
+ | None -> Dream.empty `Not_Found
+ | Some asset -> Dream.respond asset
+
+let () =
+ Dream.run @@ Dream.logger
+ @@ Dream.router
+ [
+ Dream.get "/js/**" (Dream.static ~loader:js_assets "");
+ Dream.get "/" hello;
+ route;
+ ]
+
+(* Now test the application by connecting to
+ http://localhost:8080/
+ *)