mirror of
https://git.verdigado.com/NB-Public/simple-wkd.git
synced 2024-10-30 05:05:52 +01:00
Fix webpage not being rendered
This commit is contained in:
parent
a91b213dbf
commit
2a22617752
7 changed files with 59 additions and 14 deletions
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
<div class="flex flex-row items-center h-full font-mono px-8 py-4">
|
<div class="flex flex-row items-center h-full font-mono px-8 py-4">
|
||||||
<div class="flex flex-col items-center w-full">
|
<div class="flex flex-col items-center w-full">
|
||||||
<form action="{{%u}}/api/delete" method="get" class="flex flex-col max-w-full">
|
<form action="{{%u}}api/delete" method="get" class="flex flex-col max-w-full">
|
||||||
<label for="email" class="font-bold text-xl mt-3">Request key deletion:</label>
|
<label for="email" class="font-bold text-xl mt-3">Request key deletion:</label>
|
||||||
<input type="email" name="email" id="email-input" placeholder="e-mail" size="65" class="round focus:rounded-lg mt-3" required="">
|
<input type="email" name="email" id="email-input" placeholder="e-mail" size="65" class="round focus:rounded-lg mt-3" required="">
|
||||||
<button type="submit" class="button mt-4 self-end">Delete</button>
|
<button type="submit" class="button mt-4 self-end">Delete</button>
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
<div class="flex flex-row items-center h-full font-mono px-8 py-4">
|
<div class="flex flex-row items-center h-full font-mono px-8 py-4">
|
||||||
<div class="flex flex-col items-center w-full">
|
<div class="flex flex-col items-center w-full">
|
||||||
<form action="{{%u}}/api/submit" method="post" class="flex flex-col max-w-full">
|
<form action="{{%u}}api/submit" method="post" class="flex flex-col max-w-full">
|
||||||
<label for="key" class="font-bold text-xl">Paste your pgp keyblock here:</label>
|
<label for="key" class="font-bold text-xl">Paste your pgp keyblock here:</label>
|
||||||
<textarea name="key" id="key-input" cols="65" rows="20" class="round focus:rounded-lg mt-3 resize-none" placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----" required=""></textarea>
|
<textarea name="key" id="key-input" cols="65" rows="20" class="round focus:rounded-lg mt-3 resize-none" placeholder="-----BEGIN PGP PUBLIC KEY BLOCK-----" required=""></textarea>
|
||||||
<button type="submit" class="button mt-6 self-end">Submit</button>
|
<button type="submit" class="button mt-6 self-end">Submit</button>
|
||||||
|
|
|
@ -7,7 +7,6 @@ use crate::management::{delete_key, Action, Pending};
|
||||||
use crate::pending_path;
|
use crate::pending_path;
|
||||||
use crate::settings::{MAILER, SETTINGS};
|
use crate::settings::{MAILER, SETTINGS};
|
||||||
use crate::utils::{get_email_from_cert, get_filename, parse_pem};
|
use crate::utils::{get_email_from_cert, get_filename, parse_pem};
|
||||||
use crate::PENDING_FOLDER;
|
|
||||||
|
|
||||||
use lettre::{Message, Transport};
|
use lettre::{Message, Transport};
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
|
@ -31,6 +31,8 @@ pub enum Error {
|
||||||
MailGeneration,
|
MailGeneration,
|
||||||
#[error("(0x0E) Wrong email domain")]
|
#[error("(0x0E) Wrong email domain")]
|
||||||
WrongDomain,
|
WrongDomain,
|
||||||
|
#[error("(0x0F) The requested file does not exist")]
|
||||||
|
MissingFile,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl actix_web::ResponseError for Error {
|
impl actix_web::ResponseError for Error {
|
||||||
|
@ -38,6 +40,7 @@ impl actix_web::ResponseError for Error {
|
||||||
match self {
|
match self {
|
||||||
Self::MissingPending => StatusCode::from_u16(404).unwrap(),
|
Self::MissingPending => StatusCode::from_u16(404).unwrap(),
|
||||||
Self::MissingKey => 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(),
|
Self::WrongDomain => StatusCode::from_u16(401).unwrap(),
|
||||||
_ => StatusCode::from_u16(500).unwrap(),
|
_ => StatusCode::from_u16(500).unwrap(),
|
||||||
}
|
}
|
||||||
|
|
49
src/main.rs
49
src/main.rs
|
@ -4,6 +4,7 @@ mod management;
|
||||||
mod settings;
|
mod settings;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
|
||||||
|
use crate::errors::Error;
|
||||||
use crate::settings::SETTINGS;
|
use crate::settings::SETTINGS;
|
||||||
use crate::utils::is_email_allowed;
|
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::management::{clean_stale, store_pending_addition, store_pending_deletion, Action};
|
||||||
use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
|
use self::utils::{gen_random_token, get_email_from_cert, parse_pem};
|
||||||
|
|
||||||
use actix_web::{get, post, web, App, HttpServer, Result};
|
use actix_web::http::header::ContentType;
|
||||||
use log::{error, info};
|
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 serde::Deserialize;
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
@ -20,8 +25,6 @@ use std::path::Path;
|
||||||
use tokio::{task, time};
|
use tokio::{task, time};
|
||||||
use utils::init_logger;
|
use utils::init_logger;
|
||||||
|
|
||||||
const PENDING_FOLDER: &str = "pending";
|
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Key {
|
struct Key {
|
||||||
key: String,
|
key: String,
|
||||||
|
@ -60,12 +63,44 @@ async fn main() -> std::io::Result<()> {
|
||||||
"Running server on http://127.0.0.1:{} (External URL: {})",
|
"Running server on http://127.0.0.1:{} (External URL: {})",
|
||||||
SETTINGS.port, SETTINGS.external_url
|
SETTINGS.port, SETTINGS.external_url
|
||||||
);
|
);
|
||||||
HttpServer::new(|| App::new().service(submit).service(confirm).service(delete))
|
HttpServer::new(|| {
|
||||||
|
App::new()
|
||||||
|
.service(submit)
|
||||||
|
.service(confirm)
|
||||||
|
.service(delete)
|
||||||
|
.route("/{filename:.*}", web::get().to(index))
|
||||||
|
})
|
||||||
.bind(("127.0.0.1", SETTINGS.port))?
|
.bind(("127.0.0.1", SETTINGS.port))?
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn index(req: HttpRequest) -> Result<HttpResponse, Error> {
|
||||||
|
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")]
|
#[post("/api/submit")]
|
||||||
async fn submit(pem: web::Form<Key>) -> Result<String> {
|
async fn submit(pem: web::Form<Key>) -> Result<String> {
|
||||||
let cert = parse_pem(&pem.key)?;
|
let cert = parse_pem(&pem.key)?;
|
||||||
|
@ -94,5 +129,7 @@ async fn delete(email: web::Query<Email>) -> Result<String> {
|
||||||
store_pending_deletion(email.email.clone(), &token)?;
|
store_pending_deletion(email.email.clone(), &token)?;
|
||||||
send_confirmation_email(&email.email, &Action::Delete, &token)?;
|
send_confirmation_email(&email.email, &Action::Delete, &token)?;
|
||||||
info!("User {} requested the deletion of his key!", email.email);
|
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!",
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::pending_path;
|
use crate::pending_path;
|
||||||
use crate::settings::SETTINGS;
|
use crate::settings::SETTINGS;
|
||||||
use crate::utils::{get_user_file_path, key_exists};
|
use crate::utils::{get_user_file_path, key_exists};
|
||||||
use crate::PENDING_FOLDER;
|
|
||||||
use crate::{errors::Error, utils::get_filename};
|
use crate::{errors::Error, utils::get_filename};
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
|
@ -10,7 +10,14 @@ use std::path::{Path, PathBuf};
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! pending_path {
|
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")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue