WIP server

This commit is contained in:
Nathan Anderson
2023-08-13 22:44:10 -06:00
commit ed29333a87
17 changed files with 214 additions and 0 deletions
+31
View File
@@ -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
+3
View File
@@ -0,0 +1,3 @@
(library
(name oasis_server)
(libraries dream lwt_ppx caqti-lwt caqti-driver-sqlite3))
+2
View File
@@ -0,0 +1,2 @@
let add x y = x + y
let sub x y = x - y
+5
View File
@@ -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)