diff --git a/dune-project b/dune-project index 41f5cea..d65af52 100644 --- a/dune-project +++ b/dune-project @@ -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))) diff --git a/example/css_path.ml b/example/css_path.ml index 96a5634..e69de29 100644 --- a/example/css_path.ml +++ b/example/css_path.ml @@ -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 diff --git a/example/dune b/example/dune index 1e537ac..b7b1c0c 100644 --- a/example/dune +++ b/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 "|}")))) diff --git a/example/map.html b/example/index.html similarity index 68% rename from example/map.html rename to example/index.html index 053dd17..4ba93c9 100644 --- a/example/map.html +++ b/example/index.html @@ -4,12 +4,12 @@ - +
-
+
diff --git a/example/server.ml b/example/server.ml new file mode 100644 index 0000000..755e2ab --- /dev/null +++ b/example/server.ml @@ -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 diff --git a/leaflet.opam b/leaflet.opam index d4db64e..9c0c18f 100644 --- a/leaflet.opam +++ b/leaflet.opam @@ -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: [