-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathMiddleware.Rmd
More file actions
150 lines (120 loc) · 4.65 KB
/
Copy pathMiddleware.Rmd
File metadata and controls
150 lines (120 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
---
title: "Middleware"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Middleware}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
results= 'markup'
)
```
## Middleware
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.
## Logging
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:
```{r}
library(RestRserve)
app = Application$new(content_type = "application/json")
backend = BackendRserve$new()
app$add_get("/foo", function(.req, .res) {
body = RestRserve::to_json(.req$parameters_query)
.res$set_body(body)
# specify that there is no need to specially encode the body as
# we've already set it to a JSON
.res$encode = identity
})
```
See it in action:
```{r}
req = Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
resp = app$process_request(req)
resp$body
```
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`:
```{r}
logging_middleware = Middleware$new(
process_request = function(.req, .res) {
msg = list(
middleware = "logging_middleware",
request_id = .req$id,
request = list(headers = .req$headers, method = .req$method, path = .req$path),
timestamp = Sys.time()
)
msg = RestRserve::to_json(msg)
cat(msg, sep = '\n')
},
process_response = function(.req, .res) {
msg = list(
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()
)
msg = to_json(msg)
cat(msg, sep = '\n')
},
id = "logging"
)
app$append_middleware(logging_middleware)
```
Let's test again:
```{r}
req = Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
resp = app$process_request(req)
```
Let's see what will happen if we will send request to nonexistent endpoint:
```{r}
req = Request$new(path = "/foo2", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
resp = app$process_request(req)
```
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.
## Middleware order
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.
<img src="img/middleware-order.png" width="320"/>
## Compression
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`.
```{r}
gzip_middleware = Middleware$new(
process_request = function(.req, .res) {
msg = list(
middleware = "gzip_middleware",
request_id = .req$id,
timestamp = Sys.time()
)
msg = to_json(msg)
cat(msg, sep = '\n')
},
process_response = function(.req, .res) {
# compress body
.res$set_header("Content-encoding", "gzip")
.res$set_body(memCompress(.res$body, "gzip"))
msg = list(
middleware = "gzip_middleware",
request_id = .req$id,
timestamp = Sys.time()
)
msg = to_json(msg)
cat(msg, sep = '\n')
},
id = "gzip"
)
app$append_middleware(gzip_middleware)
```
```{r}
req = Request$new(path = "/foo", method = "GET", parameters_query = list(key1 = "value1", key2 = "value2"))
resp = app$process_request(req)
```
And now check what is actual decoded response body:
```{r}
rawToChar(memDecompress(resp$body, "gzip"))
```