moving to using a rust api

This commit is contained in:
Chris Cochrun 2024-01-08 10:51:53 -06:00
parent 2d1424aeb2
commit f00ed3a04c
12 changed files with 1044 additions and 272 deletions

1
src/api/mod.rs Normal file
View file

@ -0,0 +1 @@
pub mod mt_form;

526
src/api/mt_form.rs Normal file
View file

@ -0,0 +1,526 @@
use std::fs;
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
use actix_web::{post, HttpResponse};
use lettre::{
message::{header::ContentType, Attachment, MultiPart},
transport::smtp::authentication::{Credentials, Mechanism},
Message, SmtpTransport, Transport,
};
#[derive(Debug, MultipartForm, Default)]
struct MtForm {
#[multipart(rename = "firstname")]
first_name: Option<Text<String>>,
#[multipart(rename = "lastname")]
last_name: Option<Text<String>>,
#[multipart(rename = "parentfirstname")]
parent_first_name: Option<Text<String>>,
#[multipart(rename = "parentlastname")]
parent_last_name: Option<Text<String>>,
birthdate: Option<Text<String>>,
gender: Option<Text<String>>,
street: Option<Text<String>>,
city: Option<Text<String>>,
state: Option<Text<String>>,
zip: Option<Text<i32>>,
cellphone: Option<Text<String>>,
parentphone: Option<Text<String>>,
email: Option<Text<String>>,
parentemail: Option<Text<String>>,
school: Option<Text<String>>,
grade: Option<Text<String>>,
#[multipart(rename = "pastorfirstname")]
pastor_first_name: Option<Text<String>>,
#[multipart(rename = "pastorlastname")]
pastor_last_name: Option<Text<String>>,
#[multipart(rename = "churchattendance")]
church_attendance: Option<Text<String>>,
#[multipart(rename = "tfcgroup")]
tfc_group: Option<Text<String>>,
shirt: Option<Text<String>>,
trip: Option<Text<String>>,
#[multipart(rename = "tripnotes")]
trip_notes: Option<Text<String>>,
#[multipart(rename = "relationship-with-jesus")]
relationship_with_jesus: Option<Text<String>>,
#[multipart(rename = "testimony")]
testimony: Option<Text<String>>,
#[multipart(rename = "involvement-with-group")]
involvement_with_group: Option<Text<String>>,
#[multipart(rename = "reasons-for-trip-choice")]
reasons: Option<Text<String>>,
strengths: Option<Text<String>>,
weaknesses: Option<Text<String>>,
#[multipart(rename = "previous-trip-info")]
previous_trip_info: Option<Text<String>>,
#[multipart(rename = "attitude-torward-work")]
attitude: Option<Text<String>>,
#[multipart(rename = "relevant-notes")]
relevant_notes: Option<Text<String>>,
#[multipart(rename = "final-agreement")]
final_agreement: Option<Text<String>>,
registration: Option<Text<String>>,
#[multipart(rename = "image")]
file: Option<TempFile>,
}
#[post("/mt-form")]
pub async fn mt_form(MultipartForm(form): MultipartForm<MtForm>) -> HttpResponse {
let first = form.first_name.as_ref().unwrap().0.clone();
let last = form.last_name.as_ref().unwrap().0.clone();
let email_subject = format!("{} {} signed up for mission trip!", first, last);
let filename_noext = String::from(format!("{}_{}", first, last));
let parent = format!(
"{} {}",
form.parent_first_name.as_ref().unwrap().0.clone(),
form.parent_last_name.as_ref().unwrap().0.clone()
);
let birthdate = form
.birthdate
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let gender = form
.gender
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let street = form
.street
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let city = form
.city
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let state = form
.state
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let zip = form.zip.as_ref().unwrap_or(&Text { 0: 0 }).0.clone();
let cellphone = form
.cellphone
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let parentphone = form
.parentphone
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let email = form
.email
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let parentemail = form
.parentemail
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let school = form
.school
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let grade = form
.grade
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let pastor = format!(
"{} {}",
form.pastor_first_name
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone(),
form.pastor_last_name
.as_ref()
.unwrap_or(&Text {
0: String::from("")
})
.0
.clone()
);
let church_attendance = form
.church_attendance
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let tfc_group = form
.tfc_group
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let shirt = form
.shirt
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let trip = form
.trip
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let trip_notes = form
.trip_notes
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let relationship = form
.relationship_with_jesus
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let testimony = form
.testimony
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let involvement = form
.involvement_with_group
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let reasons = form
.reasons
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let strengths = form
.strengths
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let weaknesses = form
.weaknesses
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let previous_trip = form
.previous_trip_info
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let attitude = form
.attitude
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let relevant = form
.relevant_notes
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let final_agreement = form
.final_agreement
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
let registration = form
.registration
.as_ref()
.unwrap_or(&Text {
0: String::from(""),
})
.0
.clone();
log::info!("{first} {last} signed up for mission trip!");
let email = markup::new! {
@markup::doctype()
html {
head {
title { @format!("{} {} signed up for mission trip!", first, last) }
style {
"table { border-collapse: collapse; width: 100% }"
"td, th { padding: 8px }"
"td { text-align: left; width: 70%; word-wrap: break-word }"
"th { text-align: right; border-right: 1px solid #ddd }"
"tr { border-bottom: 1px solid #ddd }"
"h1 { text-align: center }"
}
}
body {
h1 { @format!("Mission trip form for {} {}!", first, last) }
hr;
table {
tr {
th { "Name" }
td { @format!("{} {}", first, last) }
}
tr {
th { "Parent" }
td { @parent }
}
tr {
th { "Birthdate" }
td { @birthdate }
}
tr {
th { "Gender" }
td { @gender }
}
tr {
th { "Street" }
td { @street }
}
tr {
th { "City" }
td { @city }
}
tr {
th { "State" }
td { @state }
}
tr {
th { "Zip" }
td { @zip }
}
tr {
th { "Phone" }
td { @cellphone }
}
tr {
th { "Parent Phone" }
td { @parentphone }
}
tr {
th { "Email" }
td { @email }
}
tr {
th { "Parent Email" }
td { @parentemail }
}
tr {
th { "School" }
td { @school }
}
tr {
th { "Grade" }
td { @grade }
}
tr {
th { "Pastor" }
td { @pastor }
}
tr {
th { "Church Attendance" }
td { @church_attendance }
}
tr {
th { "TFC Group" }
td { @tfc_group }
}
tr {
th { "T-Shirt Size" }
td { @shirt }
}
tr {
th { "Trip Choice" }
td { @trip }
}
tr {
th { "Extra Trip Notes" }
td { @trip_notes }
}
tr {
th { "Relationship with Jesus" }
td { @relationship }
}
tr {
th { "Testimony" }
td { @testimony }
}
tr {
th { "Involvement with TFC or Youth Group" }
td { @involvement }
}
tr {
th { "Reasons for trip choice" }
td { @reasons }
}
tr {
th { "Strengths" }
td { @strengths }
}
tr {
th { "Weaknesses" }
td { @weaknesses }
}
tr {
th { "Previous Trips" }
td { @previous_trip }
}
tr {
th { "Attitude Torward Work" }
td { @attitude }
}
tr {
th { "Other Relevant Info" }
td { @relevant }
}
tr {
th { "Final Agreement" }
td { @final_agreement }
}
tr {
th { "Registration" }
td { @registration }
}
}
}
}
};
let mut path: Option<String> = Some(String::from(""));
let mut file_exists = false;
log::info!("{:?}", form);
log::info!("{:?}", file_exists);
if let Some(f) = form.file {
if let Some(file) = f.file_name {
if let Some(ext) = file.rsplit(".").next() {
path = Some(format!("./tmp/{}.{}", filename_noext, ext));
} else {
path = Some(format!("./tmp/{}", file));
}
// let path = format!("./tmp/{}", file);
log::info!("saving to {}", path.clone().unwrap());
match f.file.persist(path.clone().unwrap()) {
Ok(f) => {
log::info!("{:?}", f);
if f.metadata().unwrap().len() > 0 {
file_exists = true;
}
}
Err(e) => log::info!("{:?}: Probably a missing image", e),
}
}
}
let multi = if file_exists {
let filebody = fs::read(path.clone().unwrap_or_default());
let content_type = ContentType::parse("image/jpg").unwrap();
let attachment =
Attachment::new(path.unwrap_or_default()).body(filebody.unwrap(), content_type);
log::info!("{:?}", attachment);
MultiPart::alternative_plain_html(String::from("Testing"), email.to_string())
.singlepart(attachment)
} else {
MultiPart::alternative_plain_html(String::from("Testing"), email.to_string())
};
if let Ok(m) = Message::builder()
.from(
"TFC ADMIN <no-reply@mail.tfcconnection.org>"
.parse()
.unwrap(),
)
.to("Chris Cochrun <chris@tfcconnection.org>".parse().unwrap())
.to("Ethan Rose <ethan@tfcconnection.org>".parse().unwrap())
.subject(email_subject)
.multipart(multi)
{
let sender = SmtpTransport::relay("mail.tfcconnection.org")
.ok()
.unwrap()
.credentials(Credentials::new(
"no-reply@mail.tfcconnection.org".to_owned(),
"r9f36mNZFtiW4f".to_owned(),
))
.authentication(vec![Mechanism::Plain])
.build();
match sender.send(&m) {
Ok(res) => log::info!("{:?}", res),
Err(e) => log::info!("{e}"),
}
} else {
log::info!("Email incorrect");
}
HttpResponse::Ok().body("hi")
}

View file

@ -283,32 +283,94 @@ with the image attached to us"
(uiop:println form)
(let ((first-name (cdr (assoc "firstname" form :test 'string=)))
(last-name (cdr (assoc "lastname" form :test 'string=))))
(cl-smtp:send-email
"mail.tfcconnection.org"
"no-reply@mail.tfcconnection.org"
'("chris@tfcconnection.org" "chris@cochrun.xyz")
(format nil "~a ~a filled out a Mission Trip Form!" first-name last-name)
(format nil "Mission Trip Form for ~a ~a" first-name last-name)
:display-name "TFC ADMIN"
:ssl :tls
:authentication '(:login "no-reply@mail.tfcconnection.org" "r9f36mNZFtiW4f")
:attachments attachment
:html-message
(with-html-string
(:doctype)
(:html
(:head (:title "TFC Mission Trip Form")
(:style (apply #'lass:compile-and-write *mail-css*)))
(:body
(:h1 (format nil "Mission Trip Form for ~a ~a" first-name last-name))
(:hr)
(:table
(loop for row in form
do (:tr
(:th (car row))
(:td (trim-whitespace
(cdr row)))))))))))
(uiop:println "Mail sent!"))
(not (cl-smtp:send-email
"mail.tfcconnection.org"
"no-reply@mail.tfcconnection.org"
"chris@cochrun.xyz"
(format nil "~a ~a filled out a Mission Trip Form!" first-name last-name)
(format nil "Mission Trip Form for ~a ~a" first-name last-name)
:display-name "TFC ADMIN"
:ssl :tls
:authentication '(:login "no-reply@mail.tfcconnection.org" "r9f36mNZFtiW4f")
:attachments attachment
:html-message
(with-html-string
(:doctype)
(:html
(:head (:title "TFC Mission Trip Form")
(:style (apply #'lass:compile-and-write *mail-css*)))
(:body
(:h1 (format nil "Mission Trip Form for ~a ~a" first-name last-name))
(:hr)
(:table
(loop for row in form
do (:tr
(:th (trim-whitespace (car row)))
(:td (trim-whitespace
(cdr row)))))))))))))
(cl-smtp:send-email
"mail.tfcconnection.org"
"no-reply@mail.tfcconnection.org"
"chris@cochrun.xyz"
(format nil "~a ~a filled out a Mission Trip Form!" "Chris" "Ccohrun")
"hi"
:html-message
(let ((form '(("age" . "150")
("registration" . "later")
("final-agreement" . "yes")
("relevant-notes" . "")
("attitude-toward-work" . "")
("previous-trip-info" . "")
("weaknesses" . "")
("strengths" . "")
("reasons-for-trip-choice" . "")
("involvement-with-group" . "")
("testimony" . "")
("relationship-with-jesus" . "")
("tripnotes" . "")
("trip" . "New Mexico")
("shirt" . "small")
("tfcgroup" . "Phillipsburg")
("churchattendanceother" . "")
("churchattendance" . "yes")
("church" . "")
("pastorphone" . "9991112222")
("pastorlastname" . "The White")
("pastorfirstname" . "Gandalf")
("grade" . "sophomore")
("school" . "A cool one")
("parentemail" . "bilbosmells@braggins.xyz")
("email" . "chris@cochrun.xyz")
("parentphone" . "8889990000")
("cellphone" . "7853021664")
("zip" . "67661")
("state" . "Middle Earth")
("city" . "The Shire")
("street" . "1234 Bag End, 98 Hobbiton")
("gender" . "Male")
("birthdate" . "1873-11-23")
("parentlastname" . "Braggins")
("parentfirstname" . "Bilbo")
("lastname" . "Braggins")
("firstname" . "Frodo"))))
(with-html-string
(:doctype)
(:html
(:head (:title "TFC Mission Trip Form")
(:style (apply #'lass:compile-and-write *mail-css*)))
(:body
(:h1 (format nil "Mission Trip Form for ~a ~a" "Chris" "Ccohrun"))
(:hr)
(:table
(loop for row in form
do (:tr
(:th (trim-whitespace (car row)))
(:td (trim-whitespace
(cdr row))))))))))
:ssl :tls
:authentication '(:login "no-reply@mail.tfcconnection.org" "r9f36mNZFtiW4f")
)
(defun mail-form (form attachment)
"Takes the form as an alist and sends a table formatted email
@ -450,65 +512,70 @@ with the image attached"
((eq request-type :post)
(loop :for i :in parts
:do (let* ((content-disposition
(nth 1
(serapeum:lines i
:eol-style
:crlf
:honor-crlf t)))
(start (if content-disposition
(position #\" content-disposition)))
(end (if start
(position #\" content-disposition
:start (1+ start)) nil))
(name (if end
(if start
(subseq content-disposition
(1+ start) end) nil) nil))
(content
(if (string= name "image")
(butlast
(rest
(rest
(rest
(rest
(serapeum:lines i
:eol-style
:crlf
:honor-crlf t
:keep-eols t))))))
(trim-whitespace
(car
(butlast
(rest
(rest
(rest
(serapeum:lines i :eol-style :crlf
:honor-crlf t
:keep-eols t)))))))))
(file-start
(if (string= name "image")
(position #\" content-disposition
:start 44)))
(file-end
(if file-start
(position #\" content-disposition
:start (1+ file-start)) nil))
(image-file-name
(if (string= name "image")
(subseq content-disposition
(1+ file-start) file-end) nil)))
(nth 1
(serapeum:lines i
:eol-style
:crlf
:honor-crlf t)))
(start (if content-disposition
(position #\" content-disposition)))
(end (if start
(position #\" content-disposition
:start (1+ start)) nil))
(name (if end
(if start
(subseq content-disposition
(1+ start) end) nil) nil))
(content
(if (string= name "image")
nil
;; (butlast
;; (rest
;; (rest
;; (rest
;; (rest
;; (serapeum:lines i
;; :eol-style
;; :crlf
;; :honor-crlf t
;; :keep-eols t))))))
(trim-whitespace
(car
(butlast
(rest
(rest
(rest
(serapeum:lines i :eol-style :crlf
:honor-crlf t
:keep-eols t)))))))))
(file-start
(if (string= name "image")
(position #\" content-disposition
:start 44)))
(file-end
(if file-start
(position #\" content-disposition
:start (1+ file-start)) nil))
(image-file-name
(if (string= name "image")
(subseq content-disposition
(1+ file-start) file-end) nil)))
(if name
(if (string= name "image")
(if (uiop:file-exists-p image-file-name)
(progn
(save-string-file
(apply #'concatenate 'string content)
image-file-name)
(setq form-list (acons name image-file-name form-list))
(setf attachment image-file-name)))
(when (stringp content)
(progn
(if (not (uiop:file-exists-p image-file-name))
(save-string-file
(apply #'concatenate 'string content)
image-file-name))
(setf attachment image-file-name)
(delete-file image-file-name)))
(setq form-list (acons name content form-list))))))
(if (mail-mt-form form-list attachment)
(format nil "thankyou"))))))
(progn
(uiop:println "Mail sent")
(format nil "thankyou")))))))
(hunchentoot:define-easy-handler (respond :uri "/health-form") ()
(setf (hunchentoot:content-type*) "plain/text")

26
src/main.rs Normal file
View file

@ -0,0 +1,26 @@
mod api;
use actix_multipart::form::tempfile::TempFileConfig;
use actix_web::{middleware, App, HttpServer};
use api::mt_form::mt_form;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("creating temporary upload directory");
std::fs::create_dir_all("./tmp")?;
log::info!("starting HTTP server at http://localhost:4242");
HttpServer::new(|| {
App::new()
.wrap(middleware::Logger::default())
.app_data(TempFileConfig::default().directory("./tmp"))
.service(mt_form)
})
.bind(("127.0.0.1", 4242))?
.workers(2)
.run()
.await
}