Handle post to mail page

This commit is contained in:
Tristan Daniël Maat 2022-09-09 18:41:04 +01:00
parent 2847dad110
commit 91e905730e
Signed by: tlater
GPG key ID: 49670FD774E43268
3 changed files with 47 additions and 3 deletions

19
Cargo.lock generated
View file

@ -1040,9 +1040,23 @@ checksum = "93f6841e709003d68bb2deee8c343572bf446003ec20a583e76f7b15cebf3711"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.143" version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53e8e5d5b70924f74ff5c6d64d9a5acd91422117c60f48c4e07855238a254553" checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.144"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
@ -1219,6 +1233,7 @@ dependencies = [
"env_logger", "env_logger",
"handlebars", "handlebars",
"log", "log",
"serde",
"serde_json", "serde_json",
] ]

View file

@ -11,4 +11,5 @@ derive_more = "0.99.17"
env_logger = "0.9.0" env_logger = "0.9.0"
handlebars = { version = "4.3.3", features = ["dir_source"] } handlebars = { version = "4.3.3", features = ["dir_source"] }
log = "0.4.17" log = "0.4.17"
serde = {version = "1.0.144", features = ["derive"]}
serde_json = "1.0.83" serde_json = "1.0.83"

View file

@ -3,11 +3,13 @@ use std::net::SocketAddr;
use std::path::PathBuf; use std::path::PathBuf;
use actix_files::NamedFile; use actix_files::NamedFile;
use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use actix_web::http::{Method, StatusCode}; use actix_web::http::{Method, StatusCode};
use actix_web::middleware::{self, ErrorHandlers}; use actix_web::middleware::{self, ErrorHandlers};
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
use clap::Parser; use clap::Parser;
use handlebars::Handlebars; use handlebars::Handlebars;
use log::info;
use serde::Deserialize;
mod errors; mod errors;
@ -97,6 +99,31 @@ async fn static_file(
} }
} }
#[derive(Clone, Debug, Deserialize)]
struct Mail {
mail: String,
subject: String,
message: String,
}
#[post("/mail.html")]
async fn mail_post(
shared: web::Data<SharedData<'_>>,
form: web::Form<Mail>,
) -> actix_web::Result<impl Responder> {
info!("{:?}", form);
if shared.handlebars.has_template("mail") {
let body = shared
.handlebars
.render("mail", &())
.map_err(|_| UserError::InternalError)?;
Ok(HttpResponse::Ok().body(body))
} else {
Err(UserError::InternalError)?
}
}
#[actix_web::main] #[actix_web::main]
async fn main() -> Result<(), std::io::Error> { async fn main() -> Result<(), std::io::Error> {
let mut config = Config::parse(); let mut config = Config::parse();
@ -127,6 +154,7 @@ async fn main() -> Result<(), std::io::Error> {
.service(template) .service(template)
.service(static_file) .service(static_file)
.service(template_index) .service(template_index)
.service(mail_post)
.default_service(web::route().method(Method::GET)) .default_service(web::route().method(Method::GET))
}) })
.bind(config.address)? .bind(config.address)?