2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-06-02 09:45:00 +00:00
|
|
|
|
|
|
|
import
|
2022-06-28 10:22:59 +00:00
|
|
|
stew/results,
|
2022-06-02 09:45:00 +00:00
|
|
|
stew/shims/net,
|
2022-06-28 10:22:59 +00:00
|
|
|
chronicles,
|
|
|
|
chronos,
|
2022-06-02 09:45:00 +00:00
|
|
|
presto
|
|
|
|
|
|
|
|
|
2023-04-19 12:55:39 +00:00
|
|
|
type RestServerResult*[T] = Result[T, string]
|
2022-06-02 09:45:00 +00:00
|
|
|
|
|
|
|
|
2022-06-28 10:22:59 +00:00
|
|
|
### Configuration
|
2022-06-02 09:45:00 +00:00
|
|
|
|
2023-02-13 14:22:24 +00:00
|
|
|
type RestServerConf* = object
|
2022-06-02 09:45:00 +00:00
|
|
|
cacheSize*: Natural ## \
|
|
|
|
## The maximum number of recently accessed states that are kept in \
|
2023-02-13 14:22:24 +00:00
|
|
|
## memory. Speeds up requests obtaining information for consecutive
|
2022-06-02 09:45:00 +00:00
|
|
|
## slots or epochs.
|
|
|
|
|
|
|
|
cacheTtl*: Natural ## \
|
|
|
|
## The number of seconds to keep recently accessed states in memory
|
|
|
|
|
|
|
|
requestTimeout*: Natural ## \
|
|
|
|
## The number of seconds to wait until complete REST request will be received
|
|
|
|
|
|
|
|
maxRequestBodySize*: Natural ## \
|
|
|
|
## Maximum size of REST request body (kilobytes)
|
|
|
|
|
|
|
|
maxRequestHeadersSize*: Natural ## \
|
|
|
|
## Maximum size of REST request headers (kilobytes)
|
|
|
|
|
2022-06-28 10:22:59 +00:00
|
|
|
proc default*(T: type RestServerConf): T =
|
2022-06-02 09:45:00 +00:00
|
|
|
RestServerConf(
|
|
|
|
cacheSize: 3,
|
|
|
|
cacheTtl: 60,
|
|
|
|
requestTimeout: 0,
|
|
|
|
maxRequestBodySize: 16_384,
|
|
|
|
maxRequestHeadersSize: 64
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-28 10:22:59 +00:00
|
|
|
### Initialization
|
|
|
|
|
|
|
|
proc getRouter(allowedOrigin: Option[string]): RestRouter =
|
|
|
|
# TODO: Review this `validate` method. Check in nim-presto what is this used for.
|
|
|
|
proc validate(pattern: string, value: string): int =
|
|
|
|
## This is rough validation procedure which should be simple and fast,
|
|
|
|
## because it will be used for query routing.
|
|
|
|
if pattern.startsWith("{") and pattern.endsWith("}"): 0
|
|
|
|
else: 1
|
|
|
|
|
|
|
|
RestRouter.init(validate, allowedOrigin = allowedOrigin)
|
|
|
|
|
|
|
|
proc init*(T: type RestServerRef,
|
2022-06-02 09:45:00 +00:00
|
|
|
ip: ValidIpAddress, port: Port,
|
2022-06-28 10:22:59 +00:00
|
|
|
allowedOrigin=none(string),
|
|
|
|
conf=RestServerConf.default()): RestServerResult[T] =
|
2022-06-02 09:45:00 +00:00
|
|
|
let address = initTAddress(ip, port)
|
|
|
|
let serverFlags = {
|
|
|
|
HttpServerFlags.QueryCommaSeparatedArray,
|
|
|
|
HttpServerFlags.NotifyDisconnect
|
|
|
|
}
|
2023-02-13 14:22:24 +00:00
|
|
|
|
|
|
|
let
|
2022-06-02 09:45:00 +00:00
|
|
|
headersTimeout = if conf.requestTimeout == 0: chronos.InfiniteDuration
|
|
|
|
else: seconds(int64(conf.requestTimeout))
|
|
|
|
maxHeadersSize = conf.maxRequestHeadersSize * 1024
|
|
|
|
maxRequestBodySize = conf.maxRequestBodySize * 1024
|
|
|
|
|
|
|
|
let router = getRouter(allowedOrigin)
|
|
|
|
|
2022-06-28 10:22:59 +00:00
|
|
|
var res: RestResult[RestServerRef]
|
|
|
|
try:
|
|
|
|
res = RestServerRef.new(
|
2023-02-13 14:22:24 +00:00
|
|
|
router,
|
|
|
|
address,
|
2022-06-28 10:22:59 +00:00
|
|
|
serverFlags = serverFlags,
|
|
|
|
httpHeadersTimeout = headersTimeout,
|
|
|
|
maxHeadersSize = maxHeadersSize,
|
|
|
|
maxRequestBodySize = maxRequestBodySize
|
|
|
|
)
|
2023-04-19 12:55:39 +00:00
|
|
|
except CatchableError:
|
|
|
|
return err(getCurrentExceptionMsg())
|
2022-06-28 10:22:59 +00:00
|
|
|
|
2023-04-19 12:55:39 +00:00
|
|
|
# RestResult error type is cstring, so we need to map it to string
|
|
|
|
res.mapErr(proc(err: cstring): string = $err)
|
2023-02-13 14:22:24 +00:00
|
|
|
|
2022-06-28 10:22:59 +00:00
|
|
|
proc newRestHttpServer*(ip: ValidIpAddress, port: Port,
|
|
|
|
allowedOrigin=none(string),
|
|
|
|
conf=RestServerConf.default()): RestServerResult[RestServerRef] =
|
|
|
|
RestServerRef.init(ip, port, allowedOrigin, conf)
|