roadster 0.9.0-alpha.6

A "Batteries Included" web framework for rust designed to get you moving fast.
Documentation
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! Middleware to log the request/response payloads. Logs at the debug level.

use crate::app::context::AppContext;
use crate::error::RoadsterResult;
use crate::service::http::middleware::Middleware;
use axum::body::{Body, Bytes};
use axum::extract::{FromRef, Request, State};
use axum::http::header::CONTENT_TYPE;
use axum::http::{HeaderValue, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use axum::{Router, middleware};
use http_body_util::BodyExt;
use mime::Mime;
use serde_derive::{Deserialize, Serialize};
use serde_with::{DisplayFromStr, serde_as};
use std::collections::{BTreeSet, HashMap};
use std::str::FromStr;
use tracing::debug;
use validator::Validate;

pub use RequestResponseLoggingConfig as ReqResLoggingConfig;
pub use RequestResponseLoggingMiddleware as RequestLoggingMiddleware;

#[serde_as]
#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Default, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case", default)]
#[non_exhaustive]
pub struct RequestResponseLoggingConfig {
    /// The maximum length to log. Payloads longer than this length will be truncated. To log the
    /// entire payload without any truncating, set to a negative number.
    pub max_len: i32,

    /// The content types to log. If not provided, all content types will be logged unless
    /// otherwise specified via `content_types_req` or `content_types_res`.
    ///
    /// Note: this currently only supports exact matches, so using `text/*` will not match
    /// `text/plain`, for example.
    #[serde_as(as = "Option<BTreeSet<DisplayFromStr>>")]
    pub content_types: Option<BTreeSet<Mime>>,

    /// The request payload content types to log. If not provided, will fall back to the
    /// values from `content_types`.
    ///
    /// Note: this currently only supports exact matches, so using `text/*` will not match
    /// `text/plain`, for example.
    #[serde_as(as = "Option<BTreeSet<DisplayFromStr>>")]
    pub content_types_req: Option<BTreeSet<Mime>>,

    /// The response payload content types to log. If not provided, will fall back to the
    /// values from `content_types`.
    ///
    /// Note: this currently only supports exact matches, so using `text/*` will not match
    /// `text/plain`, for example.
    #[serde_as(as = "Option<BTreeSet<DisplayFromStr>>")]
    pub content_types_res: Option<BTreeSet<Mime>>,
}

pub struct RequestResponseLoggingMiddleware;
impl<S> Middleware<S> for RequestResponseLoggingMiddleware
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    type Error = crate::error::Error;

    fn name(&self) -> String {
        "request-response-logging".to_string()
    }

    fn enabled(&self, state: &S) -> bool {
        AppContext::from_ref(state)
            .config()
            .service
            .http
            .custom
            .middleware
            .request_response_logging
            .common
            .enabled(state)
    }

    fn priority(&self, state: &S) -> i32 {
        AppContext::from_ref(state)
            .config()
            .service
            .http
            .custom
            .middleware
            .request_response_logging
            .common
            .priority
    }

    fn install(&self, state: &S, router: Router) -> Result<Router, Self::Error> {
        let max_len = AppContext::from_ref(state)
            .config()
            .service
            .http
            .custom
            .middleware
            .request_response_logging
            .custom
            .max_len;

        let router = router.layer(middleware::from_fn_with_state(
            state.clone(),
            move |State(state): State<S>, request, next| {
                log_req_res_bodies(state, request, next, max_len)
            },
        ));

        Ok(router)
    }
}

const TRUNCATED_STR: &str = "[truncated according to the `service.http.middleware.request-response-logging.max-len` config]";
const CONTENT_TYPE_OMITTED_STR: &str = "[omitted according to the `service.http.middleware.request-response-logging.content_types*` configs]";

// https://github.com/tokio-rs/axum/blob/main/examples/consume-body-in-extractor-or-middleware/src/main.rs
async fn log_req_res_bodies<S>(
    state: S,
    request: Request,
    next: Next,
    max_len: i32,
) -> Result<impl IntoResponse, Response>
where
    S: 'static + Send + Sync + Clone,
    AppContext: FromRef<S>,
{
    let context = AppContext::from_ref(&state);
    let config = &context
        .config()
        .service
        .http
        .custom
        .middleware
        .request_response_logging
        .custom;

    // Log the request body
    let is_req = true;
    let (parts, body) = request.into_parts();
    let content_type = get_content_type(parts.headers.get(CONTENT_TYPE))
        .ok()
        .flatten();
    let body = if should_log_content_type(config, content_type.as_ref(), is_req).unwrap_or_default()
    {
        let bytes = log_body(body, max_len, is_req).await?;
        Body::from(bytes)
    } else {
        let content_type = content_type
            .as_ref()
            .map(|content_type| content_type.as_ref())
            .unwrap_or_default();
        debug!(content_type, body = CONTENT_TYPE_OMITTED_STR, "request");
        body
    };
    let request = Request::from_parts(parts, body);

    // Handle the request
    let response = next.run(request).await;

    // Log the response body
    let is_req = false;
    let (parts, body) = response.into_parts();
    let content_type = get_content_type(parts.headers.get(CONTENT_TYPE))
        .ok()
        .flatten();
    let body = if should_log_content_type(config, content_type.as_ref(), is_req).unwrap_or_default()
    {
        let bytes = log_body(body, max_len, is_req).await?;
        Body::from(bytes)
    } else {
        let content_type = content_type
            .as_ref()
            .map(|content_type| content_type.as_ref())
            .unwrap_or_default();
        debug!(content_type, body = CONTENT_TYPE_OMITTED_STR, "response");
        body
    };
    let response = Response::from_parts(parts, body);

    // Return the response
    Ok(response)
}

fn get_content_type(content_type: Option<&HeaderValue>) -> RoadsterResult<Option<Mime>> {
    if let Some(content_type) = content_type {
        Ok(Some(Mime::from_str(content_type.to_str()?)?))
    } else {
        Ok(None)
    }
}

fn should_log_content_type(
    config: &RequestResponseLoggingConfig,
    content_type: Option<&Mime>,
    is_req: bool,
) -> RoadsterResult<bool> {
    let config = if is_req {
        (
            config.content_types.as_ref(),
            config.content_types_req.as_ref(),
        )
    } else {
        (
            config.content_types.as_ref(),
            config.content_types_res.as_ref(),
        )
    };
    match config {
        (Some(a), Some(b)) => {
            if let Some(content_type) = content_type {
                Ok(a.contains(content_type) || b.contains(content_type))
            } else {
                Ok(false)
            }
        }
        (None, Some(a)) => {
            if let Some(content_type) = content_type {
                Ok(a.contains(content_type))
            } else {
                Ok(false)
            }
        }
        (Some(a), None) => {
            if let Some(content_type) = content_type {
                Ok(a.contains(content_type))
            } else {
                Ok(false)
            }
        }
        (None, None) => Ok(true),
    }
}

async fn log_body(body: Body, max_len: i32, is_req: bool) -> Result<Bytes, Response> {
    // This only works if the body is not a long-running stream
    let bytes = body
        .collect()
        .await
        .map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()).into_response())?
        .to_bytes();

    let body = axum::Json::<HashMap<String, serde_json::Value>>::from_bytes(bytes.as_ref())
        .ok()
        .and_then(|axum::Json(value)| serde_json::to_string(&value).ok())
        .unwrap_or_else(|| format!("{bytes:?}"));

    let body = if max_len == 0 {
        TRUNCATED_STR
    } else if max_len < 0 || body.len() <= max_len as usize {
        &body
    } else {
        assert!(max_len > 0);
        body.get(0..(max_len as usize)).unwrap_or_default()
    };

    if is_req {
        debug!(body, "request");
    } else {
        debug!(body, "response");
    }

    Ok(bytes)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::AppConfig;
    use rstest::rstest;

    #[rstest]
    #[case(false, Some(true), true)]
    #[case(false, Some(false), false)]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn enabled(
        #[case] default_enable: bool,
        #[case] enable: Option<bool>,
        #[case] expected_enabled: bool,
    ) {
        // Arrange
        let mut config = AppConfig::test(None).unwrap();
        config.service.http.custom.middleware.default_enable = default_enable;
        config
            .service
            .http
            .custom
            .middleware
            .request_response_logging
            .common
            .enable = enable;

        let context = AppContext::test(Some(config), None, None).unwrap();

        let middleware = RequestResponseLoggingMiddleware;

        // Act/Assert
        assert_eq!(middleware.enabled(&context), expected_enabled);
    }

    #[rstest]
    #[case(None, 9900)]
    #[case(Some(1234), 1234)]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn priority(#[case] override_priority: Option<i32>, #[case] expected_priority: i32) {
        // Arrange
        let mut config = AppConfig::test(None).unwrap();
        if let Some(priority) = override_priority {
            config
                .service
                .http
                .custom
                .middleware
                .request_response_logging
                .common
                .priority = priority;
        }

        let context = AppContext::test(Some(config), None, None).unwrap();

        let middleware = RequestResponseLoggingMiddleware;

        // Act/Assert
        assert_eq!(middleware.priority(&context), expected_priority);
    }

    #[rstest]
    #[case(
        r#"
        max-len = -1
        "#,
        None,
        true,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types = []
        "#,
        None,
        true,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types = []
        "#,
        None,
        false,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types-req = []
        "#,
        None,
        true,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types-res = []
        "#,
        None,
        true,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-req = []
        "#,
        None,
        false,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-res = []
        "#,
        None,
        false,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types = ["text/plain"]
        "#,
        Some("text/plain"),
        true,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types = ["text/plain"]
        "#,
        Some("text/plain"),
        false,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-req = ["text/plain"]
        "#,
        Some("text/plain"),
        true,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-res = ["text/plain"]
        "#,
        Some("text/plain"),
        false,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types = ["application/json"]
        "#,
        Some("text/html"),
        true,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types = ["application/json"]
        "#,
        Some("text/html"),
        false,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types-req = ["application/json"]
        "#,
        Some("text/html"),
        true,
        false
    )]
    #[case(
        r#"
        max-len = -1
        content-types-req = ["application/json"]
        "#,
        Some("text/html"),
        false,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-res = ["application/json"]
        "#,
        Some("text/html"),
        true,
        true
    )]
    #[case(
        r#"
        max-len = -1
        content-types-res = ["application/json"]
        "#,
        Some("text/html"),
        false,
        false
    )]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn should_log_content_type(
        #[case] config: &str,
        #[case] content_type: Option<&str>,
        #[case] is_req: bool,
        #[case] expected: bool,
    ) {
        let config: RequestResponseLoggingConfig = toml::from_str(config).unwrap();
        let content_type = content_type.map(|s| Mime::from_str(s).unwrap());

        let should_log =
            super::should_log_content_type(&config, content_type.as_ref(), is_req).unwrap();

        assert_eq!(should_log, expected);
    }
}