blob: 1c1c44200494e392ecafee8fa5cc7f917e59a8c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
(** Create the handler for the service *)
let nbcar_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);
})
let capitalize_handler =
Dream_handler.handle
(module Services_impl.Capitalize)
(fun () body ->
Lwt.return_ok
Services_impl.Capitalize.{ value = String.uppercase_ascii 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 nbcar_route =
Dream_handler.register (module Services_impl.Nb_car) nbcar_handler
and capitalize_route =
Dream_handler.register (module Services_impl.Capitalize) capitalize_handler
(** Generate a default static page *)
let hello : Dream.handler =
fun _ ->
Dream.html
{|<html>
<head>
<link rel="stylesheet" href="css/main.min.css">
</head>
<body>
<div id="app">
<section class="hero is-hero-bar">
<div class="hero-body">
<div class="level">
<div class="level-left">
<div class="level-item"><h1 class="title">
Example
</h1></div>
</div>
<div class="level-right" style="display: none;">
<div class="level-item"></div>
</div>
</div>
</div>
</section>
<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" />
</div>
</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 css_assets _root path _request =
(* This module is automatically generated — see the dune file to see the rule *)
match CssAssets.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 "/css/**" (Dream.static ~loader:css_assets "");
Dream.get "/" hello;
nbcar_route;
capitalize_route;
]
(* Now test the application by connecting to
http://localhost:8080/
*)
|