diff --git a/plume-models/src/blogs.rs b/plume-models/src/blogs.rs index 2a7fa121..ebd8fab5 100644 --- a/plume-models/src/blogs.rs +++ b/plume-models/src/blogs.rs @@ -26,6 +26,7 @@ use safe_string::SafeString; use schema::blogs; use search::Searcher; use std::fmt::{self, Display}; +use std::sync::RwLock; use users::User; use {Connection, Error, PlumeRocket, Result}; @@ -93,6 +94,10 @@ pub struct NewBlog { const BLOG_PREFIX: &str = "~"; +lazy_static! { + static ref CUSTOM_DOMAINS: RwLock> = RwLock::new(vec![]); +} + impl Blog { insert!(blogs, NewBlog, |inserted, conn| { let instance = inserted.get_instance(conn)?; @@ -319,7 +324,15 @@ impl Blog { .map_err(Error::from) } - pub fn list_custom_domains(conn: &Connection) -> Result> { + pub fn list_custom_domains() -> Vec { + CUSTOM_DOMAINS.read().unwrap().clone() + } + + pub fn cache_custom_domains(conn: &Connection) { + *CUSTOM_DOMAINS.write().unwrap() = Blog::list_custom_domains_uncached(conn).unwrap(); + } + + pub fn list_custom_domains_uncached(conn: &Connection) -> Result> { blogs::table .filter(blogs::custom_domain.is_not_null()) .select(blogs::custom_domain) @@ -337,7 +350,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Host { .headers() .get_one("Host") .and_then(|x| { - if x != Instance::get_local().ok()?.public_domain { + if Blog::list_custom_domains().contains(&x.to_string()) { Some(Host::new(x)) } else { None diff --git a/src/main.rs b/src/main.rs index 86d0dfc6..ec138744 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ extern crate webfinger; use clap::App; use diesel::r2d2::ConnectionManager; use plume_models::{ + blogs::Blog, blogs::Host, db_conn::{DbPool, PragmaForeignKey}, instance::Instance, @@ -89,6 +90,7 @@ fn init_pool() -> Option { .build(manager) .ok()?; Instance::cache_local(&pool.get().unwrap()); + Blog::cache_custom_domains(&pool.get().unwrap()); Some(pool) }