Skip to content

Commit

Permalink
Add implementation of PRESTO middleware. (#73)
Browse files Browse the repository at this point in the history
* Add implementation of PRESTO middleware and tests for it.

* Address review comments.

* Recover log statements which still has sense.
  • Loading branch information
cheatfate authored Feb 21, 2024
1 parent 046bd4a commit 223aade
Show file tree
Hide file tree
Showing 13 changed files with 475 additions and 309 deletions.
2 changes: 1 addition & 1 deletion presto/agent.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

{.push raises: [].}

import strutils
import std/strutils

const
PrestoName* = "nim-presto"
Expand Down
2 changes: 1 addition & 1 deletion presto/client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import std/[macros, options, uri, sequtils]
import chronos, chronos/apps/http/[httpcommon, httptable, httpclient]
import chronicles except error
import httputils, stew/base10
import segpath, common, macrocommon, agent
import "."/[segpath, common, macrocommon, agent]
export chronos, httpclient, httptable, httpcommon, options, agent, httputils
export SocketFlags

Expand Down
33 changes: 33 additions & 0 deletions presto/middleware.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# REST API framework implementation
# (c) Copyright 2021-Present
# Status Research & Development GmbH
#
# Licensed under either of
# Apache License, version 2.0, (LICENSE-APACHEv2)
# MIT license (LICENSE-MIT)

{.push raises: [].}

import chronos, chronos/apps/http/httpserver
import "."/[route, servercommon, serverprivate]
export httpserver, servercommon, serverprivate

proc new*(
t: typedesc[RestServerMiddlewareRef],
router: RestRouter,
errorHandler: RestRequestErrorHandler = nil): HttpServerMiddlewareRef =

proc middlewareCallback(
middleware: HttpServerMiddlewareRef,
request: RequestFence,
handler: HttpProcessCallback2): Future[HttpResponseRef] {.
async: (raises: [CancelledError], raw: true).} =
let restmw = RestServerMiddlewareRef(middleware)
restmw.nextHandler = handler
processRestRequest[RestServerMiddlewareRef](restmw, request)

let middleware =
RestServerMiddlewareRef(router: router, errorHandler: errorHandler,
handler: middlewareCallback)
HttpServerMiddlewareRef(middleware)
4 changes: 1 addition & 3 deletions presto/route.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import std/[macros, options]
import chronos, chronos/apps/http/[httpcommon, httptable, httpserver]
import httputils
import stew/bitops2
import btrees
import common, segpath, macrocommon

import "."/[btrees, common, segpath, macrocommon]
export chronos, options, common, httpcommon, httptable, httpserver

when defined(metrics):
Expand Down
2 changes: 1 addition & 1 deletion presto/secureserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import std/options
import chronos, chronos/apps/http/shttpserver
import chronicles
import stew/results
import route, common, segpath, servercommon, serverprivate, agent
import "."/[route, common, segpath, servercommon, serverprivate, agent]
export options, chronos, shttpserver, servercommon, chronicles, agent

type
Expand Down
2 changes: 1 addition & 1 deletion presto/segpath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import std/[uri, strutils]
import stew/[bitops2, results]
import chronos/apps
import common
import "."/common
export common, apps

type
Expand Down
2 changes: 1 addition & 1 deletion presto/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import std/[options, json, strutils]
import chronos, chronos/apps/http/httpserver
import chronicles
import stew/results
import route, common, segpath, servercommon, serverprivate, agent
import "."/[route, common, segpath, servercommon, serverprivate, agent]
export options, chronos, httpserver, servercommon, chronicles, agent

type
Expand Down
9 changes: 7 additions & 2 deletions presto/servercommon.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import std/options
import chronos, chronos/apps/http/httpserver
import chronicles
import common
export chronicles, options
import "."/[common, route]
export chronicles, options, httpserver

chronicles.formatIt(HttpTable):
var res = newSeq[string]()
Expand Down Expand Up @@ -45,3 +45,8 @@ type
error: RestRequestError,
request: HttpRequestRef): Future[HttpResponseRef] {.
async: (raises: [CancelledError]).}

RestServerMiddlewareRef* = ref object of HttpServerMiddlewareRef
router*: RestRouter
errorHandler*: RestRequestErrorHandler
nextHandler*: HttpProcessCallback2
Loading

0 comments on commit 223aade

Please sign in to comment.