]> Untitled Git - lemmy.git/blob - crates/utils/src/settings/mod.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / utils / src / settings / mod.rs
1 use crate::{
2   error::LemmyError,
3   location_info,
4   settings::structs::{PictrsConfig, Settings},
5 };
6 use anyhow::{anyhow, Context};
7 use deser_hjson::from_str;
8 use once_cell::sync::Lazy;
9 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
10 use regex::Regex;
11 use std::{env, fs, io::Error};
12
13 pub mod structs;
14
15 use structs::DatabaseConnection;
16
17 static DEFAULT_CONFIG_FILE: &str = "config/config.hjson";
18
19 pub static SETTINGS: Lazy<Settings> = Lazy::new(|| {
20   Settings::init().expect("Failed to load settings file, see documentation (https://join-lemmy.org/docs/en/administration/configuration.html)")
21 });
22 static WEBFINGER_REGEX: Lazy<Regex> = Lazy::new(|| {
23   Regex::new(&format!(
24     "^acct:([a-zA-Z0-9_]{{3,}})@{}$",
25     SETTINGS.hostname
26   ))
27   .expect("compile webfinger regex")
28 });
29
30 impl Settings {
31   /// Reads config from configuration file.
32   ///
33   /// Note: The env var `LEMMY_DATABASE_URL` is parsed in
34   /// `lemmy_db_schema/src/lib.rs::get_database_url_from_env()`
35   /// Warning: Only call this once.
36   pub(crate) fn init() -> Result<Self, LemmyError> {
37     // Read the config file
38     let config = from_str::<Settings>(&Self::read_config_file()?)?;
39
40     if config.hostname == "unset" {
41       return Err(anyhow!("Hostname variable is not set!").into());
42     }
43
44     Ok(config)
45   }
46
47   pub fn get_database_url(&self) -> String {
48     match &self.database.connection {
49       DatabaseConnection::Uri { uri } => uri.clone(),
50       DatabaseConnection::Parts(parts) => {
51         format!(
52           "postgres://{}:{}@{}:{}/{}",
53           utf8_percent_encode(&parts.user, NON_ALPHANUMERIC),
54           utf8_percent_encode(&parts.password, NON_ALPHANUMERIC),
55           parts.host,
56           parts.port,
57           utf8_percent_encode(&parts.database, NON_ALPHANUMERIC),
58         )
59       }
60     }
61   }
62
63   fn get_config_location() -> String {
64     env::var("LEMMY_CONFIG_LOCATION").unwrap_or_else(|_| DEFAULT_CONFIG_FILE.to_string())
65   }
66
67   fn read_config_file() -> Result<String, Error> {
68     fs::read_to_string(Self::get_config_location())
69   }
70
71   /// Returns either "http" or "https", depending on tls_enabled setting
72   pub fn get_protocol_string(&self) -> &'static str {
73     if self.tls_enabled {
74       "https"
75     } else {
76       "http"
77     }
78   }
79
80   /// Returns something like `http://localhost` or `https://lemmy.ml`,
81   /// with the correct protocol and hostname.
82   pub fn get_protocol_and_hostname(&self) -> String {
83     format!("{}://{}", self.get_protocol_string(), self.hostname)
84   }
85
86   /// When running the federation test setup in `api_tests/` or `docker/federation`, the `hostname`
87   /// variable will be like `lemmy-alpha:8541`. This method removes the port and returns
88   /// `lemmy-alpha` instead. It has no effect in production.
89   pub fn get_hostname_without_port(&self) -> Result<String, anyhow::Error> {
90     Ok(
91       (*self
92         .hostname
93         .split(':')
94         .collect::<Vec<&str>>()
95         .first()
96         .context(location_info!())?)
97       .to_string(),
98     )
99   }
100
101   pub fn webfinger_regex(&self) -> Regex {
102     WEBFINGER_REGEX.clone()
103   }
104
105   pub fn pictrs_config(&self) -> Result<PictrsConfig, LemmyError> {
106     self
107       .pictrs
108       .clone()
109       .ok_or_else(|| anyhow!("images_disabled").into())
110   }
111 }