]> Untitled Git - lemmy.git/blob - server/src/settings.rs
update documentation, docker and ansible files
[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 }
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   pub pool_size: u32,
47 }
48
49 lazy_static! {
50   static ref SETTINGS: Settings = {
51     return match Settings::init() {
52       Ok(c) => c,
53       Err(e) => panic!("{}", e),
54     };
55   };
56 }
57
58 impl Settings {
59   
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 }