db.httpd: Respond to HTTP requests against a database

View source: R/db-httpd.R

db.httpdR Documentation

Respond to HTTP requests against a database

Description

Uses R's built in http server in src/modules/Rhttp.c

Usage

db.httpd(path, reqquery, reqbody, reqheaders)

Arguments

path

Request URL path.

reqquery

Request query parameters.

reqbody

Request body.

reqheaders

Request headers.

Details

The URL path will always have a form that looks like /custom/<name>/<head>/<tail>, where <name> is the name of the database. Each of these parts can be extracted from the path using the function db.urlpath. Note that <tail> may consist of multiple parts. db.httpd will use the path <head> to dispatch to a function of that name, which is expected to generate the http response. This behavior follows closely the Wapp framework (wpp.tcl.tk). The function will receive the database connection, a named list of request data, and a response object that it is expected to fill out using the db.reply* family of functions.

Examples

## Not run: 

index = function(db, req, resp) {
    db.reply(resp, db.subst(db,
    '
       <html>
       <body>
       <h1>Welcome!</h1>
       <ul>
       <li><a href="<%req$PATH_ROOT%>/page1">page 1</a></li>
       <li><a href="<%req$PATH_ROOT%>/page2">page 2</a></li>
       <ul>
       </body>
       </html>
   '))
}
page1 = function(db, req, resp) {
    db.reply(resp, db.subst(db, '
        <html>
        <body>
        <a href="<%req$PATH_ROOT%>">hello, world!</a>
        </body>
        </html>
    '))
}
page2 = function(db, req, resp) {
    db.reply(resp, db.subst(db, '
        <html>
        <body>
        <a href="<%req$PATH_ROOT%>">take me home, please.</a>
        </body>
        </html>
    '))
}

db = db.open("db.sqlite",
    views=list(default=index, page1=page1, page2=page2))

db.ui(db)

db.close(db)
unlink("db.sqlite")


## End(Not run)

blueraleigh/db documentation built on Feb. 25, 2024, 9:13 a.m.