summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rw-r--r--bin/dune15
-rw-r--r--bin/server.ml53
2 files changed, 68 insertions, 0 deletions
diff --git a/bin/dune b/bin/dune
new file mode 100644
index 0000000..6f90a60
--- /dev/null
+++ b/bin/dune
@@ -0,0 +1,15 @@
+(executable
+ (public_name bs5)
+ (name server)
+ (modules server)
+ (libraries
+ ; local
+ lib
+ ;
+ piaf
+ routes
+ eio_main
+ logs.fmt
+ fmt.tty
+ logs.threaded
+ caqti-driver-sqlite3))
diff --git a/bin/server.ml b/bin/server.ml
new file mode 100644
index 0000000..b5cdd84
--- /dev/null
+++ b/bin/server.ml
@@ -0,0 +1,53 @@
+open Eio
+open Piaf
+
+let setup_log ?style_renderer level =
+ Logs_threaded.enable ();
+ Fmt_tty.setup_std_outputs ?style_renderer ();
+ Logs.set_level ~all:true level;
+ Logs.set_reporter (Logs_fmt.reporter ())
+;;
+
+let request_handler ~db_pool Server.{ request; _ } =
+ match Lib.Router.match_route request.meth request.target with
+ | Some handler -> Result.get_ok @@ handler db_pool request
+ | None ->
+ Logs.info (fun d -> d "the fuck %S\n" request.target);
+ Response.create `Not_found
+;;
+
+let () =
+ setup_log (Some Logs.Info);
+ Eio_main.run
+ @@ fun env ->
+ Switch.run
+ @@ fun sw ->
+ let config =
+ let interface = Net.Ipaddr.V4.any in
+ let port = 4455 in
+ `Tcp (interface, port)
+ in
+ let config = Server.Config.create config in
+ let db_uri =
+ Uri.make ~scheme:"sqlite3" ~path:"/home/y/code/ocaml/bs5/bulkdata/blog.db" ()
+ in
+ (* Create connection pool with initialization function *)
+ let connect_pool ~sw ~stdenv uri =
+ Caqti_eio_unix.connect_pool
+ ~sw
+ ~stdenv
+ ~post_connect:(fun conn ->
+ (* Initialize each connection with SQLite performance pragmas *)
+ match Lib.Query.init_connection conn with
+ | Ok () -> Ok ()
+ | Error err -> Error err)
+ uri
+ in
+ match connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with
+ (* match Caqti_eio_unix.connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri with *)
+ | Ok pool ->
+ let server = Server.create ~config (request_handler ~db_pool:pool) in
+ ignore @@ Server.Command.start ~sw env server
+ | Error err ->
+ Logs.err (fun m -> m "Error connecting to database: %a" Caqti_error.pp err)
+;;