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 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)]
50 static ref SETTINGS: Settings = {
51 return match Settings::init() {
53 Err(e) => panic!("{}", e),
60 /// Reads config from the files and environment.
61 /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
62 /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
63 /// added to the config.
64 fn init() -> Result<Self, ConfigError> {
65 let mut s = Config::new();
67 s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;
69 s.merge(File::with_name(CONFIG_FILE).required(false))?;
71 // Add in settings from the environment (with a prefix of LEMMY)
72 // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
73 s.merge(Environment::with_prefix("LEMMY").separator("_"))?;
78 /// Returns the config as a struct.
79 pub fn get() -> &'static Self {
83 /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
84 /// otherwise the connection url is generated from the config.
85 pub fn get_database_url(&self) -> String {
86 match env::var("LEMMY_DATABASE_URL") {
89 "postgres://{}:{}@{}:{}/{}",
91 self.database.password,
94 self.database.database
99 pub fn api_endpoint(&self) -> String {
100 format!("{}/api/v1", self.hostname)