Learn R Programming

Rook (version 1.2)

Middleware-class: Class Middleware

Description

An abstract class for building Rook Middleware applications. Middleware applications either handle the incoming web request or hand off the request to the Rook app defined in the field of the same name.

Arguments

Methods

set_app(app):

app is a Rook application that will handle the request if this Middleware app does not.

See Also

The following classes implement Middleware: Brewery and Static.

Examples

Run this code
# Middleware applications are typically instantiated in the argument list of
# Builder$new(), but here is stand-alone example.
# 
# Once your browser loads the app, you will see something like this in
# your location bar: http://127.0.0.1:28649/custom/middle. Add '/foo'
# onto the end of that and reload.

setRefClass(
    'FooBar',
    contains = 'Middleware',
    methods = list(
        initialize = function(...){
            # app to defer to.
            callSuper(app=App$new(function(env){
                res <- Response$new()
                res$write("I'm the deferred app.")
                res$finish()
            }))
        },
        call = function(env){
            req <- Request$new(env)
            res <- Response$new()
            if (length(grep('foo',req$path_info()))){
                res$write("I'm the middleware app.")        
                return(res$finish())
            } else {
                app$call(env)
            }
        }
    )
)
s <- Rhttpd$new()
if (FALSE) {
s$start(quiet=TRUE)
}
s$add(name="middle",app=getRefClass('FooBar')$new())
if (FALSE) {
s$browse('middle') # Opens a browser window to the app.
}
s$remove(all=TRUE)
rm(s)

Run the code above in your browser using DataLab