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