Many web API frameworks contain a concept called “middleware” (but every language/framework calls it differently - filters, middleware, etc). Essentially, the middleware performs some specific function on the HTTP request or response before or after the handler. Common tasks to offload to a middleware would be logging, authorization, body compression, etc.
RestRserve
comes with several build-in middlewares (AuthMiddleware
, CORSMiddleware
, ETagMiddleware
) and generic Middleware
class which facilitates user to create a custom middleware.
Let’s see it in action in example below.
Let’s say you have a simple app which has only a single endpoint - it simply convert query string parameters into a JSON format and sends it back:
library(RestRserve)
= Application$new(content_type = "application/json")
app
= BackendRserve$new()
backend
$add_get("/foo", function(.req, .res) {
app= RestRserve::to_json(.req$parameters_query)
body $set_body(body)
.res# specify that there is no need to specially encode the body as
# we've already set it to a JSON
$encode = identity
.res })
See it in action:
= Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
req = app$process_request(req)
resp $body
resp#> [1] "{\"key1\":\"value1\",\"key2\":\"value2\"}"
Assume you would like to analyze how your web service works. For that you may need log every request and response in order to see whether service replies with errors and what can cause these errors. This is a perfect task for a middleware and here is how you can achieve this with RestRserve
:
= Middleware$new(
logging_middleware process_request = function(.req, .res) {
= list(
msg middleware = "logging_middleware",
request_id = .req$id,
request = list(headers = .req$headers, method = .req$method, path = .req$path),
timestamp = Sys.time()
)= RestRserve::to_json(msg)
msg cat(msg, sep = '\n')
},process_response = function(.req, .res) {
= list(
msg middleware = "logging_middleware",
# we would like to have a request_id for each response in order to correlate
# request and response
request_id = .req$id,
response = list(headers = .res$headers, status_code = .res$status_code, body = .res$body),
timestamp = Sys.time()
)= to_json(msg)
msg cat(msg, sep = '\n')
},id = "logging"
)
$append_middleware(logging_middleware) app
Let’s test again:
= Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
req = app$process_request(req)
resp #> {"middleware":"logging_middleware","request_id":"7611709a-e7d8-11ec-b9c1-4c327594fb6f","request":{"headers":{},"method":"GET","path":"/foo"},"timestamp":"2022-06-09 13:42:22"}
#> {"middleware":"logging_middleware","request_id":"7611709a-e7d8-11ec-b9c1-4c327594fb6f","response":{"headers":{"Server":"RestRserve/1.2.0; Rserve/1.8.6"},"status_code":200,"body":"{\"key1\":\"value1\",\"key2\":\"value2\"}"},"timestamp":"2022-06-09 13:42:22"}
Let’s see what will happen if we will send request to nonexistent endpoint:
= Request$new(path = "/foo2", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
req = app$process_request(req)
resp #> {"middleware":"logging_middleware","request_id":"76130022-e7d8-11ec-b9c1-4c327594fb6f","request":{"headers":{},"method":"GET","path":"/foo2"},"timestamp":"2022-06-09 13:42:22"}
#> {"middleware":"logging_middleware","request_id":"76130022-e7d8-11ec-b9c1-4c327594fb6f","response":{"headers":{"Server":"RestRserve/1.2.0; Rserve/1.8.6"},"status_code":404,"body":"404 Not Found"},"timestamp":"2022-06-09 13:42:22"}
Later you will see all the responses with errors in the log (status_code
>= 400). Also you will be able to find corresponding requests by inspecting request_id
field.
It is important to understand that middlewares are executed in order you’ve added them (that’s why it is called append_middleware
). Flow is shown on the diagram below.
To demonstrate the order in which middleware called let’s consider another example.
Sometimes it it useful to compress response body in order to send less data over the wire. Here we will implement a middleware which will compress response with gzip
.
= Middleware$new(
gzip_middleware process_request = function(.req, .res) {
= list(
msg middleware = "gzip_middleware",
request_id = .req$id,
timestamp = Sys.time()
)= to_json(msg)
msg cat(msg, sep = '\n')
},process_response = function(.req, .res) {
# compress body
$set_header("Content-encoding", "gzip")
.res$set_body(memCompress(.res$body, "gzip"))
.res
= list(
msg middleware = "gzip_middleware",
request_id = .req$id,
timestamp = Sys.time()
)= to_json(msg)
msg cat(msg, sep = '\n')
},id = "gzip"
)$append_middleware(gzip_middleware) app
= Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
req = app$process_request(req)
resp #> {"middleware":"logging_middleware","request_id":"7616daf8-e7d8-11ec-b9c1-4c327594fb6f","request":{"headers":{},"method":"GET","path":"/foo"},"timestamp":"2022-06-09 13:42:22"}
#> {"middleware":"gzip_middleware","request_id":"7616daf8-e7d8-11ec-b9c1-4c327594fb6f","timestamp":"2022-06-09 13:42:22"}
#> {"middleware":"gzip_middleware","request_id":"7616daf8-e7d8-11ec-b9c1-4c327594fb6f","timestamp":"2022-06-09 13:42:22"}
#> {"middleware":"logging_middleware","request_id":"7616daf8-e7d8-11ec-b9c1-4c327594fb6f","response":{"headers":{"Server":"RestRserve/1.2.0; Rserve/1.8.6","Content-encoding":"gzip"},"status_code":200,"body":"eJyrVspOrTRUslIqS8wpTTVU0gHxjWB8I6VaAK3KCjs="},"timestamp":"2022-06-09 13:42:22"}
And now check what is actual decoded response body:
rawToChar(memDecompress(resp$body, "gzip"))
#> [1] "{\"key1\":\"value1\",\"key2\":\"value2\"}"