nim-json-rpc/json_rpc/server.nim

38 lines
969 B
Nim
Raw Normal View History

import
std/[json, macros],
chronos, chronicles,
./router,
./jsonmarshal
2018-06-14 16:52:41 +01:00
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 new(T: type RpcServer): T =
T(router: RpcRouter.init())
proc newRpcServer*(): RpcServer {.deprecated.} = RpcServer.new()
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] {.gcsafe.} =
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