ocamlformat
This commit is contained in:
parent
4eec69fdf6
commit
4265cdce92
5 changed files with 67 additions and 26 deletions
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue