2 // AGPL-3.0 License (see LICENSE)
5 use sailfish::Template;
6 use sailfish::TemplateSimple;
8 use std::sync::{Arc, Mutex};
10 use crate::site::blog_post::BlogPost;
11 use crate::site::helper::Helper;
12 use crate::site::screenshot::Screenshot;
14 pub struct CoreShared {
15 state: Mutex<CoreState>,
19 pub fn new() -> Self {
21 state: Mutex::new(CoreState::new()),
25 pub fn shared(self) -> Arc<Self> {
29 pub fn set_core_index_data(
31 blog_base_dir: String,
32 blog_posts: Vec<BlogPost>,
33 screenshots: Vec<Screenshot>,
35 let mut lock = self.state.lock().unwrap();
36 lock.blog_base_dir = blog_base_dir;
37 lock.blog_posts = blog_posts;
38 lock.screenshots = screenshots;
43 #[template(path = "index.stpl")]
45 pub blog_posts: Vec<BlogPost>,
46 pub screenshots: Vec<Screenshot>,
48 pub blog_base_dir: String,
52 pub fn new() -> Self {
54 blog_posts: Vec::new(),
55 screenshots: Vec::new(),
56 hfge_url: String::from("projects/hfge"),
57 blog_base_dir: String::new(),
62 pub async fn generate_core() {
63 // create output dirs needed:
66 let mut tasks = Vec::with_capacity(4);
68 // copy all static related files:
69 tasks.push(tokio::spawn(copy_static_dirs()));
71 // generate all core pages (core index will be done as the very
72 // last thing as screenshot and blog generation must be done first):
73 tasks.push(tokio::spawn(generate_error_pages()));
74 tasks.push(tokio::spawn(generate_project_pages()));
75 tasks.push(tokio::spawn(generate_contact_page()));
77 // wait until all taks are done:
83 fn create_output_dirs() {
84 Helper::create_dir_all(&Helper::get_output_dir().join("contact"));
85 Helper::create_dir_all(&Helper::get_output_dir().join("projects/hfge"));
88 async fn copy_static_dirs() {
89 // copy static and static_root to output folder:
90 Helper::copy_dir_all(Path::new("static"), Helper::get_output_dir().join("static"))
94 "Copied dir 'static' recursively to '{}'",
95 Helper::get_output_dir().join("static").display()
98 Helper::copy_dir_all(Path::new("static_root"), Helper::get_output_dir())
102 "Copied dir 'static_root' recursively to '{}'",
103 Helper::get_output_dir().display()
107 async fn generate_error_pages() {
109 #[derive(TemplateSimple)]
110 #[template(path = "404.stpl")]
111 struct Err404Template {}
113 let ctx = Err404Template {};
115 &Helper::get_output_dir().join("404.html"),
116 &ctx.render_once().unwrap().as_bytes(),
122 #[derive(TemplateSimple)]
123 #[template(path = "500.stpl")]
124 struct Err500Template {}
126 let ctx = Err500Template {};
128 &Helper::get_output_dir().join("500.html"),
129 &ctx.render_once().unwrap().as_bytes(),
135 async fn generate_project_pages() {
137 #[derive(TemplateSimple)]
138 #[template(path = "hfge.stpl")]
139 struct HFGETemplate {}
141 let ctx = HFGETemplate {};
143 &Helper::get_output_dir().join("projects/hfge/index.html"),
144 &ctx.render_once().unwrap().as_bytes(),
150 async fn generate_contact_page() {
152 #[derive(TemplateSimple)]
153 #[template(path = "contact.stpl")]
154 struct ContactTemplate {}
156 let ctx = ContactTemplate {};
158 &Helper::get_output_dir().join("contact/index.html"),
159 &ctx.render_once().unwrap().as_bytes(),
165 pub async fn generate_root_index(shared: Arc<CoreShared>) {
166 let lock = shared.state.lock().unwrap();
168 // write page to disk:
169 Helper::write_file_sync(
170 &Helper::get_output_dir().join("index.html"),
171 &lock.render().unwrap().as_bytes(),