]> Untitled Git - lemmy.git/blob - server/src/settings.rs
Added option to enable/disable federation
[lemmy.git] / server / src / settings.rs
1 extern crate lazy_static;
2 use config::{Config, ConfigError, Environment, File};
3 use serde::Deserialize;
4 use std::env;
5 use std::net::IpAddr;
6
7 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
8 static CONFIG_FILE: &str = "config/config.hjson";
9
10 #[derive(Debug, Deserialize)]
11 pub struct Settings {
12   pub database: Database,
13   pub hostname: String,
14   pub bind: IpAddr,
15   pub port: u16,
16   pub jwt_secret: String,
17   pub rate_limit: RateLimitConfig,
18   pub email: Option<EmailConfig>,
19   pub federation_enabled: bool,
20 }
21
22 #[derive(Debug, Deserialize)]
23 pub struct RateLimitConfig {
24   pub message: i32,
25   pub message_per_second: i32,
26   pub post: i32,
27   pub post_per_second: i32,
28   pub register: i32,
29   pub register_per_second: i32,
30 }
31
32 #[derive(Debug, Deserialize)]
33 pub struct EmailConfig {
34   pub smtp_server: String,
35   pub smtp_login: String,
36   pub smtp_password: String,
37   pub smtp_from_address: String,
38 }
39
40 #[derive(Debug, Deserialize)]
41 pub struct Database {
42   pub user: String,
43   pub password: String,
44   pub host: String,
45   pub port: i32,
46   pub database: String,
47   pub pool_size: u32,
48 }
49
50 lazy_static! {
51   static ref SETTINGS: Settings = {
52     return match Settings::init() {
53       Ok(c) => c,
54       Err(e) => panic!("{}", e),
55     };
56   };
57 }
58
59 impl Settings {
60   /// Reads config from the files and environment.
61   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
62   /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
63   /// added to the config.
64   fn init() -> Result<Self, ConfigError> {
65     let mut s = Config::new();
66
67     s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
68
69     s.merge(File::with_name(CONFIG_FILE).required(false))?;
70
71     // Add in settings from the environment (with a prefix of LEMMY)
72     // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
73     s.merge(Environment::with_prefix("LEMMY").separator("_"))?;
74
75     return s.try_into();
76   }
77
78   /// Returns the config as a struct.
79   pub fn get() -> &'static Self {
80     &SETTINGS
81   }
82
83   /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
84   /// otherwise the connection url is generated from the config.
85   pub fn get_database_url(&self) -> String {
86     match env::var("LEMMY_DATABASE_URL") {
87       Ok(url) => url,
88       Err(_) => format!(
89         "postgres://{}:{}@{}:{}/{}",
90         self.database.user,
91         self.database.password,
92         self.database.host,
93         self.database.port,
94         self.database.database
95       ),
96     }
97   }
98
99   pub fn api_endpoint(&self) -> String {
100     format!("{}/api/v1", self.hostname)
101   }
102 }