aboutsummaryrefslogtreecommitdiff
path: root/examples/multiservice/server.nim
diff options
context:
space:
mode:
authorOskari Timperi <oskari.timperi@iki.fi>2018-04-21 21:49:50 +0100
committerOskari Timperi <oskari.timperi@iki.fi>2018-04-21 21:49:50 +0100
commit2fc0e16aea9e03b9f957c6781bd77de8127ebd69 (patch)
tree339f2004eeebf0737f5f5eaf3b592056097c0e98 /examples/multiservice/server.nim
parent4b058c8d8cc83f0e5edf73307530d8ea065d33f1 (diff)
downloadnimtwirp-2fc0e16aea9e03b9f957c6781bd77de8127ebd69.tar.gz
nimtwirp-2fc0e16aea9e03b9f957c6781bd77de8127ebd69.zip
Add another example showcasing multiple services
Diffstat (limited to 'examples/multiservice/server.nim')
-rw-r--r--examples/multiservice/server.nim41
1 files changed, 41 insertions, 0 deletions
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()