aboutsummaryrefslogtreecommitdiff
path: root/base/src/lib.rs
blob: 436aa9a48b4b10fcb80a60c372558fb9375ad234 (plain)
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
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2025 metamuffin <metamuffin.org>
*/
pub mod assetfed;
pub mod cache;
pub mod database;
pub mod federation;
pub mod locale;
pub mod permission;

pub use jellycommon as common;

use jellycommon::config::{GlobalConfig, SecretsConfig};
use std::sync::{
    atomic::{AtomicBool, Ordering},
    LazyLock,
};

pub static CONF: LazyLock<GlobalConfig> = LazyLock::new(load_config);
pub static SECRETS: LazyLock<SecretsConfig> = LazyLock::new(load_secrets);
pub static USE_TEST: AtomicBool = AtomicBool::new(false);

pub fn use_test_config() {
    USE_TEST.store(true, Ordering::Relaxed)
}

pub fn load_config() -> GlobalConfig {
    if USE_TEST.load(Ordering::Relaxed) {
        return GlobalConfig::default();
    }
    serde_yaml::from_reader(
        std::fs::File::open(std::env::var("JELLYTHING_CONFIG").unwrap_or_else(|_| {
            if std::env::args()
                .next()
                .unwrap_or_default()
                .ends_with("jellything")
            {
                std::env::args().nth(1).expect(
                    "First argument or JELLYTHING_CONFIG must specify the configuration to use.",
                )
            } else {
                panic!("JELLYTHING_CONFIG variable is required.")
            }
        }))
        .expect("config cannot be read"),
    )
    .expect("config invalid")
}
fn load_secrets() -> SecretsConfig {
    if USE_TEST.load(Ordering::Relaxed) {
        return SecretsConfig::default();
    }
    serde_yaml::from_reader(std::fs::File::open(&CONF.secrets_path).expect("secrets file missing"))
        .expect("secrets config invalid")
}