basic example with tiny_httpd
This commit is contained in:
parent
619be59031
commit
a67ff6acab
@ -30,9 +30,11 @@
|
||||
(tags
|
||||
(leaflet javascript bindings interactive map openstreetmap))
|
||||
(depends
|
||||
(ocaml
|
||||
(>= 4.08))
|
||||
brr
|
||||
js_of_ocaml
|
||||
dune-site
|
||||
(ocaml
|
||||
(>= 4.08)))
|
||||
(tiny_httpd :with-test)
|
||||
)
|
||||
(sites (share css) (share images)))
|
||||
|
||||
@ -1,14 +0,0 @@
|
||||
(* get leaflet.css path from dune-site *)
|
||||
|
||||
let dune_site = List.filter Sys.file_exists Mysites.Sites.css
|
||||
|
||||
let lookup_file filename =
|
||||
List.find_map
|
||||
(fun dir ->
|
||||
let filename' = Filename.concat dir filename in
|
||||
if Sys.file_exists filename' then Some filename' else None )
|
||||
dune_site
|
||||
|
||||
let leaflet_css = lookup_file "leaflet.css" |> Option.get
|
||||
|
||||
let () = Format.printf "%s@." leaflet_css
|
||||
29
example/dune
29
example/dune
@ -1,14 +1,25 @@
|
||||
(executable
|
||||
(name css_path)
|
||||
(modules css_path mysites)
|
||||
(libraries dune-site))
|
||||
|
||||
(generate_sites_module
|
||||
(module mysites)
|
||||
(sites leaflet))
|
||||
|
||||
(executable
|
||||
(name script)
|
||||
(modules script)
|
||||
(libraries brr leaflet)
|
||||
(modes js))
|
||||
|
||||
(executable
|
||||
(name server)
|
||||
(modules content server css_path mysites)
|
||||
(libraries dune-site tiny_httpd))
|
||||
|
||||
(generate_sites_module
|
||||
(module mysites)
|
||||
(sites leaflet))
|
||||
|
||||
(rule
|
||||
(with-stdout-to
|
||||
content.ml
|
||||
(progn
|
||||
(echo "let index_html = {|")
|
||||
(cat index.html)
|
||||
(echo "|}")
|
||||
(echo "let script_js = {|")
|
||||
(cat script.bc.js)
|
||||
(echo "|}"))))
|
||||
|
||||
@ -4,12 +4,12 @@
|
||||
<!-- dune-site should have installed leaflet.css
|
||||
you can use css_path.ml to get it's path -->
|
||||
<link href="leaflet.css" rel="stylesheet">
|
||||
<script type="text/javascript" src="script.bc.js" defer="defer"></script>
|
||||
<script type="text/javascript" src="script.js" defer="defer"></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<!-- don't forget to set a size! -->
|
||||
<div id="map" style="height: 100%"></div>
|
||||
<div id="map" style="height: 700px; width: 700px"></div>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
44
example/server.ml
Normal file
44
example/server.ml
Normal file
@ -0,0 +1,44 @@
|
||||
let read_file file = In_channel.with_open_bin file In_channel.input_all
|
||||
|
||||
let leaflet_css =
|
||||
(* get leaflet.css path from dune-site *)
|
||||
let dune_site = List.filter Sys.file_exists Mysites.Sites.css in
|
||||
let lookup_file filename =
|
||||
List.find_map
|
||||
(fun dir ->
|
||||
let filename' = Filename.concat dir filename in
|
||||
if Sys.file_exists filename' then Some filename' else None )
|
||||
dune_site
|
||||
in
|
||||
let path =
|
||||
match lookup_file "leaflet.css" with
|
||||
| None ->
|
||||
Printf.eprintf
|
||||
"File 'leaflet.css' not found.\n\
|
||||
It should have been installed with dune-site.\n\
|
||||
Did you run `dune build @install`?\n";
|
||||
exit 1
|
||||
| Some o -> o
|
||||
in
|
||||
read_file path
|
||||
|
||||
let script_js = Content.script_js
|
||||
|
||||
let index_html = Content.index_html
|
||||
|
||||
module S = Tiny_httpd
|
||||
|
||||
let () =
|
||||
let server = S.create () in
|
||||
S.add_route_handler server
|
||||
S.Route.(exact "leaflet.css" @/ return)
|
||||
(fun _req -> S.Response.make_string (Ok leaflet_css));
|
||||
S.add_route_handler server
|
||||
S.Route.(exact "script.js" @/ return)
|
||||
(fun _req -> S.Response.make_string (Ok script_js));
|
||||
S.add_route_handler server
|
||||
S.Route.(return)
|
||||
(fun _req -> S.Response.make_string (Ok index_html));
|
||||
|
||||
Printf.printf "listening on http://%s:%d/\n%!" (S.addr server) (S.port server);
|
||||
match S.run server with Ok () -> () | Error e -> raise e
|
||||
@ -16,10 +16,11 @@ doc: "TODO/leaflet"
|
||||
bug-reports: "https://git.zapashcanon.fr/swrup/leaflet/issues"
|
||||
depends: [
|
||||
"dune" {>= "2.9"}
|
||||
"ocaml" {>= "4.08"}
|
||||
"brr"
|
||||
"js_of_ocaml"
|
||||
"dune-site"
|
||||
"ocaml" {>= "4.08"}
|
||||
"tiny_httpd" {with-test}
|
||||
"odoc" {with-doc}
|
||||
]
|
||||
build: [
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user