nim-json-rpc/json_rpc/server.nim

36 lines
953 B
Nim
Raw Normal View History

import json, tables, options, macros
import chronos, router, chronicles
2018-06-14 16:52:41 +01:00
import jsonmarshal
export chronos, json, jsonmarshal, router, chronicles
2018-06-14 16:52:41 +01:00
type
RpcServer* = ref object of RootRef
router*: RpcRouter
2018-06-14 16:52:41 +01:00
proc newRpcServer*(): RpcServer =
new result
result.router = newRpcRouter()
2018-06-15 11:12:34 +01:00
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)
2018-06-14 16:52:41 +01:00
# Wrapper for message processing
proc route*(server: RpcServer, line: string): Future[string] {.async, gcsafe.} =
result = await server.router.route(line)
2018-06-14 16:52:41 +01:00
# Server registration
2018-06-14 16:52:41 +01:00
proc register*(server: RpcServer, name: string, rpc: RpcProc) =
## Add a name/code pair to the RPC server.
server.router.register(name, rpc)
2018-06-14 16:52:41 +01:00
proc unRegisterAll*(server: RpcServer) =
# Remove all remote procedure calls from this server.
server.router.clear