roadster 0.8.1

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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
use crate::config::auth::Auth;
#[cfg(feature = "db-sql")]
use crate::config::database::Database;
#[cfg(feature = "email")]
use crate::config::email::Email;
use crate::config::environment::{ENVIRONMENT_ENV_VAR_NAME, Environment};
use crate::config::lifecycle::LifecycleHandler;
use crate::config::service::Service;
#[cfg(feature = "testing")]
use crate::config::testing::Testing;
use crate::config::tracing::Tracing;
use crate::error::RoadsterResult;
use crate::util::serde::default_true;
use ::tracing::warn;
use cfg_if::cfg_if;
use config::builder::DefaultState;
use config::{AsyncSource, Config, ConfigBuilder, FileFormat, Map, Source};
use convert_case::Casing;
use dotenvy::dotenv;
use health::check;
use health::check::HealthCheck;
use serde_derive::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;
use std::fs;
use std::path::{Path, PathBuf};
use validator::{Validate, ValidationErrors};

pub mod auth;
#[cfg(feature = "db-sql")]
pub mod database;
#[cfg(feature = "email")]
pub mod email;
pub mod environment;
pub mod health;
pub mod lifecycle;
pub mod service;
#[cfg(feature = "testing")]
pub mod testing;
pub mod tracing;

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct AppConfig {
    pub environment: Environment,
    #[validate(nested)]
    pub app: App,
    #[validate(nested)]
    pub lifecycle_handler: LifecycleHandler,
    #[validate(nested)]
    pub health_check: HealthCheck,
    #[validate(nested)]
    pub service: Service,
    #[validate(nested)]
    pub auth: Auth,
    #[validate(nested)]
    pub tracing: Tracing,
    #[cfg(feature = "db-sql")]
    #[validate(nested)]
    pub database: Database,
    #[cfg(feature = "email")]
    #[validate(nested)]
    pub email: Email,
    #[cfg(feature = "testing")]
    #[validate(nested)]
    pub testing: Testing,
    /// Allows providing custom config values. Any configs that aren't pre-defined above
    /// will be collected here.
    ///
    /// # Examples
    ///
    /// ```toml
    /// [foo]
    /// x = "y"
    /// ```
    ///
    /// This will be parsed as:
    /// ```raw
    /// AppConfig#custom: {
    ///     "foo": {
    ///         "x": "y",
    ///     }
    /// }
    /// ```
    #[serde(flatten, default)]
    #[validate(nested)]
    pub custom: CustomConfig,
}

#[serde_with::skip_serializing_none]
#[derive(
    Debug,
    Default,
    Clone,
    derive_more::Deref,
    derive_more::DerefMut,
    Validate,
    Serialize,
    Deserialize,
)]
pub struct CustomConfig {
    #[serde(flatten)]
    inner: BTreeMap<String, Value>,
}

impl From<CustomConfig> for BTreeMap<String, Value> {
    fn from(value: CustomConfig) -> Self {
        value.inner
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmptyConfig;
impl Validate for EmptyConfig {
    fn validate(&self) -> Result<(), ValidationErrors> {
        Ok(())
    }
}

pub const ENV_VAR_PREFIX: &str = "ROADSTER";
pub const ENV_VAR_SEPARATOR: &str = "__";

const DEFAULT_CONFIG_DIR: &str = "config/";

// Note that config files are loaded in the provided order, and files loaded later will override
// any duplicate values from files loaded earlier. So, basically, the last extension has a higher
// priority than the first extension.
cfg_if! {
    if #[cfg(feature = "config-yml")] {
        pub const FILE_EXTENSIONS: [&str; 3] = ["yml", "yaml", "toml"];
    } else {
        pub const FILE_EXTENSIONS: [&str; 1] = ["toml"];
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, bon::Builder)]
pub struct ConfigOverrideSource {
    #[builder(into)]
    pub name: String,
    #[builder(into)]
    pub value: config::Value,
}

impl Source for ConfigOverrideSource {
    fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
        Box::new(self.clone())
    }

    fn collect(&self) -> Result<Map<String, config::Value>, config::ConfigError> {
        Ok([(self.name.clone(), self.value.clone())]
            .into_iter()
            .collect())
    }
}

#[derive(bon::Builder)]
#[non_exhaustive]
pub struct AppConfigOptions {
    #[builder(field)]
    pub config_sources: Vec<Box<dyn Source + Send + Sync>>,
    #[builder(field)]
    pub async_config_sources: Vec<Box<dyn AsyncSource + Send>>,
    pub environment: Environment,
    #[builder(into)]
    pub config_dir: Option<PathBuf>,
}

impl<S: app_config_options_builder::State> AppConfigOptionsBuilder<S> {
    pub(crate) fn config_sources(
        mut self,
        config_sources: Vec<Box<dyn Source + Send + Sync>>,
    ) -> Self {
        self.config_sources = config_sources;
        self
    }

    pub fn add_source(mut self, source: impl Source + Send + Sync + 'static) -> Self {
        self.config_sources.push(Box::new(source));
        self
    }

    pub fn add_source_boxed(mut self, source: Box<dyn Source + Send + Sync>) -> Self {
        self.config_sources.push(source);
        self
    }

    #[allow(dead_code)]
    pub(crate) fn async_config_sources(
        mut self,
        async_config_sources: Vec<Box<dyn AsyncSource + Send>>,
    ) -> Self {
        self.async_config_sources = async_config_sources;
        self
    }

    pub fn add_async_source(mut self, source: impl AsyncSource + Send + 'static) -> Self {
        self.async_config_sources.push(Box::new(source));
        self
    }

    pub fn add_async_source_boxed(mut self, source: Box<dyn AsyncSource + Send>) -> Self {
        self.async_config_sources.push(source);
        self
    }
}

impl AppConfig {
    // This runs before tracing is initialized, so we need to use `println` in order to
    // log from this method.
    #[allow(clippy::disallowed_macros)]
    pub async fn new_with_options(options: AppConfigOptions) -> RoadsterResult<Self> {
        dotenv().ok();

        let environment_string = options.environment.clone().to_string();
        let environment_str = environment_string.as_str();

        let config_root_dir = options
            .config_dir
            .unwrap_or_else(|| PathBuf::from(DEFAULT_CONFIG_DIR))
            .canonicalize()?;

        println!("Loading configuration from directory {config_root_dir:?}");

        let config = Self::default_config(options.environment.clone())?;
        let config = config_env_file("default", &config_root_dir, config);
        let config = config_env_dir("default", &config_root_dir, config)?;
        let config = config_env_file(environment_str, &config_root_dir, config);
        let config = config_env_dir(environment_str, &config_root_dir, config)?;
        let config = config.add_source(
            config::Environment::default()
                .prefix(ENV_VAR_PREFIX)
                .convert_case(config::Case::Kebab)
                .separator(ENV_VAR_SEPARATOR),
        );

        // Convert builder state to `AsyncState`
        let config = config.add_async_source(BoxedAsyncSource(None));

        // Add all of the provided non-async sources
        let config = options
            .config_sources
            .into_iter()
            .fold(config, |config, source| {
                config.add_source(BoxedSource(source))
            });

        // Add all of the provided async sources
        let config = options
            .async_config_sources
            .into_iter()
            .fold(config, |config, source| {
                config.add_async_source(BoxedAsyncSource(Some(source)))
            });

        let config = config
            .set_override(
                ENVIRONMENT_ENV_VAR_NAME.to_case(convert_case::Case::Kebab),
                environment_str,
            )?
            .build()
            .await?;
        let config: AppConfig = config.try_deserialize()?;

        Ok(config)
    }

    #[cfg(test)]
    #[cfg_attr(coverage_nightly, coverage(off))]
    pub(crate) fn test(config_str: Option<&str>) -> RoadsterResult<Self> {
        let config = Self::default_config(Environment::Test)?
            .add_source(config::File::from_str(
                config_str.unwrap_or(
                    r#"
                    environment = "test"

                    [app]
                    name = "Test"

                    [tracing]
                    level = "debug"

                    [database]
                    uri = "postgres://example:example@invalid_host:5432/example_test"
                    auto-migrate = true
                    max-connections = 10

                    [auth.jwt]
                    secret = "secret-test"

                    [service.http]
                    scheme = "http"
                    host = "127.0.0.1"
                    port = 3000

                    [service.grpc]
                    scheme = "http"
                    host = "127.0.0.1"
                    port = 3001

                    [service.worker.sidekiq]
                    # This field normally is determined by the number of CPU cores if not provided.
                    # We provide it in the test config to avoid snapshot failures when running
                    # on varying hardware.
                    num-workers = 16

                    [service.worker.sidekiq.redis]
                    uri = "redis://invalid_host:1234"

                    [service.worker.pg]
                    # This field normally is determined by the number of CPU cores if not provided.
                    # We provide it in the test config to avoid snapshot failures when running
                    # on varying hardware.
                    num-workers = 16

                    [email.from]
                    email = "no-reply@example.com"

                    [email.smtp.connection]
                    uri = "smtps://username:password@smtp.example.com:425"

                    [email.sendgrid]
                    api-key = "api-key"
                    "#,
                ),
                FileFormat::Toml,
            ))
            .build()?;

        let config: AppConfig = config.try_deserialize()?;
        Ok(config)
    }

    #[allow(clippy::let_and_return)]
    fn default_config(
        #[allow(unused_variables)] environment: Environment,
    ) -> RoadsterResult<ConfigBuilder<DefaultState>> {
        let config = Config::builder()
            .set_default("environment", environment.clone().to_string())?
            .add_source(config::File::from_str(
                include_str!("default.toml"),
                FileFormat::Toml,
            ));

        let config = {
            let config = config.add_source(tracing::default_config());
            let config = tracing::default_config_per_env(environment.clone())
                .into_iter()
                .fold(config, |config, source| config.add_source(source));
            config
        };

        #[cfg(feature = "http")]
        let config = {
            let config = config.add_source(service::http::default_config());
            let config = service::http::default_config_per_env(environment.clone())
                .into_iter()
                .fold(config, |config, source| config.add_source(source));
            config
        };

        #[cfg(feature = "grpc")]
        let config = {
            let config = config.add_source(service::grpc::default_config());
            let config = service::grpc::default_config_per_env(environment.clone())
                .into_iter()
                .fold(config, |config, source| config.add_source(source));
            config
        };

        #[cfg(feature = "worker")]
        let config = config.add_source(service::worker::default_config());

        let config = config.add_source(lifecycle::default_config());

        let config = config.add_source(check::default_config());

        #[cfg(feature = "email-sendgrid")]
        let config = {
            let config = email::sendgrid::default_config_per_env(environment)
                .into_iter()
                .fold(config, |config, source| config.add_source(source));
            config
        };

        #[cfg(feature = "testing")]
        let config = config.add_source(testing::default_config());

        Ok(config)
    }

    pub(crate) fn validate(&self, exit_on_error: bool) -> RoadsterResult<()> {
        let result = Validate::validate(self);
        if exit_on_error {
            result?;
        } else if let Err(err) = result {
            warn!("An error occurred when validating the app config: {}", err);
        }
        Ok(())
    }
}

/// Adds the config file from the given config_dir for the given environment. The config file
/// can have any file extension specified in [`FILE_EXTENSIONS`].
fn config_env_file(
    environment: &str,
    config_dir: &Path,
    config: ConfigBuilder<DefaultState>,
) -> ConfigBuilder<DefaultState> {
    FILE_EXTENSIONS
        .map(|ext| config_dir.join(format!("{environment}.{ext}")))
        .into_iter()
        .filter(|path| path.is_file())
        .fold(config, |config, path| {
            config.add_source(config::File::from(path))
        })
}

/// Recursively adds all the config files in the given relative path `config/{environment}/` to the
/// [`ConfigBuilder`]. If no such directory exists, does nothing.
fn config_env_dir(
    environment: &str,
    config_dir: &Path,
    config: ConfigBuilder<DefaultState>,
) -> RoadsterResult<ConfigBuilder<DefaultState>> {
    let path = config_dir.join(environment);
    if !path.is_dir() {
        return Ok(config);
    }

    config_env_dir_recursive(&path, config)
}

/// Helper method for [`config_env_dir`] to recursively add config files in the given path
/// to the [`ConfigBuilder`].
fn config_env_dir_recursive(
    path: &Path,
    config: ConfigBuilder<DefaultState>,
) -> RoadsterResult<ConfigBuilder<DefaultState>> {
    fs::read_dir(path)?.try_fold(config, |config, dir_entry| {
        let path = dir_entry?.path();
        if path.is_dir() {
            config_env_dir_recursive(&path, config)
        } else if path.is_file()
            && FILE_EXTENSIONS
                .iter()
                .any(|ext| *ext == path.extension().unwrap_or_default())
        {
            Ok(config.add_source(config::File::from(path)))
        } else {
            Ok(config)
        }
    })
}

#[derive(Debug)]
struct BoxedSource(Box<dyn Source + Send + Sync>);

impl Source for BoxedSource {
    fn clone_into_box(&self) -> Box<dyn Source + Send + Sync> {
        self.0.clone_into_box()
    }

    fn collect(&self) -> Result<Map<String, config::Value>, config::ConfigError> {
        self.0.collect()
    }
}

#[derive(Debug)]
struct BoxedAsyncSource(Option<Box<dyn AsyncSource + Send + Sync>>);

#[async_trait::async_trait]
impl AsyncSource for BoxedAsyncSource {
    async fn collect(&self) -> Result<Map<String, config::Value>, config::ConfigError> {
        if let Some(source) = self.0.as_ref() {
            source.collect().await
        } else {
            Ok(Default::default())
        }
    }
}

#[serde_with::skip_serializing_none]
#[derive(Debug, Clone, Validate, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[non_exhaustive]
pub struct App {
    pub name: String,
    /// Shutdown the whole app if an error occurs in one of the app's top-level tasks (API, workers, etc).
    #[serde(default = "default_true")]
    pub shutdown_on_error: bool,
}

#[cfg(feature = "test-containers")]
#[serde_with::skip_serializing_none]
#[derive(Debug, Default, Validate, Clone, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
#[non_exhaustive]
pub struct TestContainer {
    pub enable: bool,
    pub tag: String,
}

#[cfg(all(
    test,
    feature = "default",
    feature = "default-diesel",
    feature = "open-api",
    feature = "worker-sidekiq",
    feature = "worker-pg",
    feature = "db-sea-orm",
    feature = "db-diesel-postgres-pool",
    feature = "db-diesel-mysql-pool",
    feature = "db-diesel-sqlite-pool",
    feature = "db-diesel-postgres-pool-async",
    feature = "db-diesel-mysql-pool-async",
    feature = "email-smtp",
    feature = "email-sendgrid",
    feature = "jwt-ietf",
    feature = "jwt-openid",
    feature = "cli",
    feature = "otel",
    feature = "grpc",
    feature = "test-containers",
    feature = "testing-mocks",
    feature = "config-yml",
))]
mod tests {
    use super::*;
    use insta::assert_toml_snapshot;

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn test() {
        let config = AppConfig::test(None).unwrap();

        assert_toml_snapshot!(config);
    }
}

#[cfg(test)]
mod custom_config_tests {
    use crate::config::CustomConfig;
    use serde_json::Value;
    use std::collections::BTreeMap;

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn to_map() {
        let config: CustomConfig = CustomConfig {
            inner: BTreeMap::new(),
        };
        let _map: BTreeMap<String, Value> = config.into();
    }

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn deref() {
        let mut inner = BTreeMap::new();
        inner.insert("foo".to_string(), "bar".into());
        let config: CustomConfig = CustomConfig { inner };
        assert_eq!(config.get("foo").unwrap(), "bar");
    }

    #[test]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn build() {
        let mut config = CustomConfig::default();
        config.insert("key".to_owned(), Value::String("value".to_owned()));
    }
}

#[cfg(test)]
mod file_extensions_tests {
    use insta::assert_debug_snapshot;

    #[test]
    #[cfg(not(feature = "config-yml"))]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn file_extensions_no_yml() {
        assert_debug_snapshot!(super::FILE_EXTENSIONS);
    }

    #[test]
    #[cfg(feature = "config-yml")]
    #[cfg_attr(coverage_nightly, coverage(off))]
    fn file_extensions_yml() {
        assert_debug_snapshot!(super::FILE_EXTENSIONS);
    }
}

#[cfg(test)]
mod app_config_options_tests {
    use crate::config::AppConfigOptions;
    use crate::config::environment::Environment;
    use config::{AsyncSource, Map, Value};

    #[derive(Debug)]
    struct TestAsyncSource;

    #[async_trait::async_trait]
    impl AsyncSource for TestAsyncSource {
        async fn collect(&self) -> Result<Map<String, Value>, config::ConfigError> {
            Ok(Default::default())
        }
    }

    #[test]
    fn app_config_options_builder() {
        let builder = AppConfigOptions::builder()
            .environment(Environment::Test)
            .config_dir("./")
            .async_config_sources(vec![Box::new(TestAsyncSource)])
            .add_async_source(TestAsyncSource)
            .add_async_source_boxed(Box::new(TestAsyncSource));

        let options = builder.build();

        assert_eq!(options.async_config_sources.len(), 3);
    }
}