Handle post to mail page
This commit is contained in:
parent
2847dad110
commit
91e905730e
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -1040,9 +1040,23 @@ checksum = "93f6841e709003d68bb2deee8c343572bf446003ec20a583e76f7b15cebf3711"
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.143"
|
||||
version = "1.0.144"
|
||||
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]]
|
||||
name = "serde_json"
|
||||
|
@ -1219,6 +1233,7 @@ dependencies = [
|
|||
"env_logger",
|
||||
"handlebars",
|
||||
"log",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
|
|
|
@ -11,4 +11,5 @@ derive_more = "0.99.17"
|
|||
env_logger = "0.9.0"
|
||||
handlebars = { version = "4.3.3", features = ["dir_source"] }
|
||||
log = "0.4.17"
|
||||
serde = {version = "1.0.144", features = ["derive"]}
|
||||
serde_json = "1.0.83"
|
||||
|
|
30
src/main.rs
30
src/main.rs
|
@ -3,11 +3,13 @@ use std::net::SocketAddr;
|
|||
use std::path::PathBuf;
|
||||
|
||||
use actix_files::NamedFile;
|
||||
use actix_web::{get, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::http::{Method, StatusCode};
|
||||
use actix_web::middleware::{self, ErrorHandlers};
|
||||
use actix_web::{get, post, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use clap::Parser;
|
||||
use handlebars::Handlebars;
|
||||
use log::info;
|
||||
use serde::Deserialize;
|
||||
|
||||
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]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let mut config = Config::parse();
|
||||
|
@ -127,6 +154,7 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
.service(template)
|
||||
.service(static_file)
|
||||
.service(template_index)
|
||||
.service(mail_post)
|
||||
.default_service(web::route().method(Method::GET))
|
||||
})
|
||||
.bind(config.address)?
|
||||
|
|
Loading…
Reference in a new issue