Centralize configuration and add some new config (#494)
Ideally, if someone could review the idea in [this comment](https://github.com/Plume-org/Plume/issues/273#issuecomment-474982184), I'd like to add the implementation to this pr too
This commit is contained in:
committed by
Baptiste Gelez
parent
b945d1f602
commit
65bb50e88f
@@ -26,7 +26,7 @@ use safe_string::SafeString;
|
||||
use schema::blogs;
|
||||
use search::Searcher;
|
||||
use users::User;
|
||||
use {Connection, Error, Result, BASE_URL, USE_HTTPS};
|
||||
use {Connection, Error, Result, CONFIG};
|
||||
|
||||
pub type CustomGroup = CustomObject<ApSignature, Group>;
|
||||
|
||||
@@ -142,7 +142,7 @@ impl Blog {
|
||||
}
|
||||
|
||||
fn fetch_from_webfinger(conn: &Connection, acct: &str) -> Result<Blog> {
|
||||
resolve(acct.to_owned(), *USE_HTTPS)?
|
||||
resolve(acct.to_owned(), true)?
|
||||
.links
|
||||
.into_iter()
|
||||
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
||||
@@ -288,7 +288,7 @@ impl Blog {
|
||||
Blog::find_by_ap_url(conn, url).or_else(|_| {
|
||||
// The requested blog was not in the DB
|
||||
// We try to fetch it if it is remote
|
||||
if Url::parse(url)?.host_str()? != BASE_URL.as_str() {
|
||||
if Url::parse(url)?.host_str()? != CONFIG.base_url.as_str() {
|
||||
Blog::fetch_from_url(conn, url)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
use std::env::var;
|
||||
use rocket::Config as RocketConfig;
|
||||
use rocket::config::Limits;
|
||||
|
||||
#[cfg(not(test))]
|
||||
const DB_NAME: &str = "plume";
|
||||
#[cfg(test)]
|
||||
const DB_NAME: &str = "plume_tests";
|
||||
|
||||
pub struct Config {
|
||||
pub base_url: String,
|
||||
pub db_name: &'static str,
|
||||
pub database_url: String,
|
||||
pub search_index: String,
|
||||
pub rocket: Result<RocketConfig, RocketError>,
|
||||
}
|
||||
|
||||
#[derive(Debug,Clone)]
|
||||
pub enum RocketError {
|
||||
InvalidEnv,
|
||||
InvalidAddress,
|
||||
InvalidSecretKey,
|
||||
}
|
||||
|
||||
fn get_rocket_config() -> Result<RocketConfig, RocketError> {
|
||||
let mut c = RocketConfig::active().map_err(|_| RocketError::InvalidEnv)?;
|
||||
|
||||
let address = var("ROCKET_ADDRESS").unwrap_or_else(|_| "localhost".to_owned());
|
||||
let port = var("ROCKET_PORT").ok().map(|s| s.parse::<u16>().unwrap()).unwrap_or(7878);
|
||||
let secret_key = var("ROCKET_SECRET_KEY").map_err(|_| RocketError::InvalidSecretKey)?;
|
||||
let form_size = var("FORM_SIZE").unwrap_or_else(|_| "32".to_owned()).parse::<u64>().unwrap();
|
||||
let activity_size = var("ACTIVITY_SIZE").unwrap_or_else(|_| "1024".to_owned()).parse::<u64>().unwrap();
|
||||
|
||||
c.set_address(address).map_err(|_| RocketError::InvalidAddress)?;
|
||||
c.set_port(port);
|
||||
c.set_secret_key(secret_key).map_err(|_| RocketError::InvalidSecretKey) ?;
|
||||
|
||||
c.set_limits(Limits::new()
|
||||
.limit("forms", form_size * 1024)
|
||||
.limit("json", activity_size * 1024));
|
||||
|
||||
Ok(c)
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref CONFIG: Config = Config {
|
||||
base_url: var("BASE_URL").unwrap_or_else(|_| format!(
|
||||
"127.0.0.1:{}",
|
||||
var("ROCKET_PORT").unwrap_or_else(|_| "8000".to_owned()
|
||||
))),
|
||||
db_name: DB_NAME,
|
||||
#[cfg(feature = "postgres")]
|
||||
database_url: var("DATABASE_URL").unwrap_or_else(|_| format!(
|
||||
"postgres://plume:plume@localhost/{}",
|
||||
DB_NAME
|
||||
)),
|
||||
#[cfg(feature = "sqlite")]
|
||||
database_url: var("DATABASE_URL").unwrap_or_else(|_| format!(
|
||||
"{}.sqlite",
|
||||
DB_NAME
|
||||
)),
|
||||
search_index: var("SEARCH_INDEX").unwrap_or_else(|_| "search_index".to_owned()),
|
||||
rocket: get_rocket_config()
|
||||
};
|
||||
}
|
||||
@@ -14,7 +14,7 @@ use plume_common::activity_pub::{
|
||||
};
|
||||
use schema::follows;
|
||||
use users::User;
|
||||
use {ap_url, Connection, Error, Result, BASE_URL};
|
||||
use {ap_url, Connection, Error, Result, CONFIG};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
|
||||
#[belongs_to(User, foreign_key = "following_id")]
|
||||
@@ -36,7 +36,7 @@ pub struct NewFollow {
|
||||
impl Follow {
|
||||
insert!(follows, NewFollow, |inserted, conn| {
|
||||
if inserted.ap_url.is_empty() {
|
||||
inserted.ap_url = ap_url(&format!("{}/follows/{}", *BASE_URL, inserted.id));
|
||||
inserted.ap_url = ap_url(&format!("{}/follows/{}", CONFIG.base_url, inserted.id));
|
||||
inserted.save_changes(conn).map_err(Error::from)
|
||||
} else {
|
||||
Ok(inserted)
|
||||
@@ -88,7 +88,7 @@ impl Follow {
|
||||
)?;
|
||||
|
||||
let mut accept = Accept::default();
|
||||
let accept_id = ap_url(&format!("{}/follow/{}/accept", BASE_URL.as_str(), &res.id));
|
||||
let accept_id = ap_url(&format!("{}/follow/{}/accept", CONFIG.base_url.as_str(), &res.id));
|
||||
accept.object_props.set_id_string(accept_id)?;
|
||||
accept.object_props.set_to_link(from.clone().into_id())?;
|
||||
accept.object_props.set_cc_link_vec::<Id>(vec![])?;
|
||||
@@ -204,7 +204,7 @@ mod tests {
|
||||
.expect("Couldn't insert new follow");
|
||||
assert_eq!(
|
||||
follow.ap_url,
|
||||
format!("https://{}/follows/{}", *BASE_URL, follow.id)
|
||||
format!("https://{}/follows/{}", CONFIG.base_url, follow.id)
|
||||
);
|
||||
|
||||
let follow = Follow::insert(
|
||||
|
||||
+5
-30
@@ -33,8 +33,6 @@ extern crate whatlang;
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
use std::env;
|
||||
|
||||
#[cfg(not(any(feature = "sqlite", feature = "postgres")))]
|
||||
compile_error!("Either feature \"sqlite\" or \"postgres\" must be enabled for this crate.");
|
||||
#[cfg(all(feature = "sqlite", feature = "postgres"))]
|
||||
@@ -277,34 +275,11 @@ macro_rules! last {
|
||||
};
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref BASE_URL: String = env::var("BASE_URL").unwrap_or_else(|_| format!(
|
||||
"127.0.0.1:{}",
|
||||
env::var("ROCKET_PORT").unwrap_or_else(|_| String::from("8000"))
|
||||
));
|
||||
pub static ref USE_HTTPS: bool = env::var("USE_HTTPS").map(|val| val == "1").unwrap_or(true);
|
||||
}
|
||||
|
||||
#[cfg(not(test))]
|
||||
static DB_NAME: &str = "plume";
|
||||
#[cfg(test)]
|
||||
static DB_NAME: &str = "plume_tests";
|
||||
|
||||
#[cfg(all(feature = "postgres", not(feature = "sqlite")))]
|
||||
lazy_static! {
|
||||
pub static ref DATABASE_URL: String = env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME));
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "sqlite", not(feature = "postgres")))]
|
||||
lazy_static! {
|
||||
pub static ref DATABASE_URL: String =
|
||||
env::var("DATABASE_URL").unwrap_or_else(|_| format!("{}.sqlite", DB_NAME));
|
||||
}
|
||||
mod config;
|
||||
pub use config::CONFIG;
|
||||
|
||||
pub fn ap_url(url: &str) -> String {
|
||||
let scheme = if *USE_HTTPS { "https" } else { "http" };
|
||||
format!("{}://{}", scheme, url)
|
||||
format!("https://{}", url)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -312,7 +287,7 @@ pub fn ap_url(url: &str) -> String {
|
||||
mod tests {
|
||||
use diesel::{dsl::sql_query, Connection, RunQueryDsl};
|
||||
use Connection as Conn;
|
||||
use DATABASE_URL;
|
||||
use CONFIG;
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
embed_migrations!("../migrations/sqlite");
|
||||
@@ -333,7 +308,7 @@ mod tests {
|
||||
|
||||
pub fn db() -> Conn {
|
||||
let conn =
|
||||
Conn::establish(&*DATABASE_URL.as_str()).expect("Couldn't connect to the database");
|
||||
Conn::establish(CONFIG.database_url.as_str()).expect("Couldn't connect to the database");
|
||||
embedded_migrations::run(&conn).expect("Couldn't run migrations");
|
||||
#[cfg(feature = "sqlite")]
|
||||
sql_query("PRAGMA foreign_keys = on;")
|
||||
|
||||
@@ -31,7 +31,7 @@ use schema::posts;
|
||||
use search::Searcher;
|
||||
use tags::*;
|
||||
use users::User;
|
||||
use {ap_url, ApiResult, Connection, Error, Result, BASE_URL};
|
||||
use {ap_url, ApiResult, Connection, Error, Result, CONFIG};
|
||||
|
||||
pub type LicensedArticle = CustomObject<Licensed, Article>;
|
||||
|
||||
@@ -354,7 +354,7 @@ impl Post {
|
||||
if post.ap_url.is_empty() {
|
||||
post.ap_url = ap_url(&format!(
|
||||
"{}/~/{}/{}/",
|
||||
*BASE_URL,
|
||||
CONFIG.base_url,
|
||||
post.get_blog(conn)?.fqn,
|
||||
post.slug
|
||||
));
|
||||
|
||||
@@ -43,7 +43,7 @@ use posts::Post;
|
||||
use safe_string::SafeString;
|
||||
use schema::users;
|
||||
use search::Searcher;
|
||||
use {ap_url, Connection, Error, Result, BASE_URL, USE_HTTPS};
|
||||
use {ap_url, Connection, Error, Result, CONFIG};
|
||||
|
||||
pub type CustomPerson = CustomObject<ApSignature, Person>;
|
||||
|
||||
@@ -239,7 +239,7 @@ impl User {
|
||||
}
|
||||
|
||||
fn fetch_from_webfinger(conn: &Connection, acct: &str) -> Result<User> {
|
||||
let link = resolve(acct.to_owned(), *USE_HTTPS)?
|
||||
let link = resolve(acct.to_owned(), true)?
|
||||
.links
|
||||
.into_iter()
|
||||
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
||||
@@ -683,7 +683,7 @@ impl User {
|
||||
.set_followers_string(self.followers_endpoint.clone())?;
|
||||
|
||||
let mut endpoints = Endpoint::default();
|
||||
endpoints.set_shared_inbox_string(ap_url(&format!("{}/inbox/", BASE_URL.as_str())))?;
|
||||
endpoints.set_shared_inbox_string(ap_url(&format!("{}/inbox/", CONFIG.base_url.as_str())))?;
|
||||
actor.ap_actor_props.set_endpoints_endpoint(endpoints)?;
|
||||
|
||||
let mut public_key = PublicKey::default();
|
||||
@@ -749,7 +749,7 @@ impl User {
|
||||
User::find_by_ap_url(conn, url).or_else(|_| {
|
||||
// The requested user was not in the DB
|
||||
// We try to fetch it if it is remote
|
||||
if Url::parse(&url)?.host_str()? != BASE_URL.as_str() {
|
||||
if Url::parse(&url)?.host_str()? != CONFIG.base_url.as_str() {
|
||||
User::fetch_from_url(conn, url)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
|
||||
@@ -5,7 +5,7 @@ extern crate diesel_migrations;
|
||||
extern crate plume_models;
|
||||
|
||||
use diesel::Connection;
|
||||
use plume_models::{Connection as Conn, DATABASE_URL};
|
||||
use plume_models::{Connection as Conn, CONFIG};
|
||||
|
||||
#[cfg(feature = "sqlite")]
|
||||
embed_migrations!("../migrations/sqlite");
|
||||
@@ -14,7 +14,7 @@ embed_migrations!("../migrations/sqlite");
|
||||
embed_migrations!("../migrations/postgres");
|
||||
|
||||
fn db() -> Conn {
|
||||
let conn = Conn::establish(&*DATABASE_URL.as_str()).expect("Couldn't connect to the database");
|
||||
let conn = Conn::establish(CONFIG.database_url.as_str()).expect("Couldn't connect to the database");
|
||||
embedded_migrations::run(&conn).expect("Couldn't run migrations");
|
||||
conn
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user