myc_core/
settings.rs

1use crate::{
2    domain::dtos::route::Route,
3    use_cases::gateway::routes::load_config_from_yaml,
4};
5
6use futures::lock::Mutex;
7use lazy_static::lazy_static;
8use std::env::{self, var_os};
9use tera::Tera;
10
11// ? ---------------------------------------------------------------------------
12// ? Configure default system constants
13// ? ---------------------------------------------------------------------------
14
15/// Default TOTP domain
16///
17/// This is the default domain used to generate the TOTP token.
18///
19pub const DEFAULT_TOTP_DOMAIN: &str = "Mycelium";
20
21// ? ---------------------------------------------------------------------------
22// ? Configure routes and profile
23//
24// Here routes and profile services are loaded.
25// ? ---------------------------------------------------------------------------
26
27lazy_static! {
28    pub static ref ROUTES: Mutex<Vec<Route>> = Mutex::new(vec![]);
29}
30
31pub async fn init_in_memory_routes(routes_file: Option<String>) {
32    let source_file_path = match routes_file {
33        None => {
34            match var_os("SOURCE_FILE_PATH") {
35                Some(path) => Some(path.into_string().unwrap()),
36                None => {
37                    panic!("Required environment variable SOURCE_FILE_PATH not set.")
38                }
39            }
40        }
41        Some(path) => Some(path),
42    };
43
44    let db = match load_config_from_yaml(match source_file_path.to_owned() {
45        None => panic!(
46            "Source path not already loaded. Please run the init method before 
47                load database."
48        ),
49        Some(path) => path,
50    })
51    .await
52    {
53        Err(err) => {
54            panic!("Unexpected error on load in memory database: {err}")
55        }
56        Ok(res) => res,
57    };
58
59    ROUTES.lock().await.extend(db);
60}
61
62// ? ---------------------------------------------------------------------------
63// ? Templates
64// ? ---------------------------------------------------------------------------
65
66lazy_static! {
67    pub static ref TEMPLATES: Tera = {
68        let template_dir = env::var("TEMPLATES_DIR")
69            .unwrap_or_else(|_| "templates".to_string());
70
71        tracing::info!("Loading templates from: {}", template_dir);
72
73        let mut _tera = match Tera::new(&format!("{}/{}", template_dir, "**/*"))
74        {
75            Ok(res) => res,
76            Err(err) => panic!("Error on load tera templates: {}", err),
77        };
78
79        _tera.autoescape_on(vec![".jinja", ".subject"]);
80        _tera
81    };
82}