summaryrefslogtreecommitdiff
path: root/bin/main.ml
blob: 3f112007d2ad89d3d495cdda7ee42747fbb334fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
open Eio
open Piaf

let request_handler ~db_pool Server.{ request; _ } =
  match Rinha.Router.match_route request.meth request.target with
  | Some handler -> Result.get_ok @@ handler db_pool request
  | None -> Response.create `Not_found
;;

let () =
  Eio_main.run
  @@ fun env ->
  Switch.run
  @@ fun sw ->
  let config =
    let interface = Net.Ipaddr.V4.any in
    let port = 8080 in
    `Tcp (interface, port)
  in
  let config = Server.Config.create config in
  let db_uri =
    Uri.make ~scheme:"postgres" ~userinfo:"admin:123" ~host:"db" ~path:"rinha" ()
  in
  let db_pool =
    Result.get_ok
    @@ Caqti_eio_unix.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
;;