cache custom_domains list

follow #572 and cache the list of custom domains.
This commit is contained in:
Igor Galić 2019-05-26 22:00:36 +02:00 committed by Igor Galić
parent 2746e088ae
commit e6747de998
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
2 changed files with 17 additions and 2 deletions

View File

@ -26,6 +26,7 @@ use safe_string::SafeString;
use schema::blogs; use schema::blogs;
use search::Searcher; use search::Searcher;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::sync::RwLock;
use users::User; use users::User;
use {Connection, Error, PlumeRocket, Result}; use {Connection, Error, PlumeRocket, Result};
@ -93,6 +94,10 @@ pub struct NewBlog {
const BLOG_PREFIX: &str = "~"; const BLOG_PREFIX: &str = "~";
lazy_static! {
static ref CUSTOM_DOMAINS: RwLock<Vec<String>> = RwLock::new(vec![]);
}
impl Blog { impl Blog {
insert!(blogs, NewBlog, |inserted, conn| { insert!(blogs, NewBlog, |inserted, conn| {
let instance = inserted.get_instance(conn)?; let instance = inserted.get_instance(conn)?;
@ -319,7 +324,15 @@ impl Blog {
.map_err(Error::from) .map_err(Error::from)
} }
pub fn list_custom_domains(conn: &Connection) -> Result<Vec<String>> { pub fn list_custom_domains() -> Vec<String> {
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<Vec<String>> {
blogs::table blogs::table
.filter(blogs::custom_domain.is_not_null()) .filter(blogs::custom_domain.is_not_null())
.select(blogs::custom_domain) .select(blogs::custom_domain)
@ -337,7 +350,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Host {
.headers() .headers()
.get_one("Host") .get_one("Host")
.and_then(|x| { .and_then(|x| {
if x != Instance::get_local().ok()?.public_domain { if Blog::list_custom_domains().contains(&x.to_string()) {
Some(Host::new(x)) Some(Host::new(x))
} else { } else {
None None

View File

@ -42,6 +42,7 @@ extern crate webfinger;
use clap::App; use clap::App;
use diesel::r2d2::ConnectionManager; use diesel::r2d2::ConnectionManager;
use plume_models::{ use plume_models::{
blogs::Blog,
blogs::Host, blogs::Host,
db_conn::{DbPool, PragmaForeignKey}, db_conn::{DbPool, PragmaForeignKey},
instance::Instance, instance::Instance,
@ -89,6 +90,7 @@ fn init_pool() -> Option<DbPool> {
.build(manager) .build(manager)
.ok()?; .ok()?;
Instance::cache_local(&pool.get().unwrap()); Instance::cache_local(&pool.get().unwrap());
Blog::cache_custom_domains(&pool.get().unwrap());
Some(pool) Some(pool)
} }