mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-11-27 06:41:18 +00:00
5c6258390c
* Adding a way to GetComments for a community given its name only. * Adding getcomments to api docs. * A first pass at locally working isomorphic integration. * Testing out cargo-husky. * Testing a fail hook. * Revert "Testing a fail hook." This reverts commit0941cf1736
. * Moving server to top level, now that UI is gone. * Running cargo fmt using old way. * Adding nginx, fixing up docker-compose files, fixing docs. * Trying to re-add API tests. * Fixing prod dockerfile. * Redoing nightly fmt * Trying to fix private message api test. * Adding CommunityJoin, PostJoin instead of joins from GetComments, etc. - Fixes #1122 * Fixing fmt. * Fixing up docs. * Removing translations. * Adding apps / clients to readme. * Fixing main image. * Using new lemmy-isomorphic-ui with better javascript disabled. * Try to fix image uploads in federation test * Revert "Try to fix image uploads in federation test" This reverts commita2ddf2a90b
. * Fix post url federation * Adding some more tests, some still broken. * Don't need gitattributes anymore. * Update local federation test setup * Fixing tests. * Fixing travis build. * Fixing travis build, again. * Changing lemmy-isomorphic-ui to lemmy-ui * Error in travis build again. Co-authored-by: Felix Ableitner <me@nutomic.com>
173 lines
4.6 KiB
Rust
173 lines
4.6 KiB
Rust
use config::{Config, ConfigError, Environment, File};
|
|
use serde::Deserialize;
|
|
use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
|
|
|
|
static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
|
|
static CONFIG_FILE: &str = "config/config.hjson";
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct Settings {
|
|
pub setup: Option<Setup>,
|
|
pub database: DatabaseConfig,
|
|
pub hostname: String,
|
|
pub bind: IpAddr,
|
|
pub port: u16,
|
|
pub jwt_secret: String,
|
|
pub pictrs_url: String,
|
|
pub rate_limit: RateLimitConfig,
|
|
pub email: Option<EmailConfig>,
|
|
pub federation: FederationConfig,
|
|
pub captcha: CaptchaConfig,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct Setup {
|
|
pub admin_username: String,
|
|
pub admin_password: String,
|
|
pub admin_email: Option<String>,
|
|
pub site_name: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
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,
|
|
pub image: i32,
|
|
pub image_per_second: i32,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct EmailConfig {
|
|
pub smtp_server: String,
|
|
pub smtp_login: Option<String>,
|
|
pub smtp_password: Option<String>,
|
|
pub smtp_from_address: String,
|
|
pub use_tls: bool,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct CaptchaConfig {
|
|
pub enabled: bool,
|
|
pub difficulty: String, // easy, medium, or hard
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct DatabaseConfig {
|
|
pub user: String,
|
|
pub password: String,
|
|
pub host: String,
|
|
pub port: i32,
|
|
pub database: String,
|
|
pub pool_size: u32,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
pub struct FederationConfig {
|
|
pub enabled: bool,
|
|
pub tls_enabled: bool,
|
|
pub allowed_instances: String,
|
|
pub blocked_instances: String,
|
|
}
|
|
|
|
lazy_static! {
|
|
static ref SETTINGS: RwLock<Settings> = RwLock::new(match Settings::init() {
|
|
Ok(c) => c,
|
|
Err(e) => panic!("{}", e),
|
|
});
|
|
}
|
|
|
|
impl Settings {
|
|
/// Reads config from the files and environment.
|
|
/// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
|
|
/// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
|
|
/// added to the config.
|
|
///
|
|
/// Note: The env var `LEMMY_DATABASE_URL` is parsed in
|
|
/// `lemmy_db/src/lib.rs::get_database_url_from_env()`
|
|
fn init() -> Result<Self, ConfigError> {
|
|
let mut s = Config::new();
|
|
|
|
s.merge(File::with_name(&Self::get_config_defaults_location()))?;
|
|
|
|
s.merge(File::with_name(CONFIG_FILE).required(false))?;
|
|
|
|
// Add in settings from the environment (with a prefix of LEMMY)
|
|
// Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
|
|
// 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("__"))?;
|
|
|
|
s.try_into()
|
|
}
|
|
|
|
/// Returns the config as a struct.
|
|
pub fn get() -> Self {
|
|
SETTINGS.read().unwrap().to_owned()
|
|
}
|
|
|
|
pub fn get_database_url(&self) -> String {
|
|
format!(
|
|
"postgres://{}:{}@{}:{}/{}",
|
|
self.database.user,
|
|
self.database.password,
|
|
self.database.host,
|
|
self.database.port,
|
|
self.database.database
|
|
)
|
|
}
|
|
|
|
pub fn get_config_defaults_location() -> String {
|
|
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string())
|
|
}
|
|
|
|
pub fn read_config_file() -> Result<String, Error> {
|
|
fs::read_to_string(CONFIG_FILE)
|
|
}
|
|
|
|
pub fn get_allowed_instances(&self) -> Vec<String> {
|
|
let mut allowed_instances: Vec<String> = self
|
|
.federation
|
|
.allowed_instances
|
|
.split(',')
|
|
.map(|d| d.to_string())
|
|
.collect();
|
|
|
|
// The defaults.hjson config always returns a [""]
|
|
allowed_instances.retain(|d| !d.eq(""));
|
|
|
|
allowed_instances
|
|
}
|
|
|
|
pub fn get_blocked_instances(&self) -> Vec<String> {
|
|
let mut blocked_instances: Vec<String> = self
|
|
.federation
|
|
.blocked_instances
|
|
.split(',')
|
|
.map(|d| d.to_string())
|
|
.collect();
|
|
|
|
// The defaults.hjson config always returns a [""]
|
|
blocked_instances.retain(|d| !d.eq(""));
|
|
|
|
blocked_instances
|
|
}
|
|
|
|
pub fn save_config_file(data: &str) -> Result<String, Error> {
|
|
fs::write(CONFIG_FILE, data)?;
|
|
|
|
// Reload the new settings
|
|
// From https://stackoverflow.com/questions/29654927/how-do-i-assign-a-string-to-a-mutable-static-variable/47181804#47181804
|
|
let mut new_settings = SETTINGS.write().unwrap();
|
|
*new_settings = match Settings::init() {
|
|
Ok(c) => c,
|
|
Err(e) => panic!("{}", e),
|
|
};
|
|
|
|
Self::read_config_file()
|
|
}
|
|
}
|