Skip to content

Commit

Permalink
feat: Adds req$NEXT() function to middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
andyquinterom committed Oct 16, 2024
1 parent 4930a08 commit 4f9df57
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions R/service.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,29 @@ build_http_handler <- function(tower) {
app_handler <- compiler::cmpfun(
tower$app$httpHandler,
options = compiler_options

)

# If only the app handler exists return it
if (length(tower$http_layers) == 0) {
return(compiler::cmpfun(app_handler, options = compiler_options))
}

http_layers <- append(tower$http_layers, app_handler)
handler <- function(req) {
for (layer in http_layers) {
response <- layer(req)
if (!is.null(response)) {
return(response)
}
}
next_fn <- compiler::cmpfun(
function(req) {
req$LAYER_COUNTER <- req$LAYER_COUNTER + 1
http_layers[[req$LAYER_COUNTER]](req)
},
options = compiler_options
)

handler <- function(request) {
request$LAYER_COUNTER <- 0
request$NEXT <- next_fn
request$NEXT(request)
}

return(compiler::cmpfun(handler, options = compiler_options))
}

Expand Down

0 comments on commit 4f9df57

Please sign in to comment.