# 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 howdy(input: string): string {.async: (raises: []).} =
"Howdy " & 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 {.raises: [ApplicationError].} =
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: (raises: []).} =
await srv.stop()
await srv.closeWait()
proc main() {.raises: [JsonRpcError].} =
let srv = startServer()
runForever()
when isMainModule:
main()