mirror of
https://github.com/codex-storage/nim-json-rpc.git
synced 2025-02-22 07:58:22 +00:00
31 lines
1.1 KiB
Nim
31 lines
1.1 KiB
Nim
import rpcserver, tables, chronicles, strformat
|
|
export rpcserver
|
|
|
|
type
|
|
RpcHttpServer* = RpcServer[StreamServer]
|
|
|
|
defineRpcTransport(httpProcessClient):
|
|
write:
|
|
let
|
|
msg = &"Host: {$client.localAddress} Content-Type: application/json-rpc Content-Length: {$value.len} {value}"
|
|
debug "Http write", msg = msg
|
|
client.write(msg)
|
|
afterRead:
|
|
# TODO: read: remove http to allow json validation
|
|
debug "Http read", msg = value
|
|
|
|
proc newRpcHttpServer*(addresses: openarray[TransportAddress]): RpcHttpServer =
|
|
## Create new server and assign it to addresses ``addresses``.
|
|
result = newRpcServer[StreamServer]()
|
|
result.addStreamServers(addresses, httpProcessClient)
|
|
|
|
proc newRpcHttpServer*(addresses: openarray[string]): RpcHttpServer =
|
|
## Create new server and assign it to addresses ``addresses``.
|
|
result = newRpcServer[StreamServer]()
|
|
result.addStreamServers(addresses, httpProcessClient)
|
|
|
|
proc newRpcHttpServer*(address = "localhost", port: Port = Port(8545)): RpcHttpServer =
|
|
result = newRpcServer[StreamServer]()
|
|
result.addStreamServer(address, port, httpProcessClient)
|
|
|