]> Untitled Git - lemmy.git/blob - server/src/settings.rs
Implement config (fixes #351)
[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_COSTUMIZED: &str = "config/custom.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 }
20
21 #[derive(Debug, Deserialize)]
22 pub struct RateLimitConfig {
23   pub message: i32,
24   pub message_per_second: i32,
25   pub post: i32,
26   pub post_per_second: i32,
27   pub register: i32,
28   pub register_per_second: i32,
29 }
30
31 #[derive(Debug, Deserialize)]
32 pub struct EmailConfig {
33   pub smtp_server: String,
34   pub smtp_login: String,
35   pub smtp_password: String,
36   pub smtp_from_address: String,
37 }
38
39 #[derive(Debug, Deserialize)]
40 pub struct Database {
41   pub user: String,
42   pub password: String,
43   pub host: String,
44   pub port: i32,
45   pub database: String,
46 }
47
48 lazy_static! {
49   static ref SETTINGS: Settings = {
50     return match Settings::init() {
51       Ok(c) => c,
52       Err(e) => panic!("{}", e),
53     };
54   };
55 }
56
57 impl Settings {
58   
59   /// Reads config from the files and environment.
60   /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
61   /// from CONFIG_FILE_COSTUMIZED (optional). Finally, values from the environment
62   /// (with prefix LEMMY) are added to the config.
63   fn init() -> Result<Self, ConfigError> {
64     let mut s = Config::new();
65
66     // Start off by merging in the "default" configuration file
67     s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
68
69     // TODO: we could also automatically load dev/prod configs based on environment
70     // https://github.com/mehcode/config-rs/blob/master/examples/hierarchical-env/src/settings.rs#L49
71     s.merge(File::with_name(CONFIG_FILE_COSTUMIZED).required(false))?;
72
73     // Add in settings from the environment (with a prefix of LEMMY)
74     // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
75     s.merge(Environment::with_prefix("LEMMY"))?;
76
77     return s.try_into();
78   }
79
80   /// Returns the config as a struct.
81   pub fn get() -> &'static Self {
82     &SETTINGS
83   }
84
85   /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
86   /// otherwise the connection url is generated from the config.
87   pub fn get_database_url(&self) -> String {
88     match env::var("LEMMY_DATABASE_URL") {
89       Ok(url) => url,
90       Err(_) => format!(
91         "postgres://{}:{}@{}:{}/{}",
92         self.database.user,
93         self.database.password,
94         self.database.host,
95         self.database.port,
96         self.database.database
97       ),
98     }
99   }
100
101   pub fn api_endpoint(&self) -> String {
102     format!("{}/api/v1", self.hostname)
103   }
104 }