2019-12-15 16:40:55 +00:00
|
|
|
extern crate lazy_static;
|
|
|
|
use config::{Config, ConfigError, Environment, File};
|
|
|
|
use serde::Deserialize;
|
|
|
|
use std::env;
|
|
|
|
use std::net::IpAddr;
|
|
|
|
|
|
|
|
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
|
2019-12-17 21:35:48 +00:00
|
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
2019-12-15 16:40:55 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct Settings {
|
|
|
|
pub database: Database,
|
|
|
|
pub hostname: String,
|
|
|
|
pub bind: IpAddr,
|
|
|
|
pub port: u16,
|
|
|
|
pub jwt_secret: String,
|
2019-12-28 21:06:37 +00:00
|
|
|
pub front_end_dir: String,
|
2019-12-15 16:40:55 +00:00
|
|
|
pub rate_limit: RateLimitConfig,
|
|
|
|
pub email: Option<EmailConfig>,
|
2019-12-19 00:29:56 +00:00
|
|
|
pub federation_enabled: bool,
|
2019-12-27 17:25:07 +00:00
|
|
|
pub federated_instance: Option<String>,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct RateLimitConfig {
|
|
|
|
pub message: i32,
|
|
|
|
pub message_per_second: i32,
|
|
|
|
pub post: i32,
|
|
|
|
pub post_per_second: i32,
|
|
|
|
pub register: i32,
|
|
|
|
pub register_per_second: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct EmailConfig {
|
|
|
|
pub smtp_server: String,
|
2020-01-31 11:03:26 +00:00
|
|
|
pub smtp_login: Option<String>,
|
|
|
|
pub smtp_password: Option<String>,
|
2019-12-15 16:40:55 +00:00
|
|
|
pub smtp_from_address: String,
|
2020-01-31 11:03:26 +00:00
|
|
|
pub use_tls: bool,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct Database {
|
|
|
|
pub user: String,
|
|
|
|
pub password: String,
|
|
|
|
pub host: String,
|
|
|
|
pub port: i32,
|
|
|
|
pub database: String,
|
2019-12-16 12:25:47 +00:00
|
|
|
pub pool_size: u32,
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
static ref SETTINGS: Settings = {
|
2020-01-02 11:30:00 +00:00
|
|
|
match Settings::init() {
|
2019-12-15 16:40:55 +00:00
|
|
|
Ok(c) => c,
|
|
|
|
Err(e) => panic!("{}", e),
|
2020-01-02 11:30:00 +00:00
|
|
|
}
|
2019-12-15 16:40:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Settings {
|
|
|
|
/// Reads config from the files and environment.
|
|
|
|
/// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
|
2019-12-17 21:35:48 +00:00
|
|
|
/// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
|
|
|
|
/// added to the config.
|
2019-12-15 16:40:55 +00:00
|
|
|
fn init() -> Result<Self, ConfigError> {
|
|
|
|
let mut s = Config::new();
|
|
|
|
|
|
|
|
s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
|
|
|
|
|
2019-12-17 21:35:48 +00:00
|
|
|
s.merge(File::with_name(CONFIG_FILE).required(false))?;
|
2019-12-15 16:40:55 +00:00
|
|
|
|
|
|
|
// Add in settings from the environment (with a prefix of LEMMY)
|
|
|
|
// Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
|
2019-12-28 11:11:06 +00:00
|
|
|
// Note: we need to use double underscore here, because otherwise variables containing
|
|
|
|
// underscore cant be set from environmnet.
|
|
|
|
// https://github.com/mehcode/config-rs/issues/73
|
|
|
|
s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
|
2019-12-15 16:40:55 +00:00
|
|
|
|
2020-01-02 11:30:00 +00:00
|
|
|
s.try_into()
|
2019-12-15 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the config as a struct.
|
|
|
|
pub fn get() -> &'static Self {
|
|
|
|
&SETTINGS
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
|
|
|
|
/// otherwise the connection url is generated from the config.
|
|
|
|
pub fn get_database_url(&self) -> String {
|
|
|
|
match env::var("LEMMY_DATABASE_URL") {
|
|
|
|
Ok(url) => url,
|
|
|
|
Err(_) => format!(
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
|
|
|
self.database.user,
|
|
|
|
self.database.password,
|
|
|
|
self.database.host,
|
|
|
|
self.database.port,
|
|
|
|
self.database.database
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn api_endpoint(&self) -> String {
|
|
|
|
format!("{}/api/v1", self.hostname)
|
|
|
|
}
|
|
|
|
}
|