a3s-boot 0.1.1

Adapter-first modular Rust web framework for A3S inspired by Nest.js
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
use crate::{
    BootRequest, BootResponse, BoxFuture, ExecutionContext, Interceptor, Middleware,
    MiddlewareOutcome, Module, ProviderDefinition, ProviderToken, Result,
};
use serde::Serialize;
use serde_json::Value;
use std::collections::BTreeMap;
use std::fmt;
use std::sync::{Arc, RwLock};

/// Log severity understood by Boot's provider-backed logger.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum LogLevel {
    Trace,
    Debug,
    Info,
    Warn,
    Error,
}

impl LogLevel {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Trace => "trace",
            Self::Debug => "debug",
            Self::Info => "info",
            Self::Warn => "warn",
            Self::Error => "error",
        }
    }
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Structured fields attached to a log record.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct LogFields {
    fields: BTreeMap<String, Value>,
}

impl LogFields {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_value(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
        self.fields.insert(key.into(), value.into());
        self
    }

    pub fn with<T>(mut self, key: impl Into<String>, value: T) -> Result<Self>
    where
        T: Serialize,
    {
        self.fields.insert(
            key.into(),
            serde_json::to_value(value)
                .map_err(|error| crate::BootError::Internal(error.to_string()))?,
        );
        Ok(self)
    }

    pub fn insert_value(&mut self, key: impl Into<String>, value: impl Into<Value>) {
        self.fields.insert(key.into(), value.into());
    }

    pub fn get(&self, key: &str) -> Option<&Value> {
        self.fields.get(key)
    }

    pub fn is_empty(&self) -> bool {
        self.fields.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
        self.fields.iter().map(|(key, value)| (key.as_str(), value))
    }

    pub fn into_inner(self) -> BTreeMap<String, Value> {
        self.fields
    }
}

impl From<BTreeMap<String, Value>> for LogFields {
    fn from(fields: BTreeMap<String, Value>) -> Self {
        Self { fields }
    }
}

/// Immutable structured log record.
#[derive(Debug, Clone, PartialEq)]
pub struct LogRecord {
    pub level: LogLevel,
    pub target: String,
    pub message: String,
    pub fields: LogFields,
}

impl LogRecord {
    pub fn field(&self, key: &str) -> Option<&Value> {
        self.fields.get(key)
    }
}

/// Backend abstraction used by [`Logger`].
pub trait LogSink: Send + Sync + 'static {
    fn log(&self, record: LogRecord) -> Result<()>;
}

/// Logger provider that writes structured records to a pluggable sink.
#[derive(Clone)]
pub struct Logger {
    sink: Arc<dyn LogSink>,
    target: String,
    fields: LogFields,
}

impl fmt::Debug for Logger {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Logger")
            .field("target", &self.target)
            .field("fields", &self.fields)
            .finish_non_exhaustive()
    }
}

impl Logger {
    pub fn new<S>(sink: S) -> Self
    where
        S: LogSink,
    {
        Self::from_sink_arc(Arc::new(sink))
    }

    pub fn from_sink_arc(sink: Arc<dyn LogSink>) -> Self {
        Self {
            sink,
            target: "a3s.boot".to_string(),
            fields: LogFields::new(),
        }
    }

    pub fn noop() -> Self {
        Self::new(NoopLogSink)
    }

    pub fn in_memory() -> Self {
        Self::new(InMemoryLogSink::new())
    }

    pub fn target(&self) -> &str {
        &self.target
    }

    pub fn with_target(mut self, target: impl Into<String>) -> Self {
        self.target = target.into();
        self
    }

    pub fn child(&self, target: impl Into<String>) -> Self {
        let mut logger = self.clone();
        logger.target = target.into();
        logger
    }

    pub fn with_default_field<T>(mut self, key: impl Into<String>, value: T) -> Result<Self>
    where
        T: Serialize,
    {
        self.fields = self.fields.with(key, value)?;
        Ok(self)
    }

    pub fn log(&self, level: LogLevel, message: impl Into<String>) -> Result<()> {
        self.log_with_fields(level, message, LogFields::new())
    }

    pub fn log_with_fields(
        &self,
        level: LogLevel,
        message: impl Into<String>,
        fields: LogFields,
    ) -> Result<()> {
        self.sink.log(LogRecord {
            level,
            target: self.target.clone(),
            message: message.into(),
            fields: merge_fields(&self.fields, fields),
        })
    }

    pub fn trace(&self, message: impl Into<String>) -> Result<()> {
        self.log(LogLevel::Trace, message)
    }

    pub fn debug(&self, message: impl Into<String>) -> Result<()> {
        self.log(LogLevel::Debug, message)
    }

    pub fn info(&self, message: impl Into<String>) -> Result<()> {
        self.log(LogLevel::Info, message)
    }

    pub fn warn(&self, message: impl Into<String>) -> Result<()> {
        self.log(LogLevel::Warn, message)
    }

    pub fn error(&self, message: impl Into<String>) -> Result<()> {
        self.log(LogLevel::Error, message)
    }
}

fn merge_fields(defaults: &LogFields, fields: LogFields) -> LogFields {
    let mut merged = defaults.clone().into_inner();
    for (key, value) in fields.into_inner() {
        merged.insert(key, value);
    }
    merged.into()
}

/// Sink that discards records. Useful as a default or for tests that only need
/// a logger provider to exist.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoopLogSink;

impl LogSink for NoopLogSink {
    fn log(&self, _record: LogRecord) -> Result<()> {
        Ok(())
    }
}

/// In-memory log sink intended for tests and local diagnostics.
#[derive(Debug, Clone, Default)]
pub struct InMemoryLogSink {
    records: Arc<RwLock<Vec<LogRecord>>>,
}

impl InMemoryLogSink {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn records(&self) -> Result<Vec<LogRecord>> {
        Ok(self.read_records()?.clone())
    }

    pub fn clear(&self) -> Result<()> {
        self.write_records()?.clear();
        Ok(())
    }

    fn read_records(&self) -> Result<std::sync::RwLockReadGuard<'_, Vec<LogRecord>>> {
        self.records
            .read()
            .map_err(|_| crate::BootError::Internal("log sink lock is poisoned".to_string()))
    }

    fn write_records(&self) -> Result<std::sync::RwLockWriteGuard<'_, Vec<LogRecord>>> {
        self.records
            .write()
            .map_err(|_| crate::BootError::Internal("log sink lock is poisoned".to_string()))
    }
}

impl LogSink for InMemoryLogSink {
    fn log(&self, record: LogRecord) -> Result<()> {
        self.write_records()?.push(record);
        Ok(())
    }
}

/// Middleware that logs an incoming request before it reaches guards,
/// interceptors, pipes, and handlers.
#[derive(Debug, Clone)]
pub struct RequestLoggingMiddleware {
    logger: Arc<Logger>,
}

impl RequestLoggingMiddleware {
    pub fn new(logger: Arc<Logger>) -> Self {
        Self { logger }
    }
}

impl Middleware for RequestLoggingMiddleware {
    fn handle(&self, request: BootRequest) -> BoxFuture<'static, Result<MiddlewareOutcome>> {
        let logger = Arc::clone(&self.logger);
        Box::pin(async move {
            logger.log_with_fields(
                LogLevel::Info,
                "request received",
                request_fields(&request)
                    .with_value("body_bytes", Value::from(request.body().len() as u64)),
            )?;
            Ok(MiddlewareOutcome::next(request))
        })
    }
}

/// Interceptor that logs route execution before and after the handler.
#[derive(Debug, Clone)]
pub struct RequestLoggingInterceptor {
    logger: Arc<Logger>,
}

impl RequestLoggingInterceptor {
    pub fn new(logger: Arc<Logger>) -> Self {
        Self { logger }
    }
}

impl Interceptor for RequestLoggingInterceptor {
    fn before(&self, context: ExecutionContext) -> BoxFuture<'static, Result<()>> {
        let logger = Arc::clone(&self.logger);
        Box::pin(async move {
            logger.log_with_fields(
                LogLevel::Info,
                "request started",
                execution_fields(&context),
            )
        })
    }

    fn after(
        &self,
        context: ExecutionContext,
        response: BootResponse,
    ) -> BoxFuture<'static, Result<BootResponse>> {
        let logger = Arc::clone(&self.logger);
        Box::pin(async move {
            logger.log_with_fields(
                LogLevel::Info,
                "request completed",
                execution_fields(&context).with_value("status", Value::from(response.status())),
            )?;
            Ok(response)
        })
    }
}

fn request_fields(request: &BootRequest) -> LogFields {
    let mut fields = LogFields::new()
        .with_value("method", Value::from(request.method().as_str()))
        .with_value("path", Value::from(request.path()));
    if let Some(query) = request.query_string() {
        fields.insert_value("query", Value::from(query));
    }
    fields
}

fn execution_fields(context: &ExecutionContext) -> LogFields {
    let mut fields = LogFields::new()
        .with_value("method", Value::from(context.method.as_str()))
        .with_value("request_path", Value::from(context.request_path.clone()))
        .with_value("route_path", Value::from(context.route_path.clone()));

    if let Some(module_name) = &context.module_name {
        fields.insert_value("module", Value::from(module_name.clone()));
    }
    if let Some(controller_prefix) = &context.controller_prefix {
        fields.insert_value("controller", Value::from(controller_prefix.clone()));
    }

    fields
}

/// Module that registers and exports a [`Logger`] provider.
#[derive(Clone)]
pub struct LoggingModule {
    name: &'static str,
    token: ProviderToken,
    logger: Arc<Logger>,
    global: bool,
}

impl fmt::Debug for LoggingModule {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LoggingModule")
            .field("name", &self.name)
            .field("token", &self.token)
            .field("logger", &self.logger)
            .field("global", &self.global)
            .finish_non_exhaustive()
    }
}

impl LoggingModule {
    pub fn noop(name: &'static str) -> Self {
        Self::from_logger(name, Logger::noop())
    }

    pub fn in_memory(name: &'static str) -> Self {
        Self::from_logger(name, Logger::in_memory())
    }

    pub fn from_logger(name: &'static str, logger: Logger) -> Self {
        Self {
            name,
            token: ProviderToken::of::<Logger>(),
            logger: Arc::new(logger),
            global: false,
        }
    }

    pub fn named(mut self, token: impl Into<String>) -> Self {
        self.token = ProviderToken::named(token);
        self
    }

    pub fn global(mut self) -> Self {
        self.global = true;
        self
    }
}

impl Module for LoggingModule {
    fn name(&self) -> &'static str {
        self.name
    }

    fn providers(&self) -> Result<Vec<ProviderDefinition>> {
        Ok(vec![ProviderDefinition::named_from_arc(
            self.token.as_str(),
            Arc::clone(&self.logger),
        )])
    }

    fn exports(&self) -> Result<Vec<ProviderToken>> {
        Ok(vec![self.token.clone()])
    }

    fn is_global(&self) -> bool {
        self.global
    }
}