Skip to content

Commit

Permalink
Pull new version of nim-presto that implements RestServer' new error …
Browse files Browse the repository at this point in the history
…handler callback (#2144)

Added rest request error handler to capture calls on not installed endpoints
better, more descriptive error message returned.
  • Loading branch information
NagyZoltanPeter authored Oct 27, 2023
1 parent f7b9afc commit b8bcb1e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
35 changes: 34 additions & 1 deletion apps/wakunode2/app.nim
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,33 @@ proc startApp*(app: var App): AppResult[void] =
## Monitoring and external interfaces

proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNodeConf): AppResult[RestServerRef] =
let server = ? newRestHttpServer(address, port)

# Used to register api endpoints that are not currently installed as keys,
# values are holding error messages to be returned to the client
var notInstalledTab: Table[string, string] = initTable[string, string]()

proc requestErrorHandler(error: RestRequestError,
request: HttpRequestRef):
Future[HttpResponseRef] {.async.} =
case error
of RestRequestError.Invalid:
return await request.respond(Http400, "Invalid request", HttpTable.init())
of RestRequestError.NotFound:
let rootPath = request.rawPath.split("/")[1]
if notInstalledTab.hasKey(rootPath):
return await request.respond(Http404, notInstalledTab[rootPath], HttpTable.init())
else:
return await request.respond(Http400, "Bad request initiated. Invalid path or method used.", HttpTable.init())
of RestRequestError.InvalidContentBody:
return await request.respond(Http400, "Invalid content body", HttpTable.init())
of RestRequestError.InvalidContentType:
return await request.respond(Http400, "Invalid content type", HttpTable.init())
of RestRequestError.Unexpected:
return defaultResponse()

return defaultResponse()

let server = ? newRestHttpServer(address, port, requestErrorHandler = requestErrorHandler)

## Admin REST API
installAdminApiHandlers(server.router, app.node)
Expand All @@ -641,6 +667,8 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo
app.node.subscribe((kind: ContentSub, topic: contentTopic), some(autoHandler))

installRelayApiHandlers(server.router, app.node, cache)
else:
notInstalledTab["relay"] = "/relay endpoints are not available. Please check your configuration: --relay"

## Filter REST API
if conf.filternode != "" and
Expand All @@ -652,6 +680,9 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo

let filterCache = rest_filter_api.MessageCache.init()
rest_filter_api.installFilterRestApiHandlers(server.router, app.node, filterCache)
else:
notInstalledTab["filter"] = "/filter endpoints are not available. Please check your configuration: --filternode"


## Store REST API
installStoreApiHandlers(server.router, app.node)
Expand All @@ -660,6 +691,8 @@ proc startRestServer(app: App, address: ValidIpAddress, port: Port, conf: WakuNo
if conf.lightpushnode != "" and
app.node.wakuLightpushClient != nil:
rest_lightpush_api.installLightPushRequestHandler(server.router, app.node)
else:
notInstalledTab["lightpush"] = "/lightpush endpoints are not available. Please check your configuration: --lightpushnode"

server.start()
info "Starting REST HTTP server", url = "http://" & $address & ":" & $port & "/"
Expand Down
12 changes: 8 additions & 4 deletions waku/waku_api/rest/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ proc getRouter(allowedOrigin: Option[string]): RestRouter =
proc init*(T: type RestServerRef,
ip: ValidIpAddress, port: Port,
allowedOrigin=none(string),
conf=RestServerConf.default()): RestServerResult[T] =
conf=RestServerConf.default(),
requestErrorHandler: RestRequestErrorHandler = nil): RestServerResult[T] =
let address = initTAddress(ip, port)
let serverFlags = {
HttpServerFlags.QueryCommaSeparatedArray,
Expand All @@ -82,7 +83,8 @@ proc init*(T: type RestServerRef,
serverFlags = serverFlags,
httpHeadersTimeout = headersTimeout,
maxHeadersSize = maxHeadersSize,
maxRequestBodySize = maxRequestBodySize
maxRequestBodySize = maxRequestBodySize,
requestErrorHandler = requestErrorHandler
)
except CatchableError:
return err(getCurrentExceptionMsg())
Expand All @@ -92,5 +94,7 @@ proc init*(T: type RestServerRef,

proc newRestHttpServer*(ip: ValidIpAddress, port: Port,
allowedOrigin=none(string),
conf=RestServerConf.default()): RestServerResult[RestServerRef] =
RestServerRef.init(ip, port, allowedOrigin, conf)
conf=RestServerConf.default(),
requestErrorHandler: RestRequestErrorHandler = nil):
RestServerResult[RestServerRef] =
RestServerRef.init(ip, port, allowedOrigin, conf, requestErrorHandler)

0 comments on commit b8bcb1e

Please sign in to comment.