Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Proxy server

# proxy_server.nim

{.push gcsafe, raises: [].}

import json_rpc/[rpcserver, rpcproxy]
import ./rpc_format

export rpcproxy

proc setupServer(proxy: var RpcProxy) =
  proxy.registerProxyMethod("hello")

  proxy.rpc(RpcConv):
    proc bye(input: string): string =
      "Proxy Bye " & input

proc startProxy*(srvUrl: string): Future[RpcProxy] {.async.} =
  var proxy = RpcProxy.new(["127.0.0.1:0"], getHttpClientConfig(srvUrl))
  proxy.setupServer()
  await proxy.start()
  proxy

proc stopProxy*(proxy: RpcProxy) {.async.} =
  await proxy.stop()
  await proxy.closeWait()

proc main() {.raises: [CatchableError].} =
  # Compile with -d:srvUrl="http://hostname:port"
  const srvUrl {.strdefine.}: string = ""
  let proxy = waitFor startProxy(srvUrl)
  runForever()

# Compile with -d:jsonRpcExample to run this
when defined(jsonRpcExample):
  main()