Migrate to actix-web #8
157
Cargo.lock
generated
157
Cargo.lock
generated
|
@ -204,63 +204,12 @@ dependencies = [
|
|||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "actix-web-flash-messages"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "05c89d051c33e7044e8a6e811ed06dda6ce65dfb51a1e379146f723116ca362a"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"time",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "aead"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b613b8e1e3cf911a086f53f03bf286f52fd7a7258e4fa606f0ef220d39d8877"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aes"
|
||||
version = "0.7.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e8b47f52ea9bae42228d07ec09eb676433d7c4ed1ebdf0f1d1c29ed446f1ab8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cipher",
|
||||
"cpufeatures",
|
||||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aes-gcm"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df5f85a83a7d8b0442b6aa7b504b8212c1733da07b98aae43d4bc21b2cb3cdf6"
|
||||
dependencies = [
|
||||
"aead",
|
||||
"aes",
|
||||
"cipher",
|
||||
"ctr",
|
||||
"ghash",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ahash"
|
||||
version = "0.7.6"
|
||||
|
@ -296,12 +245,6 @@ dependencies = [
|
|||
"alloc-no-stdlib",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.64"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9a8f622bcf6ff3df478e9deba3e03e4e04b300f8e6a139e192c05fa3490afc7"
|
||||
|
||||
[[package]]
|
||||
name = "askama_escape"
|
||||
version = "0.10.3"
|
||||
|
@ -397,15 +340,6 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "cipher"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ee52072ec15386f770805afd189a01c8841be8696bed250fa2f13c4c0d6dfb7"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "3.2.17"
|
||||
|
@ -457,14 +391,7 @@ version = "0.16.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94d4706de1b0fa5b132270cddffa8585166037822e260a944fe161acd137ca05"
|
||||
dependencies = [
|
||||
"aes-gcm",
|
||||
"base64",
|
||||
"hkdf",
|
||||
"hmac",
|
||||
"percent-encoding",
|
||||
"rand",
|
||||
"sha2",
|
||||
"subtle",
|
||||
"time",
|
||||
"version_check",
|
||||
]
|
||||
|
@ -497,15 +424,6 @@ dependencies = [
|
|||
"typenum",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ctr"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea"
|
||||
dependencies = [
|
||||
"cipher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derive_more"
|
||||
version = "0.99.17"
|
||||
|
@ -527,7 +445,6 @@ checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
|
|||
dependencies = [
|
||||
"block-buffer",
|
||||
"crypto-common",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -635,16 +552,6 @@ dependencies = [
|
|||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ghash"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1583cc1656d7839fd3732b80cf4f38850336cdb9b8ded1cd399ca62958de3c99"
|
||||
dependencies = [
|
||||
"opaque-debug",
|
||||
"polyval",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "h2"
|
||||
version = "0.3.13"
|
||||
|
@ -700,24 +607,6 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hkdf"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "791a029f6b9fc27657f6f188ec6e5e43f6911f6f878e0dc5501396e09809d437"
|
||||
dependencies = [
|
||||
"hmac",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hmac"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e"
|
||||
dependencies = [
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "0.2.8"
|
||||
|
@ -912,12 +801,6 @@ version = "1.13.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1"
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5"
|
||||
|
||||
[[package]]
|
||||
name = "os_str_bytes"
|
||||
version = "6.3.0"
|
||||
|
@ -1015,18 +898,6 @@ version = "0.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
|
||||
|
||||
[[package]]
|
||||
name = "polyval"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8419d2b623c7c0896ff2d5d96e2cb4ede590fed28fcc34934f4c33c036e620a1"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"opaque-debug",
|
||||
"universal-hash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.16"
|
||||
|
@ -1232,17 +1103,6 @@ dependencies = [
|
|||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sha2"
|
||||
version = "0.10.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9db03534dff993187064c4e0c05a5708d2a9728ace9a8959b77bedf415dac5"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cpufeatures",
|
||||
"digest",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.0"
|
||||
|
@ -1283,12 +1143,6 @@ version = "0.10.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.99"
|
||||
|
@ -1374,7 +1228,6 @@ version = "0.1.0"
|
|||
dependencies = [
|
||||
"actix-files",
|
||||
"actix-web",
|
||||
"actix-web-flash-messages",
|
||||
"clap",
|
||||
"derive_more",
|
||||
"env_logger",
|
||||
|
@ -1480,16 +1333,6 @@ dependencies = [
|
|||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "universal-hash"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9f214e8f697e925001e66ec2c6e37a4ef93f0f78c2eed7814394e10c62025b05"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"subtle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.2.2"
|
||||
|
|
|
@ -6,7 +6,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
actix-files = "0.6.2"
|
||||
actix-web = "4.1.0"
|
||||
actix-web-flash-messages = {version = "0.4", features = ["cookies"]}
|
||||
clap = { version = "3.2.17", features = ["derive"] }
|
||||
derive_more = "0.99.17"
|
||||
env_logger = "0.9.0"
|
||||
|
|
|
@ -5,9 +5,9 @@ use actix_web::http::StatusCode;
|
|||
use actix_web::middleware::ErrorHandlerResponse;
|
||||
use actix_web::{web, HttpResponse, ResponseError};
|
||||
use derive_more::{Display, Error};
|
||||
use serde_json::json;
|
||||
|
||||
use super::SharedData;
|
||||
use crate::template_utils::{ErrorMessage, TemplateArgs};
|
||||
|
||||
#[derive(Debug, Display, Error)]
|
||||
pub enum UserError {
|
||||
|
@ -52,14 +52,12 @@ pub fn generic_error<B>(
|
|||
handlebars,
|
||||
config: _,
|
||||
}) => {
|
||||
let args = TemplateArgs::builder()
|
||||
.error_page(ErrorMessage::new(message, status_code.as_u16()))
|
||||
.build();
|
||||
|
||||
let body = handlebars
|
||||
.render(
|
||||
"error",
|
||||
&json!({
|
||||
"message": message,
|
||||
"status_code": status_code.as_u16()
|
||||
}),
|
||||
)
|
||||
.render("error", &args)
|
||||
.map_err(|_| UserError::InternalError)?;
|
||||
|
||||
HttpResponse::build(res.status())
|
||||
|
|
|
@ -12,6 +12,7 @@ use handlebars::Handlebars;
|
|||
|
||||
mod errors;
|
||||
mod main_pages;
|
||||
mod template_utils;
|
||||
|
||||
use errors::generic_error;
|
||||
use main_pages::{mail_post, static_file, template, template_index};
|
||||
|
@ -38,7 +39,7 @@ struct SharedData<'a> {
|
|||
}
|
||||
|
||||
#[actix_web::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
async fn main() -> std::io::Result<()> {
|
||||
let mut config = Config::parse();
|
||||
config.template_directory = config.template_directory.canonicalize()?;
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ use log::info;
|
|||
use serde::Deserialize;
|
||||
|
||||
use crate::errors::UserError;
|
||||
use crate::template_utils::TemplateArgs;
|
||||
use crate::template_utils::{Flash, FlashType};
|
||||
use crate::SharedData;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
|
@ -85,10 +87,14 @@ pub(crate) async fn mail_post(
|
|||
) -> actix_web::Result<impl Responder> {
|
||||
info!("{:?}", form);
|
||||
|
||||
let args = TemplateArgs::builder()
|
||||
.flash(Flash::new("Mail successfully sent!", FlashType::Success))
|
||||
.build();
|
||||
|
||||
if shared.handlebars.has_template("mail") {
|
||||
let body = shared
|
||||
.handlebars
|
||||
.render("mail", &())
|
||||
.render("mail", &args)
|
||||
.map_err(|_| UserError::InternalError)?;
|
||||
Ok(HttpResponse::Ok().body(body))
|
||||
} else {
|
||||
|
|
87
src/template_utils.rs
Normal file
87
src/template_utils.rs
Normal file
|
@ -0,0 +1,87 @@
|
|||
use serde::Serialize;
|
||||
|
||||
/** All arguments that can be given to a template. */
|
||||
#[derive(Serialize)]
|
||||
pub struct TemplateArgs {
|
||||
flash: Option<Flash>,
|
||||
error: Option<ErrorMessage>,
|
||||
}
|
||||
|
||||
impl TemplateArgs {
|
||||
pub fn builder() -> TemplateArgsBuilder {
|
||||
TemplateArgsBuilder::new()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TemplateArgsBuilder {
|
||||
flash: Option<Flash>,
|
||||
error: Option<ErrorMessage>,
|
||||
}
|
||||
|
||||
impl TemplateArgsBuilder {
|
||||
pub fn new() -> Self {
|
||||
TemplateArgsBuilder {
|
||||
flash: None,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn flash(mut self, flash: Flash) -> Self {
|
||||
self.flash = Some(flash);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn error_page(mut self, error: ErrorMessage) -> Self {
|
||||
self.error = Some(error);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn build(self) -> TemplateArgs {
|
||||
TemplateArgs {
|
||||
flash: self.flash,
|
||||
error: self.error,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** A flash message that should be displayed as a notification on the page. */
|
||||
#[derive(Serialize)]
|
||||
pub struct Flash {
|
||||
message: String,
|
||||
#[serde(rename = "type")]
|
||||
level: FlashType,
|
||||
}
|
||||
|
||||
impl Flash {
|
||||
pub fn new(message: &str, level: FlashType) -> Self {
|
||||
Self {
|
||||
message: message.to_string(),
|
||||
level,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum FlashType {
|
||||
Info,
|
||||
Success,
|
||||
Warning,
|
||||
Danger,
|
||||
}
|
||||
|
||||
/** Contents of an error page. */
|
||||
#[derive(Serialize)]
|
||||
pub struct ErrorMessage {
|
||||
message: String,
|
||||
status_code: u16,
|
||||
}
|
||||
|
||||
impl ErrorMessage {
|
||||
pub fn new(message: &str, status_code: u16) -> Self {
|
||||
Self {
|
||||
message: message.to_string(),
|
||||
status_code,
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue