1 use config::{Config, ConfigError, Environment, File};
2 use serde::Deserialize;
6 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
7 static CONFIG_FILE: &str = "config/config.hjson";
9 #[derive(Debug, Deserialize)]
11 pub setup: Option<Setup>,
12 pub database: Database,
16 pub jwt_secret: String,
17 pub front_end_dir: String,
18 pub rate_limit: RateLimitConfig,
19 pub email: Option<EmailConfig>,
20 pub federation: Federation,
23 #[derive(Debug, Deserialize)]
25 pub admin_username: String,
26 pub admin_password: String,
27 pub admin_email: Option<String>,
28 pub site_name: String,
31 #[derive(Debug, Deserialize)]
32 pub struct RateLimitConfig {
34 pub message_per_second: i32,
36 pub post_per_second: i32,
38 pub register_per_second: i32,
41 #[derive(Debug, Deserialize)]
42 pub struct EmailConfig {
43 pub smtp_server: String,
44 pub smtp_login: Option<String>,
45 pub smtp_password: Option<String>,
46 pub smtp_from_address: String,
50 #[derive(Debug, Deserialize)]
60 #[derive(Debug, Deserialize)]
61 pub struct Federation {
63 pub followed_instances: String,
64 pub tls_enabled: bool,
68 static ref SETTINGS: Settings = {
69 match Settings::init() {
71 Err(e) => panic!("{}", e),
77 /// Reads config from the files and environment.
78 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
79 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
80 /// added to the config.
81 fn init() -> Result<Self, ConfigError> {
82 let mut s = Config::new();
84 s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
86 s.merge(File::with_name(CONFIG_FILE).required(false))?;
88 // Add in settings from the environment (with a prefix of LEMMY)
89 // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
90 // Note: we need to use double underscore here, because otherwise variables containing
91 // underscore cant be set from environmnet.
92 // https://github.com/mehcode/config-rs/issues/73
93 s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
98 /// Returns the config as a struct.
99 pub fn get() -> &'static Self {
103 /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
104 /// otherwise the connection url is generated from the config.
105 pub fn get_database_url(&self) -> String {
106 match env::var("LEMMY_DATABASE_URL") {
109 "postgres://{}:{}@{}:{}/{}",
111 self.database.password,
114 self.database.database
119 pub fn api_endpoint(&self) -> String {
120 format!("{}/api/v1", self.hostname)