| Ambiorix | R Documentation |
Web server.
An object of class Ambiorix from which one can
add routes, routers, and run the application.
ambiorix::Routing -> Ambiorix
not_found404 Response, must be a handler function that accepts the request and the response, by default uses response_404().
error500 response when the route errors, must a handler function that accepts the request and the response, by default uses response_500().
on_stopCallback function to run when the app stops, takes no argument.
portPort to run the application.
hostHost to run the application.
limitMax body size, defaults to 5 * 1024 * 1024.
ambiorix::Routing$all()ambiorix::Routing$delete()ambiorix::Routing$engine()ambiorix::Routing$get()ambiorix::Routing$get_middleware()ambiorix::Routing$get_receivers()ambiorix::Routing$get_routes()ambiorix::Routing$options()ambiorix::Routing$patch()ambiorix::Routing$post()ambiorix::Routing$prepare()ambiorix::Routing$put()ambiorix::Routing$receive()ambiorix::Routing$use()new()Ambiorix$new(
host = getOption("ambiorix.host", "0.0.0.0"),
port = getOption("ambiorix.port", NULL),
log = getOption("ambiorix.logger", TRUE)
)hostA string defining the host.
portInteger defining the port, defaults to ambiorix.port option: uses a random port if NULL.
logWhether to generate a log of events.
Define the webserver.
cache_templates()Ambiorix$cache_templates()
Cache templates in memory instead of reading them from disk.
listen()Ambiorix$listen(port)
portPort number.
Specifies the port to listen on.
app <- Ambiorix$new()
app$listen(3000L)
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
set_404()Ambiorix$set_404(handler)
handlerFunction that accepts the request and returns an object
describing an httpuv response, e.g.: response().
Sets the 404 page.
app <- Ambiorix$new()
app$set_404(function(req, res){
res$send("Nothing found here")
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
set_error()Ambiorix$set_error(handler)
handlerFunction that accepts a request, response and an error object.
Sets the error handler.
# my custom error handler:
error_handler <- function(req, res, error) {
if (!is.null(error)) {
error_msg <- conditionMessage(error)
cli::cli_alert_danger("Error: {error_msg}")
}
response <- list(
code = 500L,
msg = "Uhhmmm... Looks like there's an error from our side :("
)
res$
set_status(500L)$
json(response)
}
# handler for GET at /whoami:
whoami <- function(req, res) {
# simulate error (object 'Pikachu' is not defined)
print(Pikachu)
}
app <- Ambiorix$
new()$
set_error(error_handler)$
get("/whoami", whoami)
if (interactive()) {
app$start(open = FALSE)
}
static()Ambiorix$static(path, uri = "www")
pathLocal path to directory of assets.
uriURL path where the directory will be available.
Static directories
start()Ambiorix$start(port = NULL, host = NULL, open = interactive())
portInteger defining the port, defaults to ambiorix.port option: uses a random port if NULL.
hostA string defining the host.
openWhether to open the app the browser.
Start Start the webserver.
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start(port = 3000L)
serialiser()Ambiorix$serialiser(handler)
handlerFunction to use to serialise.
This function should accept two arguments: the object to serialise and ....
Define Serialiser
app <- Ambiorix$new()
app$serialiser(function(data, ...){
jsonlite::toJSON(x, ..., pretty = TRUE)
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
stop()Ambiorix$stop()
Stop Stop the webserver.
print()Ambiorix$print()
clone()The objects of this class are cloneable with this method.
Ambiorix$clone(deep = FALSE)
deepWhether to make a deep clone.
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
app$on_stop <- function(){
cat("Bye!\n")
}
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$listen`
## ------------------------------------------------
app <- Ambiorix$new()
app$listen(3000L)
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$set_404`
## ------------------------------------------------
app <- Ambiorix$new()
app$set_404(function(req, res){
res$send("Nothing found here")
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
## ------------------------------------------------
## Method `Ambiorix$set_error`
## ------------------------------------------------
# my custom error handler:
error_handler <- function(req, res, error) {
if (!is.null(error)) {
error_msg <- conditionMessage(error)
cli::cli_alert_danger("Error: {error_msg}")
}
response <- list(
code = 500L,
msg = "Uhhmmm... Looks like there's an error from our side :("
)
res$
set_status(500L)$
json(response)
}
# handler for GET at /whoami:
whoami <- function(req, res) {
# simulate error (object 'Pikachu' is not defined)
print(Pikachu)
}
app <- Ambiorix$
new()$
set_error(error_handler)$
get("/whoami", whoami)
if (interactive()) {
app$start(open = FALSE)
}
## ------------------------------------------------
## Method `Ambiorix$start`
## ------------------------------------------------
app <- Ambiorix$new()
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start(port = 3000L)
## ------------------------------------------------
## Method `Ambiorix$serialiser`
## ------------------------------------------------
app <- Ambiorix$new()
app$serialiser(function(data, ...){
jsonlite::toJSON(x, ..., pretty = TRUE)
})
app$get("/", function(req, res){
res$send("Using {ambiorix}!")
})
if(interactive())
app$start()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.