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_found
404 Response, must be a handler function that accepts the request and the response, by default uses response_404()
.
error
500 response when the route errors, must a handler function that accepts the request and the response, by default uses response_500()
.
on_stop
Callback function to run when the app stops, takes no argument.
port
Port to run the application.
host
Host to run the application.
limit
Max 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) )
host
A string defining the host.
port
Integer defining the port, defaults to ambiorix.port
option: uses a random port if NULL
.
log
Whether 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)
port
Port 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)
handler
Function 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)
handler
Function that accepts a request, response and an error object.
Sets the error handler.
# my custom error handler: error_handler <- \(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 <- \(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")
path
Local path to directory of assets.
uri
URL path where the directory will be available.
Static directories
start()
Ambiorix$start(port = NULL, host = NULL, open = interactive())
port
Integer defining the port, defaults to ambiorix.port
option: uses a random port if NULL
.
host
A string defining the host.
open
Whether 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)
handler
Function 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)
deep
Whether 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 <- \(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 <- \(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.