# http_server.nim
{.push gcsafe, raises: [].}
import json_rpc/rpcserver
import ./rpc_format
export rpcserver
proc setupServer(srv: RpcServer) =
srv.rpc(RpcConv):
proc hello(input: string): string =
"Hello " & input
proc bye(input {.serializedFieldName: "user-name".}: string): string =
"Bye " & input
proc `🙂`(input: string): string =
"🙂 " & input
proc empty(): void =
echo "nothing"
proc justHello(): string =
"Hello"
proc teaPot(): void =
raise (ref ApplicationError)(
code: 418, data: Opt.none(JsonString), msg: "I'm a teapot"
)
proc startServer*(): RpcHttpServer {.raises: [JsonRpcError].} =
let srv = newRpcHttpServer(["127.0.0.1:0"])
srv.setupServer()
srv.start()
srv
proc stopServer*(srv: RpcHttpServer) {.async.} =
await srv.stop()
await srv.closeWait()
proc main() {.raises: [JsonRpcError].} =
let srv = startServer()
runForever()
# Pass -d:jsonRpcExample to nim to run this
when defined(jsonRpcExample):
main()