diff --git a/examples/server/client.ml b/examples/server/client.ml new file mode 100644 index 0000000000..41106a36cc --- /dev/null +++ b/examples/server/client.ml @@ -0,0 +1,72 @@ +(* + * Copyright (c) 2023 Tarides + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *) + +open Lwt.Syntax + +module Client = + Irmin_client_unix.Make_codec (Irmin_server.Conn.Codec.Bin) (Config.Store) + +module Info = Irmin_client_unix.Info (Client.Info) + +let info msg = Info.v ~author:"tester" msg + +let main () = + let* client = Client.connect Config.uri in + let* main = Client.main client in + let* () = + Client.set_exn ~info:(info "set testing") main [ "testing" ] "testing" + in + let* () = + Client.set_exn ~info:(info "set remove") main [ "remove" ] "remove" + in + let* () = Client.remove_exn ~info:(info "remove remove") main [ "remove" ] in + let batch = + Client.Batch.( + v () + |> add_value [ "a"; "b"; "c" ] "123" + |> add_value [ "foo" ] "bar" + |> remove [ "testing" ]) + in + let* c = Client.Batch.apply ~info:(info "apply batch") ~path:[] main batch in + Logs.info (fun l -> + l "Applied batch -> commit %a" Irmin.Type.(pp Client.commit_key_t) c); + + let* abc = Client.get main [ "a"; "b"; "c" ] in + assert (String.equal abc "123"); + + let* foo = Client.get main [ "foo" ] in + assert (foo = "bar"); + + let* testing = Client.find main [ "testing" ] in + assert (Option.is_none testing); + + let* remove = Client.mem main [ "remove" ] in + assert (remove = false); + + let* commit = Client.Commit.of_key client c in + let tree = Client.Commit.tree (Option.get commit) in + let* concrete = Client.Tree.to_concrete tree in + + Logs.info (fun l -> l "%a" Irmin.Type.(pp Client.Tree.concrete_t) concrete); + + Lwt.return_unit + +let () = + Fmt_tty.setup_std_outputs (); + Logs.(set_level @@ Some Debug); + Irmin.Export_for_backends.Logging.reporter (module Mtime_clock) + |> Logs.set_reporter; + Lwt_main.run @@ main () diff --git a/examples/server/config.ml b/examples/server/config.ml new file mode 100644 index 0000000000..ba20953903 --- /dev/null +++ b/examples/server/config.ml @@ -0,0 +1,19 @@ +(* + * Copyright (c) 2023 Tarides + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *) + +module Store = Irmin_mem.KV.Make (Irmin.Contents.String) + +let uri = Uri.of_string "tcp://localhost:4242" diff --git a/examples/server/dune b/examples/server/dune new file mode 100644 index 0000000000..010d6d915e --- /dev/null +++ b/examples/server/dune @@ -0,0 +1,12 @@ +(executables + (names client server) + (libraries + irmin + irmin-client.unix + irmin-server.unix + irmin-git.unix + irmin-pack.unix + logs + unix + fmt.tty + hex)) diff --git a/examples/server/server.ml b/examples/server/server.ml new file mode 100644 index 0000000000..0856e791ae --- /dev/null +++ b/examples/server/server.ml @@ -0,0 +1,35 @@ +(* + * Copyright (c) 2023 Tarides + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *) + +open Lwt.Syntax + +module Server = + Irmin_server_unix.Make_ext (Irmin_server.Conn.Codec.Bin) (Config.Store) + +let main () = + let config = Irmin_mem.config () in + let dashboard = `TCP (`Port 1234) in + let uri = Config.uri in + let* server = Server.v ~uri ~dashboard config in + Logs.debug (fun l -> l "Listening on %a@." Uri.pp uri); + Server.serve server + +let () = + Fmt_tty.setup_std_outputs (); + Logs.(set_level @@ Some Debug); + Irmin.Export_for_backends.Logging.reporter (module Mtime_clock) + |> Logs.set_reporter; + Lwt_main.run @@ main ()