From ed29333a87d28bb2f4329a1a74a136ef7371d918 Mon Sep 17 00:00:00 2001 From: Nathan Anderson <nathananderson98@gmail.com> Date: Sun, 13 Aug 2023 22:44:10 -0600 Subject: [PATCH] WIP server --- .gitignore | 1 + .ignore | 1 + .ocamlformat | 0 bin/dune | 4 ++++ bin/main.ml | 13 +++++++++++++ data.db | Bin 0 -> 8192 bytes dune-project | 26 ++++++++++++++++++++++++++ html/index.html | 39 +++++++++++++++++++++++++++++++++++++++ html/page_template.html | 17 +++++++++++++++++ lib/db.ml | 31 +++++++++++++++++++++++++++++++ lib/dune | 3 +++ lib/math.ml | 2 ++ lib/utils.ml | 5 +++++ oasis_server.opam | 38 ++++++++++++++++++++++++++++++++++++++ static/tail-spin.svg | 32 ++++++++++++++++++++++++++++++++ test/dune | 2 ++ test/oasis_server.ml | 0 17 files changed, 214 insertions(+) create mode 100644 .gitignore create mode 100644 .ignore create mode 100644 .ocamlformat create mode 100644 bin/dune create mode 100644 bin/main.ml create mode 100644 data.db create mode 100644 dune-project create mode 100644 html/index.html create mode 100644 html/page_template.html create mode 100644 lib/db.ml create mode 100644 lib/dune create mode 100644 lib/math.ml create mode 100644 lib/utils.ml create mode 100644 oasis_server.opam create mode 100644 static/tail-spin.svg create mode 100644 test/dune create mode 100644 test/oasis_server.ml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6a151b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_build/ \ No newline at end of file diff --git a/.ignore b/.ignore new file mode 100644 index 0000000..69fa449 --- /dev/null +++ b/.ignore @@ -0,0 +1 @@ +_build/ diff --git a/.ocamlformat b/.ocamlformat new file mode 100644 index 0000000..e69de29 diff --git a/bin/dune b/bin/dune new file mode 100644 index 0000000..82427cb --- /dev/null +++ b/bin/dune @@ -0,0 +1,4 @@ +(executable + (public_name oasis_server) + (name main) + (libraries oasis_server dream core)) diff --git a/bin/main.ml b/bin/main.ml new file mode 100644 index 0000000..0e1ada7 --- /dev/null +++ b/bin/main.ml @@ -0,0 +1,13 @@ +let () = + print_endline "Hello, World!"; + Dream.run @@ Dream.logger + @@ Dream.router + [ + Dream.get "/static/**" @@ Dream.static "static"; + Oasis_server.Utils.html_page_route "/"; + Dream.get "/echo/:message" (fun request -> + Dream.html (Dream.param request "message")); + Dream.get "/test" (fun _ -> + Unix.sleep 1; + Dream.html "<p>You got me!</p>"); + ] diff --git a/data.db b/data.db new file mode 100644 index 0000000000000000000000000000000000000000..ecc49444214fe63e4e86da510c03658294e643ee GIT binary patch literal 8192 zcmeI#xe5X?5C-7MDu@fRHnQ!M3S#36c(9!a9)+Dm9A$A2$wtu9m+|3z0bAp0E7rn4 zWM*<f;OnxOO=2s&%T1~+_o+!r(SVtVyidAThH|y~nZMd={VP1`?d}bCO9eM_0|5aD zKmY;|fB*y_009U<00Izzz*m8ls)b>wu7TB?L{5d6qAcEc9u1cfFNdQ^#N`&Z{dmi9 zW@Rsik0wsFxvURz>H3*Yg{_<|XSvH$lK7qX-~2(r<Ll!%fB*y_009U<00Izz00bZa R0SG|gKLu*4Qg2PQl_wdVDf9pU literal 0 HcmV?d00001 diff --git a/dune-project b/dune-project new file mode 100644 index 0000000..e19f7d2 --- /dev/null +++ b/dune-project @@ -0,0 +1,26 @@ +(lang dune 3.10) + +(name oasis_server) + +(generate_opam_files true) + +(source + (github username/reponame)) + +(authors "Author Name") + +(maintainers "Maintainer Name") + +(license LICENSE) + +(documentation https://url/to/documentation) + +(package + (name oasis_server) + (synopsis "A short synopsis") + (description "A longer description") + (depends ocaml core dune dream lwt lwt_ppx caqti caqti-lwt caqti-driver-sqlite3) + (tags + (topics "to describe" your project))) + +; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project diff --git a/html/index.html b/html/index.html new file mode 100644 index 0000000..0ff0543 --- /dev/null +++ b/html/index.html @@ -0,0 +1,39 @@ +<!DOCTYPE html> +<html> + +<head> + <title>Peimono</title> + +</html> +<style> + :root { + --peach: #eca880; + --cherry: #b03c3c; + --slate: #909090; + --dark: #6c6c6c; + --amber: #dcb468; + --lime: #81f499; + --plum: #533a7b; + } + + body { + background-color: var(--slate); + } + + h1 { + color: var(--peach); + } +</style> +<script src="https://unpkg.com/htmx.org@1.9.4" + integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script> + +</head> + +<body> + <h1>Hello there!!</h1> + <button hx-get="/test" hx-indicator="#spinner" hx-target="#button_results">Get stuff button</button> + <img id="spinner" class="htmx-indicator" src="static/tail-spin.svg" /> + <div id="button_results"></div> +</body> + +</html> \ No newline at end of file diff --git a/html/page_template.html b/html/page_template.html new file mode 100644 index 0000000..1f2f056 --- /dev/null +++ b/html/page_template.html @@ -0,0 +1,17 @@ +<!DOCTYPE html> +<html> + +<head> + <title>Page Title</title> + +</html> +<script src="https://unpkg.com/htmx.org@1.9.4" + integrity="sha384-zUfuhFKKZCbHTY6aRR46gxiqszMk5tcHjsVFxnUo8VMus4kHGVdIYVbOYYNlKmHV" crossorigin="anonymous"></script> + +</head> + +<body> + <h1>Hello there!!</h1> +</body> + +</html> \ No newline at end of file diff --git a/lib/db.ml b/lib/db.ml new file mode 100644 index 0000000..f1c65b0 --- /dev/null +++ b/lib/db.ml @@ -0,0 +1,31 @@ +type user = { id : int; name : string } + +let conn_str = "sqlite3:./data.db" + +module type DB = Caqti_lwt.CONNECTION + +let get_all_users_q = Caqti_request.Infix() + +let pool = + let uri = Uri.of_string conn_str in + match Caqti_lwt.connect_pool ~max_size:10 uri with + | Ok pool -> pool + | Error err -> failwith (Caqti_error.show err) + +let get_users = + let get_users' (module C: Caqti_lwt.CONNECTION) = + C.fold get_all_users_q + + let find_users = + let open Lwt_result.Infix in + fun (module Db : Caqti_lwt.CONNECTION) -> + Db.collect_list find_users_request () >>| List.map make_user + + let () = + let uri = Uri.of_string conn_str in + match Lwt_main.run (Caqti_lwt.connect uri >>= find_users) with + | Ok users -> + List.iter (fun user -> Printf.printf "%d: %s\n" user.id user.name) users + | Error err -> + Format.eprintf "%a@." Caqti_error.pp err + diff --git a/lib/dune b/lib/dune new file mode 100644 index 0000000..d6d9ced --- /dev/null +++ b/lib/dune @@ -0,0 +1,3 @@ +(library + (name oasis_server) + (libraries dream lwt_ppx caqti-lwt caqti-driver-sqlite3)) diff --git a/lib/math.ml b/lib/math.ml new file mode 100644 index 0000000..eda1b9c --- /dev/null +++ b/lib/math.ml @@ -0,0 +1,2 @@ +let add x y = x + y +let sub x y = x - y diff --git a/lib/utils.ml b/lib/utils.ml new file mode 100644 index 0000000..973ef0b --- /dev/null +++ b/lib/utils.ml @@ -0,0 +1,5 @@ +let html_page_route path = + let ic = open_in ("html" ^ path ^ "index.html") in + let lines = In_channel.input_all ic in + In_channel.close ic; + Dream.get path (fun _ -> Dream.html lines) diff --git a/oasis_server.opam b/oasis_server.opam new file mode 100644 index 0000000..ab6d79b --- /dev/null +++ b/oasis_server.opam @@ -0,0 +1,38 @@ +# This file is generated by dune, edit dune-project instead +opam-version: "2.0" +synopsis: "A short synopsis" +description: "A longer description" +maintainer: ["Maintainer Name"] +authors: ["Author Name"] +license: "LICENSE" +tags: ["topics" "to describe" "your" "project"] +homepage: "https://github.com/username/reponame" +doc: "https://url/to/documentation" +bug-reports: "https://github.com/username/reponame/issues" +depends: [ + "ocaml" + "core" + "dune" {>= "3.10"} + "dream" + "lwt" + "lwt_ppx" + "caqti" + "caqti-lwt" + "caqti-driver-sqlite3" + "odoc" {with-doc} +] +build: [ + ["dune" "subst"] {dev} + [ + "dune" + "build" + "-p" + name + "-j" + jobs + "@install" + "@runtest" {with-test} + "@doc" {with-doc} + ] +] +dev-repo: "git+https://github.com/username/reponame.git" diff --git a/static/tail-spin.svg b/static/tail-spin.svg new file mode 100644 index 0000000..075a399 --- /dev/null +++ b/static/tail-spin.svg @@ -0,0 +1,32 @@ +<!-- By Sam Herbert (@sherb), for everyone. More @ http://goo.gl/7AJzbL --> +<svg width="38" height="38" viewBox="0 0 38 38" xmlns="http://www.w3.org/2000/svg"> + <defs> + <linearGradient x1="8.042%" y1="0%" x2="65.682%" y2="23.865%" id="a"> + <stop stop-color="#fff" stop-opacity="0" offset="0%"/> + <stop stop-color="#fff" stop-opacity=".631" offset="63.146%"/> + <stop stop-color="#fff" offset="100%"/> + </linearGradient> + </defs> + <g fill="none" fill-rule="evenodd"> + <g transform="translate(1 1)"> + <path d="M36 18c0-9.94-8.06-18-18-18" id="Oval-2" stroke="url(#a)" stroke-width="2"> + <animateTransform + attributeName="transform" + type="rotate" + from="0 18 18" + to="360 18 18" + dur="0.9s" + repeatCount="indefinite" /> + </path> + <circle fill="#fff" cx="36" cy="18" r="1"> + <animateTransform + attributeName="transform" + type="rotate" + from="0 18 18" + to="360 18 18" + dur="0.9s" + repeatCount="indefinite" /> + </circle> + </g> + </g> +</svg> diff --git a/test/dune b/test/dune new file mode 100644 index 0000000..5d743c1 --- /dev/null +++ b/test/dune @@ -0,0 +1,2 @@ +(test + (name oasis_server)) diff --git a/test/oasis_server.ml b/test/oasis_server.ml new file mode 100644 index 0000000..e69de29