Skip to main content

auric_runtime/
app.rs

1use crate::routing::Router;
2use handlebars::Handlebars;
3use std::collections::HashMap;
4use std::sync::Arc;
5use web_sys::window;
6
7#[derive(Default)]
8pub struct App {
9    handlebars: Handlebars<'static>,
10    router: Arc<Router>,
11}
12
13impl App {
14    pub fn new() -> Self {
15        Self::default()
16    }
17
18    pub fn handlebars(&mut self) -> &mut Handlebars<'static> {
19        &mut self.handlebars
20    }
21
22    pub fn router(&self) -> Arc<Router> {
23        self.router.clone()
24    }
25
26    pub fn start(&self) -> anyhow::Result<()> {
27        console_error_panic_hook::set_once();
28
29        // Initialize router
30        self.router.init().expect("Could not initialize router");
31
32        // Generate html for application template
33        let data: HashMap<String, String> = HashMap::new();
34        let html = self
35            .handlebars
36            .render("application", &data)
37            .expect("Could not render application.hbs");
38
39        // Add html as a main element within the body
40        let document = window()
41            .and_then(|win| win.document())
42            .expect("Could not access document");
43        let body = document.body().expect("Could not access document body");
44        let main = document
45            .create_element("main")
46            .expect("Could not create a main element within the body");
47        main.set_inner_html(&html);
48        body.append_child(main.as_ref())
49            .expect("Could not append main element to body");
50
51        Ok(())
52    }
53}