Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
Commit 0b53d2f6 authored by Pierre Jarriges's avatar Pierre Jarriges
Browse files

load website from existing

parent ce0f270e
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
......@@ -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>,
......
......@@ -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(())
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment