diff --git a/docker-compose.yml b/docker-compose.yml index 1c041c7..53da942 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -11,6 +11,10 @@ services: - "DATABASE_URL=postgres://user:pass@postgres/webarc" - "RUST_LOG=info" - "ROCKET_ADDRESS=0.0.0.0" + # Rewrite links to point back to the archive itself + - "ROUTE_INTERNAL=true" + # Download missing routes on demand + - "DOWNLOAD_ON_DEMAND=true" postgres: image: timescale/timescaledb:latest-pg16 diff --git a/src/archive.rs b/src/archive.rs index 6d0b3af..88327ac 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -1,18 +1,25 @@ -use std::{fmt::format, fs::read_to_string, path::{Path, PathBuf}}; +use std::{ + fmt::format, + fs::read_to_string, + path::{Path, PathBuf}, +}; pub struct WebsiteArchive { - pub dir: PathBuf + pub dir: PathBuf, } pub struct Domain { pub name: String, - dir: PathBuf + dir: PathBuf, } impl Domain { pub fn new(name: &str, dir: PathBuf) -> Self { std::fs::create_dir_all(&dir).unwrap(); - Self { name: name.to_string(), dir } + Self { + name: name.to_string(), + dir, + } } pub fn path(&self, path: &str) -> Document { @@ -23,15 +30,19 @@ impl Domain { pub struct Document { pub domain: String, pub path: String, - base_dir: PathBuf + base_dir: PathBuf, } impl Document { pub fn new(domain: &str, path: &str, base_dir: PathBuf) -> Self { - Self { domain: domain.to_string(), path: path.to_string(), base_dir } + Self { + domain: domain.to_string(), + path: path.to_string(), + base_dir, + } } - pub fn render_local(&self, version: Option) -> String { + pub fn render_local(&self, version: Option) -> Option { let mut file_path = self.base_dir.join(&self.domain); for p in self.path.split('/') { @@ -42,18 +53,18 @@ impl Document { format!("index_{version}.html") } else { let versions = Self::versions(&file_path); - versions.first().cloned().unwrap() + versions.first().cloned()? }; file_path = file_path.join(latest_version); // TODO : Replace links with local ones - return std::fs::read_to_string(file_path).unwrap(); + return std::fs::read_to_string(file_path).ok(); } pub fn versions(path: &PathBuf) -> Vec { let mut version_list = Vec::new(); - + if let Ok(entries) = std::fs::read_dir(path) { for entry in entries { if let Ok(entry) = entry { @@ -63,14 +74,16 @@ impl Document { } } } - + version_list } } impl WebsiteArchive { pub fn new(dir: &str) -> Self { - Self { dir: PathBuf::from(dir) } + Self { + dir: PathBuf::from(dir), + } } pub fn get_domain(&self, domain: &str) -> Domain { @@ -96,13 +109,13 @@ impl WebsiteArchive { let timestamp = chrono::Utc::now().format("%Y-%m-%d").to_string(); let filename = folder_name.join(&format!("index_{timestamp}.html")); - + run_command(&vec![ "monolith", "-I", "-o", filename.to_str().unwrap(), - &format!("https://{}/{}", domain, path) + &format!("https://{}/{}", domain, path), ]); } } @@ -112,7 +125,6 @@ impl WebsiteArchive { // transparent auto page downloading // redownload after threshold - fn run_command(cmd: &[&str]) { let mut cmd_setup = std::process::Command::new(cmd[0].clone()); let cmd_setup = cmd_setup.args(cmd.into_iter().skip(1).collect::>()); diff --git a/src/main.rs b/src/main.rs index 9d73e77..d728d1f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,8 @@ use archive::WebsiteArchive; use based::get_pg; use rocket::routes; -mod pages; mod archive; +mod pages; #[rocket::launch] async fn launch() -> _ { @@ -15,12 +15,6 @@ async fn launch() -> _ { let arc = WebsiteArchive::new("./websites"); rocket::build() - .mount( - "/", - routes![ - pages::index, - pages::render_website - ], - ) + .mount("/", routes![pages::index, pages::render_website]) .manage(arc) } diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 923935d..87ba675 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -13,10 +13,37 @@ pub async fn index() -> StringResponse { } #[get("/s//?