Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanGibb committed Mar 8, 2023
0 parents commit e199b6e
Show file tree
Hide file tree
Showing 9 changed files with 301 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
_*
result
11 changes: 11 additions & 0 deletions capnp-rpc-demo.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
opam-version: "2.0"
depends: [
"ocaml" {>= "4.08.0"}
"eio_main"
"capnp-rpc-lwt"
"logs"
]
build: [
["dune" "build" "-p" name "-j" jobs]
["dune" "runtest" "-p" name "-j" jobs] {with-test}
]
9 changes: 9 additions & 0 deletions dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(executable
(name main)
(libraries eio_main capnp-rpc-lwt logs.fmt)
(flags (:standard -w -53-55)))

(rule
(targets echo_api.ml echo_api.mli)
(deps echo_api.capnp)
(action (run capnp compile -o %{bin:capnpc-ocaml} %{deps})))
3 changes: 3 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 2.9)
(name capnp-rpc-demo)
(formatting disabled)
29 changes: 29 additions & 0 deletions echo.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(* $MDX part-begin=server *)
module Api = Echo_api.MakeRPC(Capnp_rpc_lwt)

open Capnp_rpc_lwt

let local =
let module Echo = Api.Service.Echo in
Echo.local @@ object
inherit Echo.service

method ping_impl params release_param_caps =
let open Echo.Ping in
let msg = Params.msg_get params in
release_param_caps ();
let response, results = Service.Response.create Results.init_pointer in
Results.reply_set results ("echo:" ^ msg);
Service.return response
end
(* $MDX part-end *)

(* $MDX part-begin=client *)
module Echo = Api.Client.Echo

let ping t msg =
let open Echo.Ping in
let request, params = Capability.Request.create Params.init_pointer in
Params.msg_set params msg;
Capability.call_for_value_exn t method_id request |> Results.reply_get
(* $MDX part-end *)
5 changes: 5 additions & 0 deletions echo_api.capnp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@0xb287252b6cbed46e;

interface Echo {
ping @0 (msg :Text) -> (reply :Text);
}
167 changes: 167 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 64 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
inputs = {
opam-nix.url = "github:tweag/opam-nix";
flake-utils.url = "github:numtide/flake-utils";
opam-nix.inputs.nixpkgs.follows = "nixpkgs";
};
outputs = { self, nixpkgs, flake-utils, opam-nix, ... }@inputs:
let package = "capnp-rpc-demo";
in flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
opam-nix-lib = opam-nix.lib.${system};
devPackagesQuery = {
ocaml-lsp-server = "*";
ocamlformat = "*";
};
query = {
ocaml-base-compiler = "*";
};
overlay = final: prev: {
"${package}" = prev.${package}.overrideAttrs (_: {
# Prevent the ocaml dependencies from leaking into dependent environments
doNixSupport = false;
nativeBuildInputs = [ pkgs.dune_3 ];
});
};
resolved-scope =
let scope = opam-nix-lib.buildOpamProject' { } ./. (query // devPackagesQuery); in
scope.overrideScope' overlay;
materialized-scope =
# to generate:
# nix shell github:tweag/opam-nix#opam-nix-gen -c opam-nix-gen -p ocaml-lsp-server -p ocamlformat -p ocaml-base-compiler aeon . package-defs.json
let scope = opam-nix-lib.materializedDefsToScope { sourceMap.${package} = ./.; } ./package-defs.json; in
scope.overrideScope' overlay;
in rec {
packages = {
resolved = resolved-scope;
materialized = materialized-scope;
default = resolved-scope.${package};
};
defaultPackage = packages.default;

devShells =
let
mkDevShell = scope:
let
devPackages = builtins.attrValues
(pkgs.lib.getAttrs (builtins.attrNames devPackagesQuery) scope);
in pkgs.mkShell {
inputsFrom = [ scope.${package} ];
buildInputs = devPackages;
};
in rec {
resolved = mkDevShell resolved-scope;
materialized = mkDevShell materialized-scope;
default = resolved;
};
}) // {
nixosModules.default = {
imports = [ ./module.nix ];
};
};
}

11 changes: 11 additions & 0 deletions main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
open Eio.Std

let () =
Logs.set_level (Some Logs.Warning);
Logs.set_reporter (Logs_fmt.reporter ())

let () =
Eio_main.run @@ fun _ ->
let service = Echo.local in
let reply = Echo.ping service "foo" in
traceln "Got reply %S" reply

0 comments on commit e199b6e

Please sign in to comment.