nim-json-rpc/json_rpc/server.nim
Zahary Karadjov d19de19128
Refactor the router to allow bypassing the use of std/json
The users will be now able to write RPC handlers returning the
distinct type `StringOfJson`. This will bypass any use of the
standard library's JsonNode type and its serialization routines.

The change was motivated by the integration of JSON-RPC in
status-im/nim-beacon-chain where most of the data types cannot
be handled by Nim's std lib or json-rpc's jsonmarshal module.
2020-03-17 20:38:52 +02:00

37 lines
938 B
Nim

import json, macros
import chronos, router, chronicles
import jsonmarshal
export chronos, json, jsonmarshal, router, chronicles
type
RpcServer* = ref object of RootRef
router*: RpcRouter
proc newRpcServer*(): RpcServer =
new result
result.router = newRpcRouter()
template rpc*(server: RpcServer, path: string, body: untyped): untyped =
server.router.rpc(path, body)
template hasMethod*(server: RpcServer, methodName: string): bool =
server.router.hasMethod(methodName)
# Wrapper for message processing
proc route*(server: RpcServer, line: string): Future[string] {.async, gcsafe.} =
result = await server.router.route(line)
# Server registration
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
## Add a name/code pair to the RPC server.
server.router.register(name, rpc)
proc unRegisterAll*(server: RpcServer) =
# Remove all remote procedure calls from this server.
server.router.clear