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
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use cache_buster::Files;
use lazy_static::lazy_static;
use log::{debug, info};
use sailfish::TemplateOnce;
use tokio::fs;
use tokio::io::{Error, ErrorKind};

#[derive(Clone, TemplateOnce)]
#[template(path = "auth/login/index.html")]
struct IndexPage {
    name: String,
    title: String,
}

impl Default for IndexPage {
    fn default() -> Self {
        IndexPage {
            name: "mCaptcha".into(),
            title: "Login".into(),
        }
    }
}

impl IndexPage {
    pub async fn run(&self) -> Result<(), Error> {
        let file = root_path("index.html");

        info!("rendering {}", &file);
        let index = self.clone().render_once().unwrap();

        fs::write(&file, index).await?;
        info!("wrote {}", &file);
        Ok(())
    }
}

const BASE_DIR: &str = "./prod";

lazy_static! {
    pub static ref FILES: Files = Files::load();
}

#[tokio::main]
async fn main() {
    pretty_env_logger::init();
    match fs::create_dir(BASE_DIR).await {
        Err(e) => {
            if e.kind() == ErrorKind::AlreadyExists {
                info!("cleaning up old assetes");
                fs::remove_dir_all(BASE_DIR).await.unwrap();
                debug!("creating target location");
                fs::create_dir(BASE_DIR).await.unwrap();
            }
        }
        _ => (),
    };

    IndexPage::default().run().await.unwrap();
    signup::IndexPage::default().run().await.unwrap();
    panel::IndexPage::default().run().await.unwrap();
}

fn root_path(rel: &str) -> String {
    format!("{}/{}", BASE_DIR, rel)
}

fn rel_path(dir: &str, file: &str) -> String {
    format!("{}/{}", dir, file)
}

mod signup {
    use super::*;

    #[derive(TemplateOnce, Clone)]
    #[template(path = "auth/register/index.html")]
    pub struct IndexPage {
        pub name: String,
        pub title: String,
    }

    impl Default for IndexPage {
        fn default() -> Self {
            IndexPage {
                name: "mCaptcha".into(),
                title: "Join".into(),
            }
        }
    }

    impl IndexPage {
        pub async fn run(&self) -> Result<(), Error> {
            let dir = root_path("register");
            let file = rel_path(&dir, "index.html");

            print!("");
            info!("rendering {}", &file);
            let index = self.clone().render_once().unwrap();

            fs::create_dir(&dir).await?;
            info!("creating dir {}", &dir);

            fs::write(&file, index).await?;
            info!("wrote {}", &file);
            Ok(())
        }
    }
}

pub type Literal = &'static str;

pub mod panel {
    use super::*;
    use section::*;

    #[derive(TemplateOnce, Clone)]
    #[template(path = "panel/index.html")]
    pub struct IndexPage {
        pub name: String,
        pub title: String,
        pub active: &'static SubPanel,
    }

    const TITLE: &str = "Dashboard";

    impl Default for IndexPage {
        fn default() -> Self {
            IndexPage {
                name: "mCaptcha".into(),
                title: "Home".into(),
                active: &COMMENTS,
            }
        }
    }

    impl IndexPage {
        pub async fn run(&self) -> Result<(), Error> {
            let dir = root_path("panel");
            let file = rel_path(&dir, "index.html");

            info!("rendering {}", &file);
            let index = self.clone().render_once().unwrap();

            fs::create_dir(&dir).await?;
            info!("creating dir {}", &dir);

            fs::write(&file, index).await?;
            info!("wrote {}", &file);
            Ok(())
        }
    }

    pub mod section {
        use super::*;

        pub struct Section<const N: usize> {
            pub name: Literal,
            pub elements: [&'static SubPanel; N],
        }

        pub struct SubPanel {
            pub name: Literal,
            pub icon: Literal,
        }

        macro_rules! sub_panel {
            ($var:ident, $name:expr, $icon:expr) => {
                pub static $var: SubPanel = SubPanel {
                    name: $name,
                    icon: $icon,
                };
            };
        }

        sub_panel!(COMMENTS, "Comments", "comments");
        sub_panel!(USERS, "Users", "users");
        sub_panel!(PAGES, "Pages", "pages");

        pub static ADMIN_SECTION: Section<3> = Section {
            elements: [&COMMENTS, &USERS, &PAGES],
            name: "Admin",
        };

        pub static SETTINGS_SECTION: Section<3> = Section {
            elements: [&COMMENTS, &USERS, &PAGES],
            name: "Settings",
        };
    }
}