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 37441186 authored by peterrabbit's avatar peterrabbit
Browse files

static files manager build index

parent c060de32
No related branches found
No related tags found
No related merge requests found
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)?
......
mod page;
pub use page::*;
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())),
}
}
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());
}
}
#[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>\"
}
}
]
}
]
}
";
......@@ -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();
......
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