App: Web Application

Description Super class Active bindings Methods

Description

Overview

Web Application object based on fiery::Fire via Fire and contains the following modifications:

Methods

Details

Fiery Documentation

Super class

webtools::Configurable -> App

Active bindings

plugins

read-only field for plugins attached

fire

read-only field for the fire object

handles

read-only field for the handles attached

routers

read-only field for the routers attached

statics

read-only field for the paths attached

data

alternative way of accessing application data

sockets

read-only list of active websockets

events

help information

Methods

Public methods

Inherited methods

Method new()

Create a new Application

Usage
App$new(
  ...,
  host = "0.0.0.0",
  port = 3838L,
  root = "",
  headers = NULL,
  client_id_converter = function(request) {     request$trust <- TRUE    
    return(request$ip) },
  path_extractor = function(msg, bin) "/",
  refresh_rate = 0.001,
  refresh_rate_nb = 0.001,
  trigger_dir = NULL,
  access_log_format = common_log_format,
  logger = if (interactive()) {     logger_console() } else {     logger_null() }
)
Arguments
...

(Plugin)s to attach to this Application Instance

host

(str) IP address; defaults to all addresses.

port

(int) port number from 0 to 65535.

root

(str) path at which fiery will serve requests

headers

(lst) global header that will be set on all responses

client_id_converter

(fun) function that converts a request into a client ID

path_extractor

(fun) function that converts a message to a path on which to dispatch

refresh_rate

(num) blocking refresh rate for run cycles

refresh_rate_nb

(num) non-blocking refresh rate for run cycles

trigger_dir

(path) directory in which you can save RDS files of lists where the external-event trigger will be triggered with the list as arguments. See events.

access_log_format

(str/fn) provider of the logging service.

logger

(fn) logger for this application.


Method browse()

Browse to the App URL

Usage
App$browse()

Method finalize()

Finalizer

Usage
App$finalize()

Method lifecycle_start()

Start the application

Usage
App$lifecycle_start(..., block = FALSE, browse = FALSE, silent = FALSE)
Arguments
...

(args) passed to the start or resume handler

block

(flag) blocking server

browse

(flag) open a browser after starting

silent

(flag) do not log to console


Method lifecycle_stop()

Stop the application

Usage
App$lifecycle_stop()

Method lifecycle_is_running()

Checks if the app is still running

Usage
App$lifecycle_is_running()

Method set_client_id_converter()

Set client converter

Usage
App$set_client_id_converter(client_id_converter)
Arguments
client_id_converter

(fun) takes request and returns client ID


Method set_path_extractor()

Set the path extractor

Usage
App$set_path_extractor(extractor)
Arguments
extractor

function that takes the message and returns a path


Method set_headers()

Set the heaers to be sent with every response

Usage
App$set_headers(..., .list = NULL)
Arguments
...

(arg) key-value pairs

.list

(lst) provide the values as a list


Method set_logger()

Set the logger function

Usage
App$set_logger(logger)
Arguments
logger

(fun) logger function; there are some presets:

  • logger_null() - only error, warning, message events

  • logger_console() - everything to console

  • logger_file() - everything to file

  • logger_switch() - file or console dependeing on need

Can also be any function that takes arguments: event, message, request = NULL, time = Sys.time(), and ....


Method set_data()

set data inside the app object; list merge semantics

Usage
App$set_data(name, value)
Arguments
name

(str) name of the data

value

(arg) key value data


Method get_data()

get data inside the app object

Usage
App$get_data(name)
Arguments
name

(str) name of the data


Method clear_data()

clear all data in side the app object

Usage
App$clear_data()

Method log()

Send an application log

Usage
App$log(event, message, request = NULL, ..., .envir = parent.frame())
Arguments
event

(str) event attached

message

(str) glue string

request

(req) reqres::Request object

...

(arg) extra arguments

.envir

(env) calling environment


Method handle()

Attach a handler to an event

Usage
App$handle(event, name, handler, ..., position = NULL)
Arguments
event

(str) name of the event to respond to

name

(str) human-readable name for easy identification

handler

(fun) handler for the event

...

(arg) key value pairs of attributes to attach to handler

position

(int) position in the handler stack; default last


Method handle_find()

Find a handler to an event

Usage
App$handle_find(..., discard = FALSE)
Arguments
...

(arg) extra arguments

discard

(flg) if TRUE, then selected is discarded instead of kept


Method handle_remove()

Remove a handler

Usage
App$handle_remove(..., discard = FALSE)
Arguments
...

(arg) extra arguments

discard

(flg) if TRUE, then selected is discarded instead of kept


Method handle_lifecycle()

Lifecycle Handler

Triggered once per server invocation. Return value is ignored.

Arguments

Events

Usage
App$handle_lifecycle(
  event = c("start", "resume", "end"),
  name,
  handler,
  ...,
  position = NULL
)
Arguments
event

(str) name of the event to respond to

name

(str) human-readable name for easy identification

handler

(fun) handler for the event

...

(arg) key value pairs of attributes to attach to handler

position

(int) position in the handler stack; default last


Method handle_cycle()

Cycle Handler

Triggered for every event loop. Return value is ignored.

Arguments

Events

Usage
App$handle_cycle(
  event = c("start", "end"),
  name,
  handler,
  ...,
  position = NULL
)
Arguments
event

(str) name of the event to respond to

name

(str) human-readable name for easy identification

handler

(fun) handler for the event

...

(arg) key value pairs of attributes to attach to handler

position

(int) position in the handler stack; default last


Method handle_request()

Request Handler

Triggered by actual request made to the web server.

Arguments

Events

Usage
App$handle_request(
  event = c("header", "before", "request", "after"),
  name,
  handler,
  ...,
  position = NULL
)
Arguments
event

(str) name of the event to respond to

name

(str) human-readable name for easy identification

handler

(fun) handler for the event

...

(arg) key value pairs of attributes to attach to handler

position

(int) position in the handler stack; default last


Method handle_ws()

Websocket Event Handler

Arguments

Events

Usage
App$handle_ws(
  event = c("before", "message", "after", "send", "closed"),
  name,
  handler,
  ...,
  position = NULL
)
Arguments
event

(str) name of the event to respond to

name

(str) human-readable name for easy identification

handler

(fun) handler for the event

...

(arg) key value pairs of attributes to attach to handler

position

(int) position in the handler stack; default last


Method handle_trigger()

handle Triggers an event for the handlers

Usage
App$handle_trigger(event, ..., check = TRUE)
Arguments
event

(str) event name

...

(arg) args passed to handler

check

(flg) check whether this is protected


Method router()

Adds a route handler

Usage
App$router(
  path,
  handler,
  ...,
  name = path,
  methods = "get",
  attach = "request",
  priority = 0L,
  replace = FALSE
)
Arguments
path

(str) endpoint

handler

(fun) handler; must have arguments request, response, keys, ....

...

(dta) extra metadata to identify thion_atts route.

name

(str) human-readable friendly name

methods

(chr) combination of GET, POST, PUT, PATCH, DELETE

attach

(str) event to attach. defaults to request.

priority

(int) relative positioning of the handler; lower will be executed first.

replace

(flg) whether or not to replace same name or error out


Method router_find()

Find routers based on metadata

Usage
App$router_find(
  ...,
  attach = c("request", "message", "header"),
  discard = FALSE
)
Arguments
...

(sel) selector expressions

attach

(str) whether to use the request, message, or header stack

discard

(flg) if TRUE, then selected is discarded instead of kept


Method router_remove()

Remove routers based on metadata

Usage
App$router_remove(
  ...,
  attach = c("request", "message", "header"),
  discard = FALSE
)
Arguments
...

(arg) extra arguments

attach

(str) whether to use the request, message, or header stack

discard

(flg) if TRUE, then selected is discarded instead of kept


Method static()

Adds a route for server static files

Usage
App$static(
  path,
  file,
  ...,
  name = path,
  index_html = TRUE,
  fallthrough = FALSE,
  html_charset = "utf-8",
  headers = list(),
  validation = character(0),
  replace = FALSE
)
Arguments
path

(url) URL path to serve

file

(pth) directory to serve on that path

...

(dta) extra metadata to identify this static path.

name

(str) human-readable friendly name

index_html

(flg) whether or not to serve when index.html exists

fallthrough

(flg) if not available, use R callback

html_charset

(str) html charset, defaults to UTF-8

headers

(lst) headers that are included with the response

validation

(chr) character vector of headers needed

replace

(flg) if already exists, overwrite


Method static_find()

Find static paths based on metadata

Usage
App$static_find(..., discard = FALSE)
Arguments
...

(arg) extra arguments

discard

(flg) if TRUE, then selected is discarded instead of kept


Method static_remove()

Remove static paths based on metadata

Usage
App$static_remove(..., discard = FALSE)
Arguments
...

(arg) extra arguments

discard

(flg) if TRUE, then selected is discarded instead of kept


Method ws_send()

Send a websocket message

Usage
App$ws_send(message, id)
Arguments
message

(lst) websocket message

id

(str) client identifier; all clients if missing (for send)


Method ws_close()

Close a websocket connection

Usage
App$ws_close(id)
Arguments
id

(str) client identifier


Method plugin_attach()

Attach a plugin

Usage
App$plugin_attach(plugin, ..., force = FALSE, bind = TRUE, clean = TRUE)
Arguments
plugin

(Plugin) instance

...

(arg) passed to the plugin's on_attach method

force

(flg) force to attach even if there is already existing

bind

(flg) add an active binding to fetch the plugin itself

clean

(flg) remove the attach methods once they have already been run to clean up the interface


Method plugin_detach()

Detach a plugin

Usage
App$plugin_detach(name, ...)
Arguments
name

(str) name of the plugin to detach

...

(arg) extra arguments


Method plugin_detach_all()

Detach all plugins

Usage
App$plugin_detach_all(...)
Arguments
...

(arg) extra arguments


Method plugin_has()

Check if a plugin of a class is attached

Usage
App$plugin_has(class)
Arguments
class

(str) class of the plugin


Method plugin_get()

Check if a plugin of a class is attached

Usage
App$plugin_get(class)
Arguments
class

(str) class of the plugin


Method print()

print method

Usage
App$print()

tjpalanca/webtools documentation built on Dec. 23, 2021, 11 a.m.