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:
fdb-hiroshima
2019-03-21 10:30:33 +01:00
committed by Baptiste Gelez
parent b945d1f602
commit 65bb50e88f
12 changed files with 117 additions and 89 deletions
+20 -32
View File
@@ -42,12 +42,11 @@ use diesel::r2d2::ConnectionManager;
use plume_models::{
db_conn::{DbPool, PragmaForeignKey},
search::{Searcher as UnmanagedSearcher, SearcherError},
Connection, Error, DATABASE_URL,
Connection, Error, CONFIG,
};
use rocket::{config::Limits, Config, State};
use rocket::State;
use rocket_csrf::CsrfFairingBuilder;
use scheduled_thread_pool::ScheduledThreadPool;
use std::env;
use std::process::exit;
use std::sync::{Arc, Mutex};
use std::time::Duration;
@@ -72,7 +71,7 @@ type Searcher<'a> = State<'a, Arc<UnmanagedSearcher>>;
fn init_pool() -> Option<DbPool> {
dotenv::dotenv().ok();
let manager = ConnectionManager::<Connection>::new(DATABASE_URL.as_str());
let manager = ConnectionManager::<Connection>::new(CONFIG.database_url.as_str());
DbPool::builder()
.connection_customizer(Box::new(PragmaForeignKey))
.build(manager)
@@ -84,15 +83,28 @@ fn main() {
let workpool = ScheduledThreadPool::with_name("worker {}", num_cpus::get());
// we want a fast exit here, so
#[allow(clippy::match_wild_err_arm)]
let searcher = match UnmanagedSearcher::open(&"search_index") {
let searcher = match UnmanagedSearcher::open(&CONFIG.search_index) {
Err(Error::Search(e)) => match e {
SearcherError::WriteLockAcquisitionError => panic!(
r#"Your search index is locked. Plume can't start. To fix this issue
r#"
Your search index is locked. Plume can't start. To fix this issue
make sure no other Plume instance is started, and run:
plm search unlock
Then try to restart Plume.
"#
),
SearcherError::IndexOpeningError => panic!(
r#"
Plume was unable to open the search index. If you created the index
before, make sure to run Plume in the directory it was created, or
to set SEARCH_INDEX accordingly. If you did not create the search
index, run this command:
plm search init
Then try to restart Plume
"#
),
e => Err(e).unwrap(),
@@ -115,38 +127,14 @@ Then try to restart Plume.
})
.expect("Error setting Ctrl-c handler");
let mut config = Config::active().unwrap();
config
.set_address(env::var("ROCKET_ADDRESS").unwrap_or_else(|_| "localhost".to_owned()))
.unwrap();
config.set_port(
env::var("ROCKET_PORT")
.ok()
.map(|s| s.parse::<u16>().unwrap())
.unwrap_or(7878),
);
let _ = env::var("ROCKET_SECRET_KEY").map(|k| config.set_secret_key(k).unwrap());
let form_size = &env::var("FORM_SIZE")
.unwrap_or_else(|_| "32".to_owned())
.parse::<u64>()
.unwrap();
let activity_size = &env::var("ACTIVITY_SIZE")
.unwrap_or_else(|_| "1024".to_owned())
.parse::<u64>()
.unwrap();
config.set_limits(
Limits::new()
.limit("forms", form_size * 1024)
.limit("json", activity_size * 1024),
);
let mail = mail::init();
if mail.is_none() && config.environment.is_prod() {
if mail.is_none() && CONFIG.rocket.as_ref().unwrap().environment.is_prod() {
println!("Warning: the email server is not configured (or not completely).");
println!("Please refer to the documentation to see how to configure it.");
}
rocket::custom(config)
rocket::custom(CONFIG.rocket.clone().unwrap())
.mount(
"/",
routes![
+2 -2
View File
@@ -19,7 +19,7 @@ use mail::{build_mail, Mailer};
use plume_models::{
db_conn::DbConn,
users::{User, AUTH_COOKIE},
Error, BASE_URL,
Error, CONFIG,
};
use routes::errors::ErrorPage;
@@ -175,7 +175,7 @@ pub fn password_reset_request(
creation_date: Instant::now(),
});
let link = format!("https://{}/password-reset/{}", *BASE_URL, id);
let link = format!("https://{}/password-reset/{}", CONFIG.base_url, id);
if let Some(message) = build_mail(
form.email.clone(),
i18n!(intl.catalog, "Password reset"),
+5 -5
View File
@@ -3,7 +3,7 @@ use rocket::response::Content;
use serde_json;
use webfinger::*;
use plume_models::{ap_url, blogs::Blog, db_conn::DbConn, users::User, BASE_URL};
use plume_models::{ap_url, blogs::Blog, db_conn::DbConn, users::User, CONFIG};
#[get("/.well-known/nodeinfo")]
pub fn nodeinfo() -> Content<String> {
@@ -13,11 +13,11 @@ pub fn nodeinfo() -> Content<String> {
"links": [
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
"href": ap_url(&format!("{domain}/nodeinfo/2.0", domain = BASE_URL.as_str()))
"href": ap_url(&format!("{domain}/nodeinfo/2.0", domain = CONFIG.base_url.as_str()))
},
{
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.1",
"href": ap_url(&format!("{domain}/nodeinfo/2.1", domain = BASE_URL.as_str()))
"href": ap_url(&format!("{domain}/nodeinfo/2.1", domain = CONFIG.base_url.as_str()))
}
]
})
@@ -36,7 +36,7 @@ pub fn host_meta() -> String {
"#,
url = ap_url(&format!(
"{domain}/.well-known/webfinger?resource={{uri}}",
domain = BASE_URL.as_str()
domain = CONFIG.base_url.as_str()
))
)
}
@@ -45,7 +45,7 @@ struct WebfingerResolver;
impl Resolver<DbConn> for WebfingerResolver {
fn instance_domain<'a>() -> &'a str {
BASE_URL.as_str()
CONFIG.base_url.as_str()
}
fn find(acct: String, conn: DbConn) -> Result<Webfinger, ResolverError> {