From 5a2f5bf9a12b270e15757df48afeb1c1db5b34b3 Mon Sep 17 00:00:00 2001 From: Mateus Cruz Date: Mon, 5 Feb 2024 03:31:35 -0300 Subject: add db queries --- bin/dune | 10 +--------- bin/main.ml | 14 ++++++++++---- 2 files changed, 11 insertions(+), 13 deletions(-) (limited to 'bin') diff --git a/bin/dune b/bin/dune index a186150..9c34236 100644 --- a/bin/dune +++ b/bin/dune @@ -5,17 +5,9 @@ (:standard -cclib -static -cclib -no-pie)) (libraries rinha - piaf routes eio_main - caqti-driver-postgresql logs.fmt fmt.tty - logs.threaded - caqti-eio - caqti - ppx_rapper_eio) - - (preprocess - (pps ppx_rapper))) + logs.threaded)) diff --git a/bin/main.ml b/bin/main.ml index 7e537a7..99cc0e0 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -4,9 +4,9 @@ open! StdLabels open Eio open Piaf -let request_handler Server.{ request; _ } = +let request_handler ~db_pool Server.{ request; _ } = match Rinha.Router.match_route request.meth request.target with - | Some handler -> handler request + | Some handler -> handler db_pool request | None -> Response.create `Not_found ;; @@ -17,10 +17,16 @@ let () = @@ fun sw -> let config = let interface = Net.Ipaddr.V4.any in - let port = 3000 in + let port = 8080 in `Tcp (interface, port) in let config = Server.Config.create config in - let server = Server.create ~config request_handler in + let db_uri = + Uri.make ~scheme:"postgres" ~userinfo:"admin:123" ~host:"db" ~path:"rinha" () + in + let db_pool = + Result.get_ok @@ Caqti_eio.connect_pool ~sw ~stdenv:(env :> Caqti_eio.stdenv) db_uri + in + let server = Server.create ~config (request_handler ~db_pool) in ignore @@ Server.Command.start ~sw env server ;; -- cgit v1.2.3