2024-04-10 14:14:11 +00:00
|
|
|
use crate::{error::LemmyResult, location_info};
|
2021-03-01 17:24:11 +00:00
|
|
|
use anyhow::{anyhow, Context};
|
|
|
|
use deser_hjson::from_str;
|
2021-11-22 18:58:31 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2022-10-27 09:24:07 +00:00
|
|
|
use regex::Regex;
|
2022-06-22 20:24:54 +00:00
|
|
|
use std::{env, fs, io::Error};
|
2024-01-25 14:22:11 +00:00
|
|
|
use urlencoding::encode;
|
2021-03-01 17:24:11 +00:00
|
|
|
|
|
|
|
pub mod structs;
|
|
|
|
|
2024-02-25 00:54:27 +00:00
|
|
|
use structs::{DatabaseConnection, PictrsConfig, PictrsImageMode, Settings};
|
2023-06-09 12:18:22 +00:00
|
|
|
|
2021-09-28 15:34:18 +00:00
|
|
|
static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
|
2021-03-01 17:24:11 +00:00
|
|
|
|
2023-05-12 00:12:12 +00:00
|
|
|
pub static SETTINGS: Lazy<Settings> = Lazy::new(|| {
|
2024-04-08 10:05:54 +00:00
|
|
|
if env::var("LEMMY_INITIALIZE_WITH_DEFAULT_SETTINGS").is_ok() {
|
|
|
|
println!(
|
|
|
|
"LEMMY_INITIALIZE_WITH_DEFAULT_SETTINGS was set, any configuration file has been ignored."
|
|
|
|
);
|
|
|
|
println!("Use with other environment variables to configure this instance further; e.g. LEMMY_DATABASE_URL.");
|
|
|
|
Settings::default()
|
|
|
|
} else {
|
|
|
|
Settings::init().expect("Failed to load settings file, see documentation (https://join-lemmy.org/docs/en/administration/configuration.html).")
|
|
|
|
}
|
2023-05-12 00:12:12 +00:00
|
|
|
});
|
2024-04-08 10:05:54 +00:00
|
|
|
|
2021-11-22 18:58:31 +00:00
|
|
|
static WEBFINGER_REGEX: Lazy<Regex> = Lazy::new(|| {
|
|
|
|
Regex::new(&format!(
|
2021-12-20 22:23:06 +00:00
|
|
|
"^acct:([a-zA-Z0-9_]{{3,}})@{}$",
|
2022-06-22 20:24:54 +00:00
|
|
|
SETTINGS.hostname
|
2021-10-08 15:07:24 +00:00
|
|
|
))
|
2021-11-22 18:58:31 +00:00
|
|
|
.expect("compile webfinger regex")
|
|
|
|
});
|
2021-03-01 17:24:11 +00:00
|
|
|
|
|
|
|
impl Settings {
|
2021-05-26 13:39:38 +00:00
|
|
|
/// Reads config from configuration file.
|
2021-03-01 17:24:11 +00:00
|
|
|
///
|
|
|
|
/// Note: The env var `LEMMY_DATABASE_URL` is parsed in
|
2021-10-16 13:33:38 +00:00
|
|
|
/// `lemmy_db_schema/src/lib.rs::get_database_url_from_env()`
|
2021-09-22 15:57:09 +00:00
|
|
|
/// Warning: Only call this once.
|
2024-04-10 14:14:11 +00:00
|
|
|
pub(crate) fn init() -> LemmyResult<Self> {
|
2021-10-08 15:07:24 +00:00
|
|
|
let config = from_str::<Settings>(&Self::read_config_file()?)?;
|
2021-08-04 21:13:51 +00:00
|
|
|
if config.hostname == "unset" {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(anyhow!("Hostname variable is not set!").into())
|
|
|
|
} else {
|
|
|
|
Ok(config)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_database_url(&self) -> String {
|
2023-10-25 15:34:38 +00:00
|
|
|
if let Ok(url) = env::var("LEMMY_DATABASE_URL") {
|
|
|
|
return url;
|
|
|
|
}
|
2023-06-09 12:18:22 +00:00
|
|
|
match &self.database.connection {
|
|
|
|
DatabaseConnection::Uri { uri } => uri.clone(),
|
|
|
|
DatabaseConnection::Parts(parts) => {
|
|
|
|
format!(
|
|
|
|
"postgres://{}:{}@{}:{}/{}",
|
2024-01-25 14:22:11 +00:00
|
|
|
encode(&parts.user),
|
|
|
|
encode(&parts.password),
|
2023-06-09 12:18:22 +00:00
|
|
|
parts.host,
|
|
|
|
parts.port,
|
2024-01-25 14:22:11 +00:00
|
|
|
encode(&parts.database),
|
2023-06-09 12:18:22 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 00:12:12 +00:00
|
|
|
fn get_config_location() -> String {
|
2021-09-28 15:34:18 +00:00
|
|
|
env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string())
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
2023-05-12 00:12:12 +00:00
|
|
|
fn read_config_file() -> Result<String, Error> {
|
2021-03-01 17:24:11 +00:00
|
|
|
fs::read_to_string(Self::get_config_location())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns either "http" or "https", depending on tls_enabled setting
|
|
|
|
pub fn get_protocol_string(&self) -> &'static str {
|
2021-08-04 21:13:51 +00:00
|
|
|
if self.tls_enabled {
|
|
|
|
"https"
|
2021-03-01 17:24:11 +00:00
|
|
|
} 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 {
|
2021-08-04 21:13:51 +00:00
|
|
|
format!("{}://{}", self.get_protocol_string(), self.hostname)
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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(
|
2022-11-19 04:33:54 +00:00
|
|
|
(*self
|
2021-08-04 21:13:51 +00:00
|
|
|
.hostname
|
2021-03-01 17:24:11 +00:00
|
|
|
.split(':')
|
|
|
|
.collect::<Vec<&str>>()
|
|
|
|
.first()
|
2022-11-19 04:33:54 +00:00
|
|
|
.context(location_info!())?)
|
|
|
|
.to_string(),
|
2021-03-01 17:24:11 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:03:09 +00:00
|
|
|
pub fn webfinger_regex(&self) -> Regex {
|
2022-11-19 04:33:54 +00:00
|
|
|
WEBFINGER_REGEX.clone()
|
2021-09-22 15:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 14:14:11 +00:00
|
|
|
pub fn pictrs_config(&self) -> LemmyResult<PictrsConfig> {
|
2022-06-13 19:15:04 +00:00
|
|
|
self
|
2022-07-14 18:25:10 +00:00
|
|
|
.pictrs
|
2022-11-19 04:33:54 +00:00
|
|
|
.clone()
|
2022-06-13 19:15:04 +00:00
|
|
|
.ok_or_else(|| anyhow!("images_disabled").into())
|
|
|
|
}
|
2021-03-01 17:24:11 +00:00
|
|
|
}
|
2024-01-25 14:22:11 +00:00
|
|
|
|
|
|
|
impl PictrsConfig {
|
|
|
|
pub fn image_mode(&self) -> PictrsImageMode {
|
|
|
|
if let Some(cache_external_link_previews) = self.cache_external_link_previews {
|
|
|
|
if cache_external_link_previews {
|
|
|
|
PictrsImageMode::StoreLinkPreviews
|
|
|
|
} else {
|
|
|
|
PictrsImageMode::None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.image_mode.clone()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|