stack_picker/bin/main.ml

52 lines
1.5 KiB
OCaml
Raw Permalink Normal View History

open Tyxml
open Stack_picker.Srv
(* let () = Eio_main.run @@ fun env -> *)
(* let url = "sqlite3://stack.db" |> Uri.of_string in *)
(* Caqti_eio_unix.with_connection ~stdenv:(env :> Caqti_eio.stdenv) url (Stack_picker.Db.test env#stdout) *)
(* |> Caqti_eio.or_fail *)
let () = Logs.set_reporter (Logs_fmt.reporter ())
and () = Logs.Src.set_level Cohttp_eio.src (Some Debug)
let () =
let port = ref 8080 in
Arg.parse
[ ("-p", Arg.Set_int port, " Listening port number(8080 by default)") ]
ignore "An HTTP/1.1 server";
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
run ~env ~sw ~port:!port
[
get "/ping" (fun _req ->
let num = 1 in
let resp =
Printf.sprintf "Hello this is the num: %d" num
|> Eio.Flow.string_source
in
Stack_picker.Srv.respond ~body:resp ());
get "/" (fun _ ->
let%html html_doc =
{|
<html>
<script src="https://unpkg.com/htmx.org@2.0.3"></script>
<title>Ocaml</title>
<body>
<p>Hello from ocaml!</p>
<button _hx-get="/shop" _hx-swap="outerHTML">Shop</button>
</body>
</html>|}
in
Stack_picker.Srv.respondHtmlDoc ~body:html_doc ());
get "/shop" (fun _ ->
let%html doc =
{|
<ol>
<li>Bat</li>
<li>Cat</li>
<li>Doggie</li>
</ol>
|}
in
respondHtml ~body:doc ());
]