diff --git a/src/main.rs b/src/main.rs index ba4dd2d465a30b28d7956cf1cd434efd60b4e09a..1edfb00d8a584c51d2450f5e67730c1c30ad609a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,10 +26,14 @@ async fn main() -> std::io::Result<()> { ) .init(); - let website = WebSiteBuilder::load(&app_state.config) - .with_static_files_manager(StaticFilesManager::new(&app_state).unwrap().build()) - .build() - .unwrap(); + let website = { + let mut wb = WebSiteBuilder::load(&app_state.config); + let static_manager = StaticFilesManager::new(&app_state).unwrap().build(); + wb.save(&app_state.config).unwrap(); + wb.with_static_files_manager(static_manager) + .build() + .unwrap() + }; let host = app_state.config.host.clone(); let port = app_state.config.port; @@ -37,9 +41,6 @@ async fn main() -> std::io::Result<()> { let srv_conf = tls_config(&app_state.config); let dir = website.static_files_manager.dir.clone(); - // let mut_app_state = RwLock::new(app_state); - // let app_state = web::Data::new(mut_app_state); - // let mut_website = web::Data::new(RwLock::new(website)); HttpServer::new(move || { App::new() diff --git a/src/static_files/static_files.rs b/src/static_files/static_files.rs index 5fd66438c589ac64c27980e7de9e3c9dbe8bc4da..00e42d5439b454a0f1ce29a1fb24cee0c93dd80e 100644 --- a/src/static_files/static_files.rs +++ b/src/static_files/static_files.rs @@ -3,7 +3,7 @@ use crate::website::Page; use std::io::prelude::*; use std::path::{Path, PathBuf}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)] pub struct StaticFilesManager { pub dir: PathBuf, pub index: Vec<String>, diff --git a/src/website/website.rs b/src/website/website.rs index 8e2d20158743c49810d9bd15877d663e9a39bade..970451d6e653009483f59470690c7a4d6b83ef32 100644 --- a/src/website/website.rs +++ b/src/website/website.rs @@ -2,6 +2,7 @@ use super::page::{Page, PageTemplate}; use crate::app::AppConfig; use crate::static_files::StaticFilesManager; use serde::{Deserialize, Serialize}; +use std::io::prelude::*; use std::path::PathBuf; #[derive(Debug, Serialize, Deserialize, Clone)] @@ -45,16 +46,52 @@ impl WebSiteBuilder { .with_static_files_manager(StaticFilesManager::testing_new(test_dir).unwrap()) } - pub fn load(config: &AppConfig) -> WebSiteBuilder { + fn blank_website_template() -> PathBuf { + std::env::current_dir() + .unwrap() + .join("templates") + .join("new_website.json") + } + + pub fn load(config: &AppConfig) -> Self { let file_path = match &config.load { - None => std::env::current_dir() - .unwrap() - .join("templates") - .join("new_website.json"), Some(pth) => pth.clone(), + None => { + if let Some(loaded) = Self::try_load_from_existing_file(config) { + return loaded; + } + Self::blank_website_template() + } }; - WebSiteBuilder::from_json(&std::fs::read_to_string(file_path).unwrap()) + Self::from_json(&std::fs::read_to_string(file_path).unwrap()) + } + + fn try_load_from_existing_file(config: &AppConfig) -> Option<Self> { + let website_file_path = config.storage_dir.join("website.json"); + + if website_file_path.exists() { + match &std::fs::read_to_string(&website_file_path) { + Ok(json) => return Some(Self::from_json(&json)), + Err(_) => return None, + } + } + + None + } + + pub fn save(&self, config: &AppConfig) -> std::io::Result<()> { + let save_json = serde_json::to_string(self).unwrap(); + let json_path = config.storage_dir.join("website.json"); + let mut f = std::fs::OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(&json_path)?; + f.write_all(save_json.as_bytes())?; + f.flush()?; + + Ok(()) } }