mirror of https://github.com/LemmyNet/lemmy
Merge remote-tracking branch 'origin/main' into 1462-jwt-revocation-on-pwd-change
* origin/main: revert Compose file version from 3.3 to 2.2 Adding more mem limits bump memory limit of iframely Remove extra category_id s . Fixes #1429 Fixing wrong user_ and community icon and banner urls. Remove category from activitypub context Adding a password length check to other API actions. (#1474) Update test script Use URL type in most outstanding struct fields (#1468) Forbid usage of unwrap Upgrade Rust version Rewrite settings implementation. Fixes #1270 (#1433) Rename `lemmy_structs` to `lemmy_api_structs` # Conflicts: # crates/db_schema/src/source/user.rspull/1493/head
commit
7b0a09e84e
@ -1,210 +0,0 @@
|
||||
use crate::location_info;
|
||||
use anyhow::Context;
|
||||
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 tls_enabled: bool,
|
||||
pub jwt_secret: String,
|
||||
pub pictrs_url: String,
|
||||
pub iframely_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 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_queries/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(&Self::get_config_location()).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_DEFAULTS_LOCATION").unwrap_or_else(|_| CONFIG_FILE_DEFAULTS.to_string())
|
||||
}
|
||||
|
||||
pub fn get_config_location() -> String {
|
||||
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string())
|
||||
}
|
||||
|
||||
pub fn read_config_file() -> Result<String, Error> {
|
||||
fs::read_to_string(Self::get_config_location())
|
||||
}
|
||||
|
||||
pub fn get_allowed_instances(&self) -> Vec<String> {
|
||||
let mut allowed_instances: Vec<String> = self
|
||||
.federation
|
||||
.allowed_instances
|
||||
.split(',')
|
||||
.map(|d| d.trim().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.trim().to_string())
|
||||
.collect();
|
||||
|
||||
// The defaults.hjson config always returns a [""]
|
||||
blocked_instances.retain(|d| !d.eq(""));
|
||||
|
||||
blocked_instances
|
||||
}
|
||||
|
||||
/// Returns either "http" or "https", depending on tls_enabled setting
|
||||
pub fn get_protocol_string(&self) -> &'static str {
|
||||
if self.tls_enabled {
|
||||
"https"
|
||||
} else {
|
||||
"http"
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns something like `http://localhost` or `https://lemmy.ml`,
|
||||
/// with the correct protocol and hostname.
|
||||
pub fn get_protocol_and_hostname(&self) -> String {
|
||||
format!("{}://{}", self.get_protocol_string(), self.hostname)
|
||||
}
|
||||
|
||||
/// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
|
||||
/// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
|
||||
/// `lemmy-alpha` instead. It has no effect in production.
|
||||
pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
|
||||
Ok(
|
||||
self
|
||||
.hostname
|
||||
.split(':')
|
||||
.collect::<Vec<&str>>()
|
||||
.first()
|
||||
.context(location_info!())?
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
use crate::settings::{CaptchaConfig, DatabaseConfig, FederationConfig, RateLimitConfig, Settings};
|
||||
use std::net::{IpAddr, Ipv4Addr};
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
database: Some(DatabaseConfig::default()),
|
||||
rate_limit: Some(RateLimitConfig::default()),
|
||||
federation: Some(FederationConfig::default()),
|
||||
captcha: Some(CaptchaConfig::default()),
|
||||
email: None,
|
||||
setup: None,
|
||||
hostname: None,
|
||||
bind: Some(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0))),
|
||||
port: Some(8536),
|
||||
tls_enabled: Some(true),
|
||||
jwt_secret: Some("changeme".into()),
|
||||
pictrs_url: Some("http://pictrs:8080".into()),
|
||||
iframely_url: Some("http://iframely".into()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DatabaseConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
user: "lemmy".into(),
|
||||
password: "password".into(),
|
||||
host: "localhost".into(),
|
||||
port: 5432,
|
||||
database: "lemmy".into(),
|
||||
pool_size: 5,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for CaptchaConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: true,
|
||||
difficulty: "medium".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for FederationConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
allowed_instances: None,
|
||||
blocked_instances: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for RateLimitConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
message: 180,
|
||||
message_per_second: 60,
|
||||
post: 6,
|
||||
post_per_second: 600,
|
||||
register: 3,
|
||||
register_per_second: 3600,
|
||||
image: 6,
|
||||
image_per_second: 3600,
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
use crate::{
|
||||
location_info,
|
||||
settings::structs::{
|
||||
CaptchaConfig,
|
||||
DatabaseConfig,
|
||||
EmailConfig,
|
||||
FederationConfig,
|
||||
RateLimitConfig,
|
||||
Settings,
|
||||
SetupConfig,
|
||||
},
|
||||
LemmyError,
|
||||
};
|
||||
use anyhow::{anyhow, Context};
|
||||
use deser_hjson::from_str;
|
||||
use merge::Merge;
|
||||
use std::{env, fs, io::Error, net::IpAddr, sync::RwLock};
|
||||
|
||||
pub(crate) mod defaults;
|
||||
pub mod structs;
|
||||
|
||||
static CONFIG_FILE: &str = "config/config.hjson";
|
||||
|
||||
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_queries/src/lib.rs::get_database_url_from_env()`
|
||||
fn init() -> Result<Self, LemmyError> {
|
||||
// Read the config file
|
||||
let mut custom_config = from_str::<Settings>(&Self::read_config_file()?)?;
|
||||
|
||||
// Merge with env vars
|
||||
custom_config.merge(envy::prefixed("LEMMY_").from_env::<Settings>()?);
|
||||
|
||||
// Merge with default
|
||||
custom_config.merge(Settings::default());
|
||||
|
||||
if custom_config.hostname == Settings::default().hostname {
|
||||
return Err(anyhow!("Hostname variable is not set!").into());
|
||||
}
|
||||
|
||||
Ok(custom_config)
|
||||
}
|
||||
|
||||
/// Returns the config as a struct.
|
||||
pub fn get() -> Self {
|
||||
SETTINGS.read().expect("read config").to_owned()
|
||||
}
|
||||
|
||||
pub fn get_database_url(&self) -> String {
|
||||
let conf = self.database();
|
||||
format!(
|
||||
"postgres://{}:{}@{}:{}/{}",
|
||||
conf.user, conf.password, conf.host, conf.port, conf.database,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn get_config_location() -> String {
|
||||
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| CONFIG_FILE.to_string())
|
||||
}
|
||||
|
||||
pub fn read_config_file() -> Result<String, Error> {
|
||||
fs::read_to_string(Self::get_config_location())
|
||||
}
|
||||
|
||||
pub fn get_allowed_instances(&self) -> Option<Vec<String>> {
|
||||
self.federation().allowed_instances
|
||||
}
|
||||
|
||||
pub fn get_blocked_instances(&self) -> Option<Vec<String>> {
|
||||
self.federation().blocked_instances
|
||||
}
|
||||
|
||||
/// Returns either "http" or "https", depending on tls_enabled setting
|
||||
pub fn get_protocol_string(&self) -> &'static str {
|
||||
if let Some(tls_enabled) = self.tls_enabled {
|
||||
if tls_enabled {
|
||||
"https"
|
||||
} else {
|
||||
"http"
|
||||
}
|
||||
} else {
|
||||
"http"
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns something like `http://localhost` or `https://lemmy.ml`,
|
||||
/// with the correct protocol and hostname.
|
||||
pub fn get_protocol_and_hostname(&self) -> String {
|
||||
format!("{}://{}", self.get_protocol_string(), self.hostname())
|
||||
}
|
||||
|
||||
/// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
|
||||
/// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
|
||||
/// `lemmy-alpha` instead. It has no effect in production.
|
||||
pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
|
||||
Ok(
|
||||
self
|
||||
.hostname()
|
||||
.split(':')
|
||||
.collect::<Vec<&str>>()
|
||||
.first()
|
||||
.context(location_info!())?
|
||||
.to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn save_config_file(data: &str) -> Result<String, LemmyError> {
|
||||
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().expect("write config");
|
||||
*new_settings = match Settings::init() {
|
||||
Ok(c) => c,
|
||||
Err(e) => panic!("{}", e),
|
||||
};
|
||||
|
||||
Ok(Self::read_config_file()?)
|
||||
}
|
||||
|
||||
pub fn database(&self) -> DatabaseConfig {
|
||||
self.database.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn hostname(&self) -> String {
|
||||
self.hostname.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn bind(&self) -> IpAddr {
|
||||
self.bind.expect("return bind address")
|
||||
}
|
||||
pub fn port(&self) -> u16 {
|
||||
self.port.unwrap_or_default()
|
||||
}
|
||||
pub fn tls_enabled(&self) -> bool {
|
||||
self.tls_enabled.unwrap_or_default()
|
||||
}
|
||||
pub fn jwt_secret(&self) -> String {
|
||||
self.jwt_secret.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn pictrs_url(&self) -> String {
|
||||
self.pictrs_url.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn iframely_url(&self) -> String {
|
||||
self.iframely_url.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn rate_limit(&self) -> RateLimitConfig {
|
||||
self.rate_limit.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn federation(&self) -> FederationConfig {
|
||||
self.federation.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn captcha(&self) -> CaptchaConfig {
|
||||
self.captcha.to_owned().unwrap_or_default()
|
||||
}
|
||||
pub fn email(&self) -> Option<EmailConfig> {
|
||||
self.email.to_owned()
|
||||
}
|
||||
pub fn setup(&self) -> Option<SetupConfig> {
|
||||
self.setup.to_owned()
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
use merge::Merge;
|
||||
use serde::Deserialize;
|
||||
use std::net::IpAddr;
|
||||
|
||||
#[derive(Debug, Deserialize, Clone, Merge)]
|
||||
pub struct Settings {
|
||||
pub(crate) database: Option<DatabaseConfig>,
|
||||
pub(crate) rate_limit: Option<RateLimitConfig>,
|
||||
pub(crate) federation: Option<FederationConfig>,
|
||||
pub(crate) hostname: Option<String>,
|
||||
pub(crate) bind: Option<IpAddr>,
|
||||
pub(crate) port: Option<u16>,
|
||||
pub(crate) tls_enabled: Option<bool>,
|
||||
pub(crate) jwt_secret: Option<String>,
|
||||
pub(crate) pictrs_url: Option<String>,
|
||||
pub(crate) iframely_url: Option<String>,
|
||||
pub(crate) captcha: Option<CaptchaConfig>,
|
||||
pub(crate) email: Option<EmailConfig>,
|
||||
pub(crate) setup: Option<SetupConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Clone)]
|
||||
pub struct CaptchaConfig {
|
||||
pub enabled: bool,
|
||||
pub difficulty: String,
|
||||
}
|
||||
|
||||
#[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 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 FederationConfig {
|
||||
pub enabled: bool,
|
||||
pub allowed_instances: Option<Vec<String>>,
|
||||
pub blocked_instances: Option<Vec<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 SetupConfig {
|
||||
pub admin_username: String,
|
||||
pub admin_password: String,
|
||||
pub admin_email: Option<String>,
|
||||
pub site_name: String,
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue