From 2a22617752268390a91384cb1a227ef2894cc1df Mon Sep 17 00:00:00 2001 From: Delta1925 Date: Sun, 16 Apr 2023 01:07:43 +0200 Subject: [PATCH] Fix webpage not being rendered --- assets/webpage/delete/index.html | 2 +- assets/webpage/submit/index.html | 2 +- src/confirmation.rs | 1 - src/errors.rs | 3 ++ src/main.rs | 55 ++++++++++++++++++++++++++------ src/management.rs | 1 - src/utils.rs | 9 +++++- 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/assets/webpage/delete/index.html b/assets/webpage/delete/index.html index 0f0806e..58ff97d 100644 --- a/assets/webpage/delete/index.html +++ b/assets/webpage/delete/index.html @@ -9,7 +9,7 @@
-
+ diff --git a/assets/webpage/submit/index.html b/assets/webpage/submit/index.html index 3be1b3f..a1171e9 100644 --- a/assets/webpage/submit/index.html +++ b/assets/webpage/submit/index.html @@ -9,7 +9,7 @@
- + diff --git a/src/confirmation.rs b/src/confirmation.rs index 8eeb2fb..76bf6e3 100644 --- a/src/confirmation.rs +++ b/src/confirmation.rs @@ -7,7 +7,6 @@ use crate::management::{delete_key, Action, Pending}; use crate::pending_path; use crate::settings::{MAILER, SETTINGS}; use crate::utils::{get_email_from_cert, get_filename, parse_pem}; -use crate::PENDING_FOLDER; use lettre::{Message, Transport}; use std::fs; diff --git a/src/errors.rs b/src/errors.rs index 4e323e8..945806e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -31,6 +31,8 @@ pub enum Error { MailGeneration, #[error("(0x0E) Wrong email domain")] WrongDomain, + #[error("(0x0F) The requested file does not exist")] + MissingFile, } impl actix_web::ResponseError for Error { @@ -38,6 +40,7 @@ impl actix_web::ResponseError for Error { match self { Self::MissingPending => StatusCode::from_u16(404).unwrap(), Self::MissingKey => StatusCode::from_u16(404).unwrap(), + Self::MissingFile => StatusCode::from_u16(404).unwrap(), Self::WrongDomain => StatusCode::from_u16(401).unwrap(), _ => StatusCode::from_u16(500).unwrap(), } diff --git a/src/main.rs b/src/main.rs index 69e072c..f058fac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod management; mod settings; mod utils; +use crate::errors::Error; use crate::settings::SETTINGS; use crate::utils::is_email_allowed; @@ -11,8 +12,12 @@ use self::confirmation::{confirm_action, send_confirmation_email}; use self::management::{clean_stale, store_pending_addition, store_pending_deletion, Action}; use self::utils::{gen_random_token, get_email_from_cert, parse_pem}; -use actix_web::{get, post, web, App, HttpServer, Result}; -use log::{error, info}; +use actix_web::http::header::ContentType; +use actix_web::http::StatusCode; +use actix_web::{ + get, post, web, App, HttpRequest, HttpResponse, HttpResponseBuilder, HttpServer, Result, +}; +use log::{debug, error, info}; use serde::Deserialize; use std::env; use std::fs; @@ -20,8 +25,6 @@ use std::path::Path; use tokio::{task, time}; use utils::init_logger; -const PENDING_FOLDER: &str = "pending"; - #[derive(Deserialize, Debug)] struct Key { key: String, @@ -60,10 +63,42 @@ async fn main() -> std::io::Result<()> { "Running server on http://127.0.0.1:{} (External URL: {})", SETTINGS.port, SETTINGS.external_url ); - HttpServer::new(|| App::new().service(submit).service(confirm).service(delete)) - .bind(("127.0.0.1", SETTINGS.port))? - .run() - .await + HttpServer::new(|| { + App::new() + .service(submit) + .service(confirm) + .service(delete) + .route("/{filename:.*}", web::get().to(index)) + }) + .bind(("127.0.0.1", SETTINGS.port))? + .run() + .await +} + +async fn index(req: HttpRequest) -> Result { + let path = webpage_path!().join(req.match_info().query("filename")); + for file in &["", "index.html"] { + let path = if file.is_empty() { + path.to_owned() + } else { + path.join(file) + }; + if path.is_file() { + let template = match fs::read_to_string(&path) { + Ok(template) => template, + Err(_) => { + debug!("file {} is inaccessible", path.display()); + return Err(Error::Inaccessible); + } + }; + let page = template.replace("{{%u}}", SETTINGS.external_url.as_ref()); + return Ok(HttpResponseBuilder::new(StatusCode::OK) + .insert_header(ContentType::html()) + .body(page)); + } + } + debug!("File {} does not exist", path.display()); + Err(Error::MissingFile) } #[post("/api/submit")] @@ -94,5 +129,7 @@ async fn delete(email: web::Query) -> Result { store_pending_deletion(email.email.clone(), &token)?; send_confirmation_email(&email.email, &Action::Delete, &token)?; info!("User {} requested the deletion of his key!", email.email); - Ok(String::from("(0x00) Deletion request submitted successfully!")) + Ok(String::from( + "(0x00) Deletion request submitted successfully!", + )) } diff --git a/src/management.rs b/src/management.rs index cf35b0f..e184042 100644 --- a/src/management.rs +++ b/src/management.rs @@ -1,7 +1,6 @@ use crate::pending_path; use crate::settings::SETTINGS; use crate::utils::{get_user_file_path, key_exists}; -use crate::PENDING_FOLDER; use crate::{errors::Error, utils::get_filename}; use chrono::Utc; diff --git a/src/utils.rs b/src/utils.rs index 1ccce72..8f706ea 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -10,7 +10,14 @@ use std::path::{Path, PathBuf}; #[macro_export] macro_rules! pending_path { () => { - Path::new(&SETTINGS.root_folder).join(PENDING_FOLDER) + Path::new(&SETTINGS.root_folder).join("pending") + }; +} + +#[macro_export] +macro_rules! webpage_path { + () => { + Path::new("assets").join("webpage") }; }