blob: f28889142cfdbf14d9a30518d46a6ba81269ba43 (
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 Litedb.Router.match_route request.meth request.target with
| Some handler -> Result.get_ok @@ handler db_pool request
| None ->
Logs.info (fun d -> d "Não encontrei %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/combattant/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 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)
;;
|