2018-06-21 18:16:06 +01:00
|
|
|
import rpcserver, tables, chronicles, strformat
|
2018-06-19 18:21:37 +01:00
|
|
|
export rpcserver
|
|
|
|
|
|
|
|
type
|
|
|
|
RpcHttpServer* = RpcServer[StreamServer]
|
|
|
|
|
2018-06-21 18:16:06 +01:00
|
|
|
defineRpcTransport(httpProcessClient):
|
|
|
|
write:
|
|
|
|
let
|
|
|
|
msg = &"Host: {$client.localAddress} Content-Type: application/json-rpc Content-Length: {$value.len} {value}"
|
2018-06-21 18:39:47 +01:00
|
|
|
debug "Http write", msg = msg
|
2018-06-21 18:16:06 +01:00
|
|
|
client.write(msg)
|
2018-06-21 18:39:47 +01:00
|
|
|
afterRead:
|
|
|
|
# TODO: read: remove http to allow json validation
|
|
|
|
debug "Http read", msg = value
|
2018-06-19 18:21:37 +01:00
|
|
|
|
|
|
|
proc newRpcHttpServer*(addresses: openarray[TransportAddress]): RpcHttpServer =
|
|
|
|
## Create new server and assign it to addresses ``addresses``.
|
2018-06-21 18:16:06 +01:00
|
|
|
result = newRpcServer[StreamServer]()
|
|
|
|
result.addStreamServers(addresses, httpProcessClient)
|
2018-06-19 18:21:37 +01:00
|
|
|
|
|
|
|
proc newRpcHttpServer*(addresses: openarray[string]): RpcHttpServer =
|
|
|
|
## Create new server and assign it to addresses ``addresses``.
|
2018-06-21 18:16:06 +01:00
|
|
|
result = newRpcServer[StreamServer]()
|
|
|
|
result.addStreamServers(addresses, httpProcessClient)
|
2018-06-19 18:21:37 +01:00
|
|
|
|
|
|
|
proc newRpcHttpServer*(address = "localhost", port: Port = Port(8545)): RpcHttpServer =
|
2018-06-21 18:16:06 +01:00
|
|
|
result = newRpcServer[StreamServer]()
|
|
|
|
result.addStreamServer(address, port, httpProcessClient)
|
2018-06-19 18:21:37 +01:00
|
|
|
|