Middleware-class: Class 'Middleware'

Description Methods See Also Examples

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.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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("<h1>I'm the deferred app.</h1>")
                res$finish()
            }))
        },
        call = function(env){
            req <- Request$new(env)
            res <- Response$new()
            if (length(grep('foo',req$path_info()))){
                res$write("<h1>I'm the middleware app.</h1>")        
                return(res$finish())
            } else {
                app$call(env)
            }
        }
    )
)
s <- Rhttpd$new()
## Not run: 
s$start(quiet=TRUE)

## End(Not run)
s$add(name="middle",app=getRefClass('FooBar')$new())
## Not run: 
s$browse('middle') # Opens a browser window to the app.

## End(Not run)
s$remove(all=TRUE)
rm(s)

jeffreyhorner/Rook documentation built on May 19, 2019, 4:01 a.m.