@@ -302,50 +302,50 @@ end
302302module Session = struct
303303
304304 include Session
305+ open Lwt.Syntax
305306
306- let file = " sessions.json"
307+ module Store = Irmin_git_unix.FS. KV (Irmin.Contents. Json_value )
308+ module Info = Irmin_git_unix. Info (Store. Info )
307309
308- let enc =
309- let open Json_encoding in
310- list (obj3
311- (req " session" Session. enc)
312- (req " token" Token. enc)
313- (req " last_connection" float ))
314-
315- let path dir = Filename. concat dir file
316-
317- let load dir =
318- let p = path dir in
319- Lwt_unix. file_exists dir >> = fun dir_exists ->
320- (if not dir_exists then Lwt_unix. mkdir dir 0o700 else Lwt. return_unit) >> = fun () ->
321- Lwt_unix. file_exists p >> = function
322- | false ->
323- Printf. printf " No session file, creating empty list\n %!" ;
324- Lwt. return []
325- | true ->
326- Printf. printf " Loading sessions from: %s\n %!" p;
327- get_from_file enc p
310+ let repo_path = ref " ./session_store.git"
328311
329- let save dir table =
330- write_to_file enc table (path dir)
312+ let config () = Irmin_git. config ~bare: true ! repo_path
331313
332- let get_user_token session =
333- load ! data_dir >> = fun table ->
334- match List. find_opt (fun (s , _ , _ ) -> s = session) table with
335- | Some (_ , token , _ ) -> Lwt. return_some token
336- | None -> Lwt. return_none
314+ type entry = {
315+ session : Session .t ;
316+ token : Token .t ;
317+ last_connection : float ;
318+ }
319+
320+ let enc =
321+ let open Json_encoding in
322+ conv
323+ (fun {session; token; last_connection} -> (session, token, last_connection))
324+ (fun (session , token , last_connection ) -> {session; token; last_connection})
325+ (obj3
326+ (req " session" Session. enc)
327+ (req " token" Token. enc)
328+ (req " last_connection" float ))
337329
338330 let set_session session token =
339331 let now = Unix. gettimeofday () in
340- load ! data_dir >> = fun table ->
341- let table = (session, token, now) :: table in
342- save ! data_dir table
332+ let * repo = Store.Repo. v (config () ) in
333+ let * t = Store. main repo in
334+ Store. set_exn t ~info: (Info. v " Set session/token" ) [Session. to_string session] (Json_encoding. construct enc {session; token; last_connection = now})
335+
336+ let get_user_token session =
337+ let * repo = Store.Repo. v (config () ) in
338+ let * t = Store. main repo in
339+ Store. find t [Session. to_string session] > |= function
340+ | Some value ->
341+ let entry = Json_encoding. destruct enc value in
342+ Some entry.token
343+ | None -> None
343344
344345 let gen_session () =
345346 let len = 32 in
346347 Cryptokit.Random. string Cryptokit.Random. secure_rng len
347- |> Cryptokit. transform_string @@ Cryptokit.Hexa. encode ()
348-
348+ |> Cryptokit. transform_string @@ Cryptokit.Hexa. encode ()
349349end
350350
351351module Token = struct
0 commit comments