]> Untitled Git - lemmy.git/blob - server/src/settings.rs
Merge branch 'dev' into 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 front_end_dir: String,
18   pub rate_limit: RateLimitConfig,
19   pub email: Option<EmailConfig>,
20   pub federation_enabled: bool,
21   pub federated_instance: Option<String>,
22 }
23
24 #[derive(Debug, Deserialize)]
25 pub struct RateLimitConfig {
26   pub message: i32,
27   pub message_per_second: i32,
28   pub post: i32,
29   pub post_per_second: i32,
30   pub register: i32,
31   pub register_per_second: i32,
32 }
33
34 #[derive(Debug, Deserialize)]
35 pub struct EmailConfig {
36   pub smtp_server: String,
37   pub smtp_login: Option<String>,
38   pub smtp_password: Option<String>,
39   pub smtp_from_address: String,
40   pub use_tls: bool,
41 }
42
43 #[derive(Debug, Deserialize)]
44 pub struct Database {
45   pub user: String,
46   pub password: String,
47   pub host: String,
48   pub port: i32,
49   pub database: String,
50   pub pool_size: u32,
51 }
52
53 lazy_static! {
54   static ref SETTINGS: Settings = {
55     match Settings::init() {
56       Ok(c) => c,
57       Err(e) => panic!("{}", e),
58     }
59   };
60 }
61
62 impl Settings {
63   /// Reads config from the files and environment.
64   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
65   /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
66   /// added to the config.
67   fn init() -> Result<Self, ConfigError> {
68     let mut s = Config::new();
69
70     s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
71
72     s.merge(File::with_name(CONFIG_FILE).required(false))?;
73
74     // Add in settings from the environment (with a prefix of LEMMY)
75     // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
76     // Note: we need to use double underscore here, because otherwise variables containing
77     //       underscore cant be set from environmnet.
78     // https://github.com/mehcode/config-rs/issues/73
79     s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
80
81     s.try_into()
82   }
83
84   /// Returns the config as a struct.
85   pub fn get() -> &'static Self {
86     &SETTINGS
87   }
88
89   /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
90   /// otherwise the connection url is generated from the config.
91   pub fn get_database_url(&self) -> String {
92     match env::var("LEMMY_DATABASE_URL") {
93       Ok(url) => url,
94       Err(_) => format!(
95         "postgres://{}:{}@{}:{}/{}",
96         self.database.user,
97         self.database.password,
98         self.database.host,
99         self.database.port,
100         self.database.database
101       ),
102     }
103   }
104
105   pub fn api_endpoint(&self) -> String {
106     format!("{}/api/v1", self.hostname)
107   }
108 }