mirror of
https://github.com/status-im/nim-json-rpc.git
synced 2025-02-24 18:28:10 +00:00
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.
37 lines
938 B
Nim
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
|
|
|
|
|