Creates application - RestRserve usage starts from here
Creates application - RestRserve usage starts from here
Creates Application object. Application provides an interface for building high-performance REST API by registering R functions as handlers http requests.
Details
There are several advanced options to control how HTTP headers are processed:
There is also an option to switch-off runtime types validation in the Request/Response handlers. This might provide some performance gains, but ultimately leads to less robust applications. Use at your own risk! See options("RestRserve.runtime.asserts")
Examples
# init loggerapp_logger = Logger$new()# set log level for the middlewareapp_logger$set_log_level("debug")# set logger nameapp_logger$set_name("MW Logger")# init middleware to loggingmw = Middleware$new( process_request =function(rq, rs){ app_logger$info(sprintf("Incomming request (id %s): %s", rq$id, rq$path))}, process_response =function(rq, rs){ app_logger$info(sprintf("Outgoing response (id %s): %s", rq$id, rs$status))}, id ="awesome-app-logger")# init applicationapp = Application$new(middleware = list(mw))# set internal log levelapp$logger$set_log_level("error")# define simply request handlerstatus_handler =function(rq, rs){ rs$set_body("OK") rs$set_content_type("text/plain") rs$set_status_code(200L)}# add routeapp$add_get("/status", status_handler,"exact")# add static file handlerdesc_file = system.file("DESCRIPTION", package ="RestRserve")# add routeapp$add_static("/desc", desc_file,"text/plain")# define say message handlersay_handler =function(rq, rs){ who = rq$parameters_path[["user"]] msg = rq$parameters_query[["message"]]if(is.null(msg)) msg ="Hello" rs$set_body(paste(who,"say", dQuote(msg))) rs$set_content_type("text/plain") rs$set_status_code(200L)}# add routeapp$add_get("/say/{user}", say_handler,"regex")# print application infoapp
# test app# simulate requestsnot_found_rq = Request$new(path ="/no")status_rq = Request$new(path ="/status")desc_rq = Request$new(path ="/desc")say_rq = Request$new(path ="/say/anonym", parameters_query = list("message"="Hola"))# process prepared requestsapp$process_request(not_found_rq)app$process_request(status_rq)app$process_request(desc_rq)app$process_request(say_rq)# run appbackend = BackendRserve$new()if(interactive()){ backend$start(app,8080)}
See Also
HTTPError Middleware
Request Response
Public fields
logger: Logger object which records events during request processing. Alternatively user can use loggers from lgr package as a drop-in replacement - Logger methods and loggers created by lgr share function signatures.
content_type: Default response body content type.
HTTPError: Class which raises HTTP errors. Global HTTPError is used by default. In theory user can replace it with his own class (see RestRserve:::HTTPErrorFactory). However we believe in the majority of the cases using HTTPError will be enough.
Active bindings
endpoints: Prints all the registered routes with allowed methods.
method: HTTP method. Allowed methods at the moment: GET, HEAD, POST, PUT, DELETE, OPTIONS, PATCH.
FUN: User function to handle requests. FUN must take two arguments: first is request (Request ) and second is response (Response ).
The goal of the user function is to modify `response` or throw exception (call `raise()` or `stop()`).
Both `response` and `request` objects modified in-place and internally passed further to RestRserve execution pipeline.
match: Defines how route will be processed. Allowed values:
* `exact` - match route as is. Returns 404 if route is not matched.
* `partial` - match route as prefix. Returns 404 if prefix are not matched.
* `regex` - match route as template. Returns 404 if template pattern not matched.
...: Not used.
Method add_get()
Shorthand to Application$add_route() with GET method.
FUN: User function to handle requests. FUN must take two arguments: first is request (Request ) and second is response (Response ).
The goal of the user function is to modify `response` or throw exception (call `raise()` or `stop()`).
Both `response` and `request` objects modified in-place and internally passed further to RestRserve execution pipeline.
match: Defines how route will be processed. Allowed values:
* `exact` - match route as is. Returns 404 if route is not matched.
* `partial` - match route as prefix. Returns 404 if prefix are not matched.
* `regex` - match route as template. Returns 404 if template pattern not matched.
...: Not used.
add_head: Adds HEAD method.
Method add_post()
Shorthand to Application$add_route() with POST method.
Usage
Application$add_post(path, FUN, match = c("exact", "partial", "regex"), ...)
Arguments
path: Endpoint path.
FUN: User function to handle requests. FUN must take two arguments: first is request (Request ) and second is response (Response ).
The goal of the user function is to modify `response` or throw exception (call `raise()` or `stop()`).
Both `response` and `request` objects modified in-place and internally passed further to RestRserve execution pipeline.
match: Defines how route will be processed. Allowed values:
* `exact` - match route as is. Returns 404 if route is not matched.
* `partial` - match route as prefix. Returns 404 if prefix are not matched.
* `regex` - match route as template. Returns 404 if template pattern not matched.
...: Not used.
Method add_static()
Adds GET method to serve file or directory at file_path.
If `content_type = NULL` then MIME code `content_type` will be inferred automatically (from file extension).
If it will be impossible to guess about file type then `content_type` will be set to `application/octet-stream`.
...: Not used.
Method add_openapi()
Adds endpoint to serve OpenAPI description of available methods.