Fix Rocket API
This commit is contained in:
parent
164c38dd87
commit
f606d31074
|
@ -21,6 +21,7 @@ structopt = "0.2"
|
|||
url = "1.6"
|
||||
hex = "0.3"
|
||||
lettre_email = "0.8"
|
||||
handlebars = "1.1.0"
|
||||
|
||||
[dependencies.lettre]
|
||||
version = "0.8"
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::io::Cursor;
|
|||
use std::convert::TryFrom;
|
||||
|
||||
use time;
|
||||
use sequoia_openpgp::{packet::Signature, TPK, packet::UserID, Packet, PacketPile, constants::SignatureType, serialize::Serialize};
|
||||
use sequoia_openpgp::{packet::Signature, TPK, packet::UserID, Packet, PacketPile, constants::SignatureType, parse::Parse};
|
||||
use Result;
|
||||
use types::{Fingerprint, Email};
|
||||
|
||||
|
|
21
src/mail.rs
21
src/mail.rs
|
@ -1,5 +1,4 @@
|
|||
use rocket_contrib::Template;
|
||||
|
||||
use handlebars::Handlebars;
|
||||
use lettre::{SendmailTransport, EmailTransport};
|
||||
use lettre_email::EmailBuilder;
|
||||
|
||||
|
@ -19,8 +18,21 @@ fn send_mail<T>(to: &Email, subject: &str, template_dir: &str,
|
|||
template_base: &str, domain: &str, ctx: T)
|
||||
-> Result<()> where T: Serialize + Clone
|
||||
{
|
||||
let html = Template::show(template_dir, format!("{}-html", template_base), ctx.clone());
|
||||
let txt = Template::show(template_dir, format!("{}-txt", template_base), ctx);
|
||||
// TODO: Should be done only on startup
|
||||
let tmpl = format!("{}/{}", template_dir, template_base);
|
||||
let mut handlebars = Handlebars::new();
|
||||
handlebars.register_template_file("html", format!("{}-html.hbs", tmpl)).unwrap();
|
||||
handlebars.register_template_file("txt", format!("{}-txt.hbs", tmpl)).unwrap();
|
||||
|
||||
let (html, txt) = {
|
||||
if let (Ok(inner_html), Ok(inner_txt)) =
|
||||
(handlebars.render("html", &ctx), handlebars.render("txt", &ctx)) {
|
||||
(Some(inner_html), Some(inner_txt))
|
||||
} else {
|
||||
(None, None)
|
||||
}
|
||||
};
|
||||
|
||||
let email = EmailBuilder::new()
|
||||
.to(to.to_string())
|
||||
.from(format!("noreply@{}", domain))
|
||||
|
@ -31,7 +43,6 @@ fn send_mail<T>(to: &Email, subject: &str, template_dir: &str,
|
|||
.build().unwrap();
|
||||
|
||||
let mut sender = SendmailTransport::new();
|
||||
|
||||
sender.send(&email)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#![feature(plugin, decl_macro, custom_derive)]
|
||||
#![plugin(rocket_codegen)]
|
||||
#![feature(proc_macro_hygiene, plugin, decl_macro, custom_derive)]
|
||||
#![recursion_limit = "1024"]
|
||||
#![feature(try_from)]
|
||||
|
||||
|
@ -11,8 +10,8 @@ extern crate time;
|
|||
extern crate url;
|
||||
extern crate hex;
|
||||
|
||||
#[cfg(not(test))] extern crate rocket;
|
||||
#[cfg(test)] extern crate rocket;
|
||||
#[cfg(not(test))] #[macro_use] extern crate rocket;
|
||||
#[cfg(test)] #[macro_use] extern crate rocket;
|
||||
extern crate rocket_contrib;
|
||||
extern crate multipart;
|
||||
|
||||
|
@ -25,6 +24,7 @@ extern crate parking_lot;
|
|||
extern crate structopt;
|
||||
extern crate lettre;
|
||||
extern crate lettre_email;
|
||||
extern crate handlebars;
|
||||
|
||||
mod web;
|
||||
mod database;
|
||||
|
|
|
@ -6,7 +6,7 @@ use rocket::response::status::Custom;
|
|||
use rocket::response::NamedFile;
|
||||
use rocket::fairing::AdHoc;
|
||||
|
||||
use rocket_contrib::Template;
|
||||
use rocket_contrib::templates::Template;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
mod upload;
|
||||
|
@ -61,7 +61,10 @@ impl<'a, 'r> FromRequest<'a, 'r> for queries::Hkp {
|
|||
use std::collections::HashMap;
|
||||
|
||||
let query = request.uri().query().unwrap_or("");
|
||||
let fields = FormItems::from(query).map(|(k,v)| {
|
||||
let fields = FormItems::from(query).map(|item| {
|
||||
|
||||
let (k, v) = item.key_value();
|
||||
|
||||
let key = k.url_decode().unwrap_or_default();
|
||||
let value = v.url_decode().unwrap_or_default();
|
||||
(key, value)
|
||||
|
@ -291,8 +294,8 @@ pub fn serve(opt: &Opt, db: Polymorphic) -> Result<()> {
|
|||
.port(port)
|
||||
.workers(2)
|
||||
.root(opt.base.clone())
|
||||
.extra("template_dir", format!("{}/templates", opt.base.display()))
|
||||
.extra("static_dir", format!("{}/public", opt.base.display()))
|
||||
.extra("template_dir", opt.base.join("templates").to_str().ok_or("Template path invalid")?)
|
||||
.extra("static_dir", opt.base.join("public").to_str().ok_or("Static path invalid")?)
|
||||
.extra("domain", opt.domain.clone())
|
||||
.finalize()?;
|
||||
let routes = routes![
|
||||
|
@ -311,9 +314,9 @@ pub fn serve(opt: &Opt, db: Polymorphic) -> Result<()> {
|
|||
confirm,
|
||||
];
|
||||
|
||||
rocket::custom(config, opt.verbose)
|
||||
rocket::custom(config)
|
||||
.attach(Template::fairing())
|
||||
.attach(AdHoc::on_attach(|rocket| {
|
||||
.attach(AdHoc::on_attach("static_dir", |rocket| {
|
||||
let static_dir = rocket.config()
|
||||
.get_str("static_dir")
|
||||
.unwrap()
|
||||
|
@ -321,7 +324,7 @@ pub fn serve(opt: &Opt, db: Polymorphic) -> Result<()> {
|
|||
|
||||
Ok(rocket.manage(StaticDir(static_dir)))
|
||||
}))
|
||||
.attach(AdHoc::on_attach(|rocket| {
|
||||
.attach(AdHoc::on_attach("template_dir", |rocket| {
|
||||
let static_dir = rocket.config()
|
||||
.get_str("template_dir")
|
||||
.unwrap()
|
||||
|
@ -329,7 +332,7 @@ pub fn serve(opt: &Opt, db: Polymorphic) -> Result<()> {
|
|||
|
||||
Ok(rocket.manage(MailTemplateDir(static_dir)))
|
||||
}))
|
||||
.attach(AdHoc::on_attach(|rocket| {
|
||||
.attach(AdHoc::on_attach("domain", |rocket| {
|
||||
let static_dir = rocket.config()
|
||||
.get_str("domain")
|
||||
.unwrap()
|
||||
|
|
|
@ -5,7 +5,7 @@ use multipart::server::save::SaveResult::*;
|
|||
use rocket::{State, Data};
|
||||
use rocket::http::{ContentType, Status};
|
||||
use rocket::response::status::Custom;
|
||||
use rocket_contrib::Template;
|
||||
use rocket_contrib::templates::Template;
|
||||
|
||||
use types::Email;
|
||||
use mail::send_verification_mail;
|
||||
|
@ -44,7 +44,7 @@ pub fn multipart_upload(db: State<Polymorphic>, cont_type: &ContentType,
|
|||
)
|
||||
)?;
|
||||
|
||||
process_upload(boundary, data, db.inner(), &tmpl.0, &domain.0)
|
||||
process_upload(boundary, data, db.inner(), &(tmpl.0)[..], &(domain.0)[..])
|
||||
} else if cont_type.is_form() {
|
||||
use rocket::request::FormItems;
|
||||
use std::io::Cursor;
|
||||
|
@ -57,7 +57,8 @@ pub fn multipart_upload(db: State<Polymorphic>, cont_type: &ContentType,
|
|||
"`Content-Type: application/x-www-form-urlencoded` not valid".into()))
|
||||
})?;
|
||||
|
||||
for (key, value) in FormItems::from(&*String::from_utf8_lossy(&buf)) {
|
||||
for item in FormItems::from(&*String::from_utf8_lossy(&buf)) {
|
||||
let (key, value) = item.key_value();
|
||||
let decoded_value = value.url_decode().or_else(|_| {
|
||||
Err(Custom(Status::BadRequest,
|
||||
"`Content-Type: application/x-www-form-urlencoded` not valid".into()))
|
||||
|
@ -66,7 +67,7 @@ pub fn multipart_upload(db: State<Polymorphic>, cont_type: &ContentType,
|
|||
match key.as_str() {
|
||||
"keytext" => {
|
||||
return process_key(Cursor::new(decoded_value.as_bytes()),
|
||||
&db, &tmpl.0, &domain.0);
|
||||
&db, &(tmpl.0)[..], &(domain.0)[..]);
|
||||
}
|
||||
_ => { /* skip */ }
|
||||
}
|
||||
|
@ -112,11 +113,8 @@ fn process_multipart(entries: Entries, db: &Polymorphic, tmpl: &str,
|
|||
fn process_key<R>(reader: R, db: &Polymorphic, tmpl: &str, domain: &str)
|
||||
-> Result<Template, Custom<String>> where R: Read
|
||||
{
|
||||
use sequoia_openpgp::{Reader, TPK};
|
||||
let reader = Reader::from_reader(reader).or_else(|_| {
|
||||
Err(Custom(Status::BadRequest,
|
||||
"`Content-Type: application/x-www-form-urlencoded` not valid".into()))
|
||||
})?;
|
||||
use sequoia_openpgp::TPK;
|
||||
use sequoia_openpgp::parse::Parse;
|
||||
|
||||
match TPK::from_reader(reader) {
|
||||
Ok(tpk) => {
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
{{#if verified }}
|
||||
<h1>Email verified</h1>
|
||||
<p>You've verified <em>{{ userid }}</em> successfully. Everybody who knows
|
||||
your email address is no able to find your key. You can <a href="/vks/delete/{{
|
||||
your email address is now able to find your key. You can <a href="/vks/delete/{{
|
||||
fpr }}">delete</a> your key any time you want.</p>
|
||||
{{else}}
|
||||
<h1>Email verification failed</h1>
|
||||
|
|
Loading…
Reference in New Issue