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