From 374411862c27a59d73d9633395d75c1774309d76 Mon Sep 17 00:00:00 2001 From: peterrabbit <peterrabbit@msi.home> Date: Sat, 20 Aug 2022 11:58:53 +0200 Subject: [PATCH] static files manager build index --- src/main.rs | 33 +++++---------------------------- src/service.rs | 2 ++ src/service/page.rs | 12 ++++++++++++ src/static_files.rs | 40 +++++++++++++++++++++++++++++----------- src/testing.rs | 34 ++++++++++++++++++++++++++++++++++ src/website.rs | 36 ++---------------------------------- 6 files changed, 84 insertions(+), 73 deletions(-) create mode 100644 src/service.rs create mode 100644 src/service/page.rs create mode 100644 src/testing.rs diff --git a/src/main.rs b/src/main.rs index 299e502..1bd1725 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,37 +1,16 @@ mod app_state; +mod service; mod static_files; +mod testing; mod tls_config; mod website; -use actix_web::{get, App, HttpResponse, HttpServer, Responder}; +use actix_web::{App, HttpServer}; use actix_web_lab::middleware::RedirectHttps; use app_state::AppState; use static_files::StaticFilesManager; -use std::path::PathBuf; use tls_config::tls_config; use website::WebSite; -#[get("/{pth:.*}")] -async fn page( - website: actix_web::web::Data<WebSite>, - pth: actix_web::web::Path<PathBuf>, -) -> impl Responder { - let pth = pth.into_inner(); - match website.get_page_by_url(&pth) { - Some(page) => HttpResponse::Ok().body(page.html_doc.to_string()), - None => HttpResponse::NotFound().body(format!("Not found {}", pth.display())), - } -} - -#[get("/admin/dashboard")] -async fn admin_dashboard() -> impl Responder { - HttpResponse::Ok().body("Admin") -} - -#[get("/admin/login")] -async fn admin_login() -> impl Responder { - HttpResponse::Ok().body("Login") -} - #[actix_web::main] async fn main() -> std::io::Result<()> { let app_state = AppState::new(); @@ -43,7 +22,7 @@ async fn main() -> std::io::Result<()> { let website = WebSite::load(&app_state.config); let mut static_files_manager = StaticFilesManager::new(&app_state).unwrap(); - static_files_manager.build_index().unwrap(); + static_files_manager.build_index(); let host = app_state.config.host.clone(); let port = app_state.config.port; @@ -64,9 +43,7 @@ async fn main() -> std::io::Result<()> { .app_data(actix_web::web::Data::clone(&app_state)) .app_data(actix_web::web::Data::clone(&static_files_manager)) .app_data(actix_web::web::Data::new(website.clone())) - .service(admin_dashboard) - .service(admin_login) - .service(page) + .service(service::page) }) .bind(format!("{}:{}", host, port))? .bind_rustls(format!("{}:{}", host, port_tls), srv_conf)? diff --git a/src/service.rs b/src/service.rs new file mode 100644 index 0000000..f5f67f8 --- /dev/null +++ b/src/service.rs @@ -0,0 +1,2 @@ +mod page; +pub use page::*; diff --git a/src/service/page.rs b/src/service/page.rs new file mode 100644 index 0000000..197a50e --- /dev/null +++ b/src/service/page.rs @@ -0,0 +1,12 @@ +use crate::WebSite; +use actix_web::{get, web, HttpResponse, Responder}; +use std::path::PathBuf; + +#[get("/{pth:.*}")] +pub async fn page(website: web::Data<WebSite>, pth: web::Path<PathBuf>) -> impl Responder { + let pth = pth.into_inner(); + match website.get_page_by_url(&pth) { + Some(page) => HttpResponse::Ok().body(page.html_doc.to_string()), + None => HttpResponse::NotFound().body(format!("Not found {}", pth.display())), + } +} diff --git a/src/static_files.rs b/src/static_files.rs index f8eb2fc..c4fbab6 100644 --- a/src/static_files.rs +++ b/src/static_files.rs @@ -1,5 +1,5 @@ use crate::app_state::AppState; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; pub struct StaticFilesManager { dir: PathBuf, @@ -7,6 +7,16 @@ pub struct StaticFilesManager { } impl StaticFilesManager { + pub fn new(app_state: &AppState) -> Result<Self, String> { + match Self::create_dir_if_missing(&app_state.config.storage_dir) { + Ok(dir) => Ok(StaticFilesManager { + index: Vec::new(), + dir, + }), + Err(msg) => Err(msg), + } + } + fn create_dir_if_missing(app_dir: &PathBuf) -> Result<PathBuf, String> { let static_dir = app_dir.join("static"); @@ -44,18 +54,26 @@ impl StaticFilesManager { } } - pub fn new(app_state: &AppState) -> Result<Self, String> { - match Self::create_dir_if_missing(&app_state.config.storage_dir) { - Ok(dir) => Ok(StaticFilesManager { - index: Vec::new(), - dir, - }), - Err(msg) => Err(msg), + fn rec_read_dir(&mut self, root: &Path, strip_from: &Path) { + for entry in root.read_dir().unwrap() { + if let Ok(entry) = entry { + if entry.path().is_dir() { + self.rec_read_dir(&entry.path(), strip_from); + } else { + self._push_path(&entry.path(), strip_from); + } + } } } - pub fn build_index(&mut self) -> Result<(), String> { - self.index.push("TODO".to_string()); - Ok(()) + fn _push_path(&mut self, path: &Path, strip_from: &Path) { + let push_path = path.strip_prefix(strip_from).unwrap(); + self.index + .push(format!("/{}", push_path.to_str().unwrap().to_owned())); + } + + pub fn build_index(&mut self) { + self.index = Vec::new(); + self.rec_read_dir(&self.dir.clone(), &self.dir.clone()); } } diff --git a/src/testing.rs b/src/testing.rs new file mode 100644 index 0000000..dec579b --- /dev/null +++ b/src/testing.rs @@ -0,0 +1,34 @@ +#[cfg(test)] +pub const TEST_JSON_WEBSITE: &'static str = " +{ + \"page_data\": { + \"title\": \"Test Website\", + \"slug\": \"\", + \"lang\": \"en\", + \"description\": \"A test website\", + \"html_body\": \"<h1>Test Website</h1>\" + }, + \"sub_pages\": [ + { + \"page_data\": { + \"title\": \"A sub page\", + \"slug\": \"subpage\", + \"lang\": \"en\", + \"description\": \"A sub page of the testing web site\", + \"html_body\": \"<h1>A sub page</h1>\" + }, + \"sub_pages\": [ + { + \"page_data\": { + \"title\": \"Nested page\", + \"lang\": \"en\", + \"slug\": \"nested\", + \"description\": \"Nested testing page\", + \"html_body\": \"<h1>Nested page</h1>\" + } + } + ] + } + ] +} +"; diff --git a/src/website.rs b/src/website.rs index 5d0b95c..c65d5c5 100644 --- a/src/website.rs +++ b/src/website.rs @@ -140,43 +140,11 @@ impl WebSite { #[cfg(test)] mod test_website { use super::*; - const JSON_TEMPLATE: &'static str = " -{ - \"page_data\": { - \"title\": \"Test Website\", - \"slug\": \"\", - \"lang\": \"en\", - \"description\": \"A test website\", - \"html_body\": \"<h1>Test Website</h1>\" - }, - \"sub_pages\": [ - { - \"page_data\": { - \"title\": \"A sub page\", - \"slug\": \"subpage\", - \"lang\": \"en\", - \"description\": \"A sub page of the testing web site\", - \"html_body\": \"<h1>A sub page</h1>\" - }, - \"sub_pages\": [ - { - \"page_data\": { - \"title\": \"Nested page\", - \"lang\": \"en\", - \"slug\": \"nested\", - \"description\": \"Nested testing page\", - \"html_body\": \"<h1>Nested page</h1>\" - } - } - ] - } - ] -} - "; + use crate::testing::TEST_JSON_WEBSITE; #[test] fn test_index_pages_by_slug() { - let website = WebSite::from_json_str(JSON_TEMPLATE); + let website = WebSite::from_json_str(TEST_JSON_WEBSITE); let root_page = website.get_page_by_url(&PathBuf::from("/")); assert!(root_page.is_some()); let root_page = root_page.unwrap(); -- GitLab