1 extern crate lazy_static;
2 use config::{Config, ConfigError, Environment, File};
3 use serde::Deserialize;
7 static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
8 static CONFIG_FILE: &str = "config/config.hjson";
10 #[derive(Debug, Deserialize)]
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_enabled: bool,
21 pub federated_instance: Option<String>,
24 #[derive(Debug, Deserialize)]
25 pub struct RateLimitConfig {
27 pub message_per_second: i32,
29 pub post_per_second: i32,
31 pub register_per_second: i32,
34 #[derive(Debug, Deserialize)]
35 pub struct EmailConfig {
36 pub smtp_server: String,
37 pub smtp_login: String,
38 pub smtp_password: String,
39 pub smtp_from_address: String,
42 #[derive(Debug, Deserialize)]
53 static ref SETTINGS: Settings = {
54 match Settings::init() {
56 Err(e) => panic!("{}", e),
62 /// Reads config from the files and environment.
63 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
64 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
65 /// added to the config.
66 fn init() -> Result<Self, ConfigError> {
67 let mut s = Config::new();
69 s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
71 s.merge(File::with_name(CONFIG_FILE).required(false))?;
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 // Note: we need to use double underscore here, because otherwise variables containing
76 // underscore cant be set from environmnet.
77 // https://github.com/mehcode/config-rs/issues/73
78 s.merge(Environment::with_prefix("LEMMY").separator("__"))?;
83 /// Returns the config as a struct.
84 pub fn get() -> &'static Self {
88 /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
89 /// otherwise the connection url is generated from the config.
90 pub fn get_database_url(&self) -> String {
91 match env::var("LEMMY_DATABASE_URL") {
94 "postgres://{}:{}@{}:{}/{}",
96 self.database.password,
99 self.database.database
104 pub fn api_endpoint(&self) -> String {
105 format!("{}/api/v1", self.hostname)