aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/multiservice/Makefile10
-rw-r--r--examples/multiservice/barservice.proto13
-rw-r--r--examples/multiservice/client.nim44
-rw-r--r--examples/multiservice/fooservice.proto13
-rw-r--r--examples/multiservice/nim.cfg1
-rw-r--r--examples/multiservice/server.nim41
6 files changed, 122 insertions, 0 deletions
diff --git a/examples/multiservice/Makefile b/examples/multiservice/Makefile
new file mode 100644
index 0000000..5def82b
--- /dev/null
+++ b/examples/multiservice/Makefile
@@ -0,0 +1,10 @@
+all: server client
+
+server: server.nim fooservice_pb.nim fooservice_twirp.nim barservice_pb.nim barservice_twirp.nim
+ nim c server.nim
+
+client: client.nim fooservice_pb.nim fooservice_twirp.nim barservice_pb.nim barservice_twirp.nim
+ nim c client.nim
+
+%_pb.nim %_twirp.nim: %.proto
+ ../../nimtwirp/nimtwirp_build -I:. --out:. $^
diff --git a/examples/multiservice/barservice.proto b/examples/multiservice/barservice.proto
new file mode 100644
index 0000000..bc207f2
--- /dev/null
+++ b/examples/multiservice/barservice.proto
@@ -0,0 +1,13 @@
+syntax = "proto3";
+
+service Bar {
+ rpc MakeBar(BarReq) returns (BarResp);
+}
+
+message BarReq {
+ int32 a = 1;
+}
+
+message BarResp {
+ int32 b = 2;
+}
diff --git a/examples/multiservice/client.nim b/examples/multiservice/client.nim
new file mode 100644
index 0000000..9df9b9b
--- /dev/null
+++ b/examples/multiservice/client.nim
@@ -0,0 +1,44 @@
+import os
+import strformat
+import strutils
+import parseopt
+
+import fooservice_pb
+import fooservice_twirp
+
+import barservice_pb
+import barservice_twirp
+
+var service = "foo"
+var value: int
+
+for kind, key, val in getopt():
+ case kind
+ of cmdArgument:
+ value = parseInt(key)
+ of cmdLongOption, cmdShortOption:
+ case key
+ of "foo", "bar": service = key
+ else:
+ echo("error: unknown option: " & key)
+ quit(QuitFailure)
+ of cmdEnd: assert(false)
+
+if service == "foo":
+ var req = newFooReq()
+ req.a = int32(value)
+ let client = newFooClient("http://localhost:8081")
+ try:
+ let resp = MakeFoo(client, req)
+ echo(&"Response from foo: {resp.b}")
+ except Exception as exc:
+ echo(&"oh no: {exc.msg}")
+else:
+ var req = newBarReq()
+ req.a = int32(value)
+ let client = newBarClient("http://localhost:8081")
+ try:
+ let resp = MakeBar(client, req)
+ echo(&"Response from bar: {resp.b}")
+ except Exception as exc:
+ echo(&"oh no: {exc.msg}")
diff --git a/examples/multiservice/fooservice.proto b/examples/multiservice/fooservice.proto
new file mode 100644
index 0000000..08d985d
--- /dev/null
+++ b/examples/multiservice/fooservice.proto
@@ -0,0 +1,13 @@
+syntax = "proto3";
+
+service Foo {
+ rpc MakeFoo(FooReq) returns (FooResp);
+}
+
+message FooReq {
+ int32 a = 1;
+}
+
+message FooResp {
+ int32 b = 2;
+}
diff --git a/examples/multiservice/nim.cfg b/examples/multiservice/nim.cfg
new file mode 100644
index 0000000..1c2f0c1
--- /dev/null
+++ b/examples/multiservice/nim.cfg
@@ -0,0 +1 @@
+--path:"../.."
diff --git a/examples/multiservice/server.nim b/examples/multiservice/server.nim
new file mode 100644
index 0000000..983b1cd
--- /dev/null
+++ b/examples/multiservice/server.nim
@@ -0,0 +1,41 @@
+import asynchttpserver
+import asyncdispatch
+import random
+
+import nimtwirp/nimtwirp
+import nimtwirp/errors
+
+import fooservice_pb
+import fooservice_twirp
+
+import barservice_pb
+import barservice_twirp
+
+proc MakeFooImpl(service: Foo, req: FooReq): Future[FooResp] {.async.} =
+ result = newFooResp()
+ result.b = req.a * 2
+
+proc MakeBarImpl(service: Bar, req: BarReq): Future[BarResp] {.async.} =
+ result = newBarResp()
+ result.b = req.a * 3
+
+var
+ foo {.threadvar.}: Foo
+ bar {.threadvar.}: Bar
+
+foo = newFoo()
+foo.MakeFooImpl = MakeFooImpl
+
+bar = newBar()
+bar.MakeBarImpl = MakeBarImpl
+
+# You need to declare `nimtwirp.Settings`, which you must pass to
+# `twirpServices`. You must create a variable, because the twirpServices macro
+# will refer to this variable in the code it generates.
+var settings = nimtwirp.newSettings(Port(8081))
+
+twirpServices(settings):
+ foo
+ bar
+
+runForever()