[FEATURE] Add additional constructor to the proxy (#108)

This commit is contained in:
KonradStaniec 2021-07-12 07:22:01 +02:00 committed by GitHub
parent 147ef3f562
commit a138c410c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 4 deletions

View File

@ -13,14 +13,22 @@ proc proxyCall(client: RpcHttpClient, name: string): RpcProc =
let res = await client.call(name, params)
return StringOfJson($res)
proc new*(T: type RpcHttpProxy, listenAddresses: openArray[string]): T {.raises: [Defect, CatchableError].}=
proc new*(T: type RpcHttpProxy, server: RpcHttpServer): T =
let client = newRpcHttpClient()
let router = RpcRouter.init()
T(rpcHttpClient: client, rpcHttpServer: newRpcHttpServer(listenAddresses, router))
T(rpcHttpClient: client, rpcHttpServer: server)
proc new*(T: type RpcHttpProxy, listenAddresses: openArray[TransportAddress]): T {.raises: [Defect, CatchableError].} =
RpcHttpProxy.new(newRpcHttpServer(listenAddresses, RpcRouter.init()))
proc new*(T: type RpcHttpProxy, listenAddresses: openArray[string]): T {.raises: [Defect, CatchableError].} =
RpcHttpProxy.new(newRpcHttpServer(listenAddresses, RpcRouter.init()))
proc newRpcHttpProxy*(listenAddresses: openArray[string]): RpcHttpProxy {.raises: [Defect, CatchableError].} =
RpcHttpProxy.new(listenAddresses)
proc newRpcHttpProxy*(listenAddresses: openArray[TransportAddress]): RpcHttpProxy {.raises: [Defect, CatchableError].} =
RpcHttpProxy.new(listenAddresses)
proc start*(proxy:RpcHttpProxy, proxyServerUrl: string) {.async.} =
proxy.rpcHttpServer.start()
await proxy.rpcHttpClient.connect(proxyServerUrl)
@ -43,4 +51,4 @@ proc stop*(rpcHttpProxy: RpcHttpProxy) {.raises: [Defect, CatchableError].} =
rpcHttpProxy.rpcHttpServer.stop()
proc closeWait*(rpcHttpProxy: RpcHttpProxy) {.async.} =
await rpcHttpProxy.rpcHttpServer.closeWait()
await rpcHttpProxy.rpcHttpServer.closeWait()

View File

@ -323,6 +323,11 @@ proc newRpcHttpServer*(addresses: openArray[string], router: RpcRouter): RpcHttp
result = newRpcHttpServer(router)
result.addStreamServers(addresses)
proc newRpcHttpServer*(addresses: openArray[TransportAddress], router: RpcRouter): RpcHttpServer =
## Create new server and assign it to addresses ``addresses``.
result = newRpcHttpServer(router)
result.addStreamServers(addresses)
proc start*(server: RpcHttpServer) =
## Start the RPC server.
for item in server.servers: