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
use crate::templates::helpers::register_helpers;
use handlebars::Handlebars;
use std::sync::Arc;

lazy_static! {
    /// Lazy Static to store app data at runtime.
    static ref APP_DATA: Arc<AppData> = {
        Arc::new(AppData::new())
    };
}

/// Struct to store shared app data and objects.
#[derive(Clone)]
pub struct AppData {
    /// The handlebars template registry.
    template_registry: Arc<Handlebars<'static>>,
}

impl AppData {
    /// Create new App Data object using the global static config.
    fn new() -> Self {
        // Register handlebars templates
        let mut template_registry = Handlebars::new();
        template_registry
            .register_templates_directory(".hbs", "templates")
            .map_err(|e| {
                error!("Failed to properly register handlebars templates: {}", e);
                e
            })
            .unwrap();

        // We do not use handlebars strict mode anymore since it increasingly breaks templates
        // without warning.
        // template_registry.set_strict_mode(true);

        // Register the helpers defined in the helpers module.
        register_helpers(&mut template_registry);
        info!("Handlebars templates registered.");

        Self {
            template_registry: Arc::new(template_registry),
        }
    }

    /// Get an [`Arc`] reference to the global, lazily generated app-data.
    pub fn global() -> Arc<AppData> {
        APP_DATA.clone()
    }

    /// Get an [`Arc`] reference to the template registry.
    pub fn get_handlebars_registry(&self) -> Arc<Handlebars<'static>> {
        self.template_registry.clone()
    }
}