aboutsummaryrefslogtreecommitdiff
path: root/base/src/lib.rs
blob: 48d3b373b43b0cd28bc0217270b065e30a204abd (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
/*
    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) 2023 metamuffin <metamuffin.org>
*/
#![feature(lazy_cell)]
pub mod cache;
pub mod database;
pub mod federation;
pub mod permission;
pub mod temp;

use jellycommon::{
    config::{GlobalConfig, SecretsConfig},
    AssetLocation,
};
use std::{fs::File, path::PathBuf, sync::LazyLock};

pub static CONF: LazyLock<GlobalConfig> = LazyLock::new(|| {
    serde_yaml::from_reader(
        File::open(std::env::var("JELLYTHING_CONFIG").unwrap_or_else(|_| {
            if std::env::args().nth(0) == Some("jellything".to_string()) {
                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")
});
pub static SECRETS: LazyLock<SecretsConfig> = LazyLock::new(|| {
    serde_yaml::from_reader(File::open(&CONF.secrets_path).expect("secrets file missing"))
        .expect("secrets config invalid")
});

pub trait AssetLocationExt {
    fn path(&self) -> PathBuf;
}
impl AssetLocationExt for AssetLocation {
    fn path(&self) -> PathBuf {
        match self {
            AssetLocation::Assets(p) => CONF.asset_path.join(p),
            AssetLocation::Cache(p) => CONF.cache_path.join(p),
            AssetLocation::Library(p) => CONF.library_path.join(p),
            AssetLocation::Temp(p) => CONF.temp_path.join(p),
            AssetLocation::Media(p) => CONF.media_path.join(p),
        }
    }
}