dragonfly_client_rs/
app_config.rs1use std::sync::LazyLock;
2
3use figment::{
4 providers::{Env, Format, Serialized, Toml},
5 Figment,
6};
7use serde::{Deserialize, Serialize};
8
9const MEBIBYTE: u64 = 1024 * 1024;
10
11#[derive(Serialize, Deserialize)]
12pub struct AppConfig {
13 pub reuse_cache_database: bool,
14 pub reuse_cache_mode: crate::reuse_cache::CacheMode,
15 pub reuse_cache_entries: usize,
16 pub reuse_cache_bytes: usize,
17 pub base_url: String,
18 pub threads: usize,
19 pub load_duration: u64,
20 pub bulk_size: usize,
21 pub cf_access_client_id: String,
22 pub cf_access_client_secret: String,
23 pub max_archive_entries: usize,
24 pub max_distributions: usize,
25 pub max_download_size: u64,
26 pub max_expanded_size: u64,
27 pub max_scan_size: u64,
28}
29
30impl Default for AppConfig {
31 fn default() -> Self {
32 let available_parallelism = std::thread::available_parallelism().map_or(1, usize::from);
33
34 #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
35 AppConfig {
36 reuse_cache_database: false,
37 reuse_cache_mode: crate::reuse_cache::CacheMode::Off,
38 reuse_cache_entries: 4096,
39 reuse_cache_bytes: 32 * 1024 * 1024,
40 base_url: String::from("https://dragonfly.vipyrsec.com"),
41 cf_access_client_id: String::new(),
42 cf_access_client_secret: String::new(),
43 threads: available_parallelism,
44 bulk_size: 20,
45 load_duration: 60,
46 max_archive_entries: 4096,
47 max_distributions: 32,
48 max_download_size: 32 * MEBIBYTE,
49 max_expanded_size: 64 * MEBIBYTE,
50 max_scan_size: 16 * MEBIBYTE,
51 }
52 }
53}
54
55impl AppConfig {
56 #[allow(clippy::result_large_err)]
57 pub fn build() -> Result<AppConfig, figment::Error> {
58 Figment::from(Serialized::defaults(AppConfig::default()))
59 .merge(Toml::file("Config.toml"))
60 .merge(Toml::file("Config-dev.toml"))
61 .merge(Env::prefixed("DRAGONFLY_"))
62 .extract()
63 }
64}
65
66pub static APP_CONFIG: LazyLock<AppConfig> = LazyLock::new(|| AppConfig::build().unwrap());