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