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