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_COSTUMIZED: &str = "config/custom.hjson";
10 #[derive(Debug, Deserialize)]
12 pub database: Database,
16 pub jwt_secret: String,
17 pub rate_limit: RateLimitConfig,
18 pub email: Option<EmailConfig>,
21 #[derive(Debug, Deserialize)]
22 pub struct RateLimitConfig {
24 pub message_per_second: i32,
26 pub post_per_second: i32,
28 pub register_per_second: i32,
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,
39 #[derive(Debug, Deserialize)]
49 static ref SETTINGS: Settings = {
50 return match Settings::init() {
52 Err(e) => panic!("{}", e),
59 /// Reads config from the files and environment.
60 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
61 /// from CONFIG_FILE_COSTUMIZED (optional). Finally, values from the environment
62 /// (with prefix LEMMY) are added to the config.
63 fn init() -> Result<Self, ConfigError> {
64 let mut s = Config::new();
66 // Start off by merging in the "default" configuration file
67 s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
69 // TODO: we could also automatically load dev/prod configs based on environment
70 // https://github.com/mehcode/config-rs/blob/master/examples/hierarchical-env/src/settings.rs#L49
71 s.merge(File::with_name(CONFIG_FILE_COSTUMIZED).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 s.merge(Environment::with_prefix("LEMMY"))?;
80 /// Returns the config as a struct.
81 pub fn get() -> &'static Self {
85 /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
86 /// otherwise the connection url is generated from the config.
87 pub fn get_database_url(&self) -> String {
88 match env::var("LEMMY_DATABASE_URL") {
91 "postgres://{}:{}@{}:{}/{}",
93 self.database.password,
96 self.database.database
101 pub fn api_endpoint(&self) -> String {
102 format!("{}/api/v1", self.hostname)