From ed29333a87d28bb2f4329a1a74a136ef7371d918 Mon Sep 17 00:00:00 2001 From: Nathan Anderson 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 "

You got me!

"); + ] 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*wu7TB?L{5d6qAcEc9u1cfFNdQ^#N`&Z{dmi9 zW@Rsik0wsFxvURz>H3*Yg{_<|XSvH$lK7qX-~2(r + + + + Peimono + + + + + + + + +

Hello there!!

+ + +
+ + + \ 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 @@ + + + + + Page Title + + + + + + + +

Hello there!!

+ + + \ 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 @@ + + + + + + + + + + + + + + + + + + + + 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