create & attach an AdHoc Fairing for dealing with Custom Domains

this rewrites the URL to /custom_domain/<url>
(we have no route handlers for this path yet)

Lots of help from @fdb-hiroshima & @BaptisteGelez in dealing with the
borrow checker — thank you 💜
This commit is contained in:
Igor Galić 2019-05-26 11:59:53 +02:00 committed by Igor Galić
parent 351c01f71c
commit 3a4c2f2cf9
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 18 additions and 3 deletions

View File

@ -25,6 +25,7 @@ use posts::Post;
use safe_string::SafeString; use safe_string::SafeString;
use schema::blogs; use schema::blogs;
use search::Searcher; use search::Searcher;
use std::fmt::{self, Display};
use users::User; use users::User;
use {Connection, Error, PlumeRocket, Result}; use {Connection, Error, PlumeRocket, Result};
@ -39,6 +40,12 @@ impl Host {
} }
} }
impl Display for Host {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl AsRef<str> for Host { impl AsRef<str> for Host {
fn as_ref(&self) -> &str { fn as_ref(&self) -> &str {
&self.0 &self.0

View File

@ -42,12 +42,14 @@ extern crate webfinger;
use clap::App; use clap::App;
use diesel::r2d2::ConnectionManager; use diesel::r2d2::ConnectionManager;
use plume_models::{ use plume_models::{
blogs::Host,
db_conn::{DbPool, PragmaForeignKey}, db_conn::{DbPool, PragmaForeignKey},
instance::Instance, instance::Instance,
migrations::IMPORTED_MIGRATIONS, migrations::IMPORTED_MIGRATIONS,
search::{Searcher as UnmanagedSearcher, SearcherError}, search::{Searcher as UnmanagedSearcher, SearcherError},
Connection, Error, CONFIG, Connection, Error, CONFIG,
}; };
use rocket::{fairing::AdHoc, http::uri::Origin};
use rocket_csrf::CsrfFairingBuilder; use rocket_csrf::CsrfFairingBuilder;
use scheduled_thread_pool::ScheduledThreadPool; use scheduled_thread_pool::ScheduledThreadPool;
use std::process::exit; use std::process::exit;
@ -175,8 +177,13 @@ Then try to restart Plume
println!("Please refer to the documentation to see how to configure it."); println!("Please refer to the documentation to see how to configure it.");
} }
let custom_domains = plume_models::blogs::Blog::list_custom_domains(&dbpool.get().unwrap()).unwrap(); let custom_domain_fairing = AdHoc::on_request("Custom Blog Domains", |req, _data| {
dbg!(custom_domains); let host = req.guard::<Host>();
if host.is_success() {
let rewrite_uri = format!("/custom_domains/{}/{}", host.unwrap(), req.uri());
req.set_uri(Origin::parse_owned(rewrite_uri).unwrap())
}
});
let rocket = rocket::custom(CONFIG.rocket.clone().unwrap()) let rocket = rocket::custom(CONFIG.rocket.clone().unwrap())
.mount( .mount(
@ -317,7 +324,8 @@ Then try to restart Plume
]) ])
.finalize() .finalize()
.expect("main: csrf fairing creation error"), .expect("main: csrf fairing creation error"),
); )
.attach(custom_domain_fairing);
#[cfg(feature = "test")] #[cfg(feature = "test")]
let rocket = rocket.mount("/test", routes![test_routes::health,]); let rocket = rocket.mount("/test", routes![test_routes::health,]);