Skip to content

Commit 7d86215

Browse files
committed
WIP
1 parent a669c98 commit 7d86215

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

src/state/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@
4040
(name learnocaml_store)
4141
(wrapped false)
4242
(modules Learnocaml_store)
43-
(libraries cryptokit lwt_utils learnocaml_api)
43+
(libraries cryptokit lwt_utils learnocaml_api irmin irmin-git irmin-git.unix)
4444
)

src/state/learnocaml_store.ml

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ module Exercise = struct
299299

300300
end
301301

302-
module Session = struct
302+
module Session1 = struct
303303

304304
include Session
305305

@@ -348,6 +348,110 @@ module Session = struct
348348

349349
end
350350

351+
module Session = struct
352+
353+
include Session
354+
355+
let logfailwith str arg =
356+
Printf.printf "[ERROR] %s (%s)\n%!" str arg;
357+
failwith str
358+
359+
let generate_random_hex len =
360+
Cryptokit.Random.string Cryptokit.Random.secure_rng len
361+
|> Cryptokit.transform_string @@ Cryptokit.Hexa.encode ()
362+
363+
(* module type IndexKV = functor (Store: Irmin.S) -> sig
364+
type token = Learnocaml_data.Token.t
365+
type t
366+
367+
val parse : [> `O of (string * [> `String of 'a ]) list ] -> 'a
368+
val serialise : 'a -> [> `O of (string * [> `String of 'a ]) list ]
369+
val read :
370+
Store.path list -> (Store.contents -> 'a) -> string -> 'a list Lwt.t
371+
val write :
372+
Store.path list ->
373+
('a -> Store.contents) -> 'a list -> string -> unit Lwt.t
374+
val create_index : string -> unit Lwt.t
375+
val exists : t
376+
val remove : t
377+
end *)
378+
379+
(* module AUTH: IndexKV = struct *)
380+
(* objectif 1: explorer la doc des différents modules ci-dessous,
381+
avec C-c C-l, C-c C-a et dans le browser:
382+
https://ocaml.org/p/irmin-git/latest/doc/Irmin_git/index.html
383+
https://github.com/mirage/irmin/blob/main/examples/irmin_git_store.ml
384+
objectif 2 : refactorer le code ci-dessous commenté
385+
pour utiliser Store.set_exn au lieu du Json_encoding.obj3...
386+
*)
387+
module Store = Irmin_git.KV.Make(Irmin.Contents.Json_value)
388+
module Info = Irmin_unix.Info(Store.Info)
389+
390+
let read keys parse path=
391+
let config = Irmin_git.config ~bare:true path in
392+
let* repo = Store.Repo.v config in
393+
let* t = Store.main repo in
394+
Lwt_list.map_p
395+
(fun key ->
396+
let+ x = Store.get t key in parse x)
397+
keys
398+
399+
let write keys serialise data_list path=
400+
let config = Irmin_git.config ~bare:true path in
401+
let* repo = Store.Repo.v config in
402+
let* t = Store.main repo in
403+
Lwt_list.iter_p
404+
(fun (key,data) ->
405+
Store.set_exn t ~info:(Info.v "message") key
406+
(*deal with the errors if using `set` instead of `set_exn`*)
407+
(serialise data))
408+
@@ List.combine keys data_list
409+
410+
(* let file = "sessions.json" *)
411+
412+
(* let enc = *)
413+
(* let open Json_encoding in *)
414+
(* list (obj3 *)
415+
(* (req "session" Session.enc) *)
416+
(* (req "token" Token.enc) *)
417+
(* (req "last_connection" float)) *)
418+
419+
(* let path dir = Filename.concat dir file *)
420+
421+
(* let load dir = *)
422+
(* let p = path dir in *)
423+
(* Lwt_unix.file_exists dir >>= fun dir_exists -> *)
424+
(* (if not dir_exists then Lwt_unix.mkdir dir 0o700 else Lwt.return_unit) >>= fun () -> *)
425+
(* Lwt_unix.file_exists p >>= function *)
426+
(* | false -> *)
427+
(* Printf.printf "No session file, creating empty list\n%!"; *)
428+
(* Lwt.return [] *)
429+
(* | true -> *)
430+
(* Printf.printf "Loading sessions from: %s\n%!" p; *)
431+
(* get_from_file enc p *)
432+
433+
(* let save dir table = *)
434+
(* write_to_file enc table (path dir) *)
435+
436+
(* let get_user_token session = *)
437+
(* load !data_dir >>= fun table -> *)
438+
(* match List.find_opt (fun (s, _, _) -> s = session) table with *)
439+
(* | Some (_, token, _) -> Lwt.return_some token *)
440+
(* | None -> Lwt.return_none *)
441+
442+
(* let set_session session token = *)
443+
(* let now = Unix.gettimeofday () in *)
444+
(* load !data_dir >>= fun table -> *)
445+
(* let table = (session, token, now) :: table in *)
446+
(* save !data_dir table *)
447+
448+
(* let gen_session () = *)
449+
(* let len = 32 in *)
450+
(* Cryptokit.Random.string Cryptokit.Random.secure_rng len *)
451+
(* |> Cryptokit.transform_string @@ Cryptokit.Hexa.encode () *)
452+
453+
end
454+
351455
module Token = struct
352456

353457
include Token

0 commit comments

Comments
 (0)