summaryrefslogtreecommitdiffstats
path: root/server/src/settings.rs
blob: 216c057e4ee7fcdd180c9a757618750a2cd6e324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
use config::{Config, ConfigError, Environment, File};
use failure::Error;
use serde::Deserialize;
use std::env;
use std::fs;
use std::net::IpAddr;

static CONFIG_FILE_DEFAULTS: &str = "config/defaults.hjson";
static CONFIG_FILE: &str = "config/config.hjson";

#[derive(Debug, Deserialize)]
pub struct Settings {
  pub setup: Option<Setup>,
  pub database: Database,
  pub hostname: String,
  pub bind: IpAddr,
  pub port: u16,
  pub jwt_secret: String,
  pub front_end_dir: String,
  pub rate_limit: RateLimitConfig,
  pub email: Option<EmailConfig>,
  pub federation_enabled: bool,
}

#[derive(Debug, Deserialize)]
pub struct Setup {
  pub admin_username: String,
  pub admin_password: String,
  pub admin_email: Option<String>,
  pub site_name: String,
}

#[derive(Debug, Deserialize)]
pub struct RateLimitConfig {
  pub message: i32,
  pub message_per_second: i32,
  pub post: i32,
  pub post_per_second: i32,
  pub register: i32,
  pub register_per_second: i32,
}

#[derive(Debug, Deserialize)]
pub struct EmailConfig {
  pub smtp_server: String,
  pub smtp_login: Option<String>,
  pub smtp_password: Option<String>,
  pub smtp_from_address: String,
  pub use_tls: bool,
}

#[derive(Debug, Deserialize)]
pub struct Database {
  pub user: String,
  pub password: String,
  pub host: String,
  pub port: i32,
  pub database: String,
  pub pool_size: u32,
}

lazy_static! {
  static ref SETTINGS: Settings = {
    match Settings::init() {
      Ok(c) => c,
      Err(e) => panic!("{}", e),
    }
  };
}

impl Settings {
  /// Reads config from the files and environment.
  /// First, defaults are loaded from CONFIG_FILE_DEFAULTS, then these values can be overwritten
  /// from CONFIG_FILE (optional). Finally, values from the environment (with prefix LEMMY) are
  /// added to the config.
  fn init() -> Result<Self, ConfigError> {
    let mut s = Config::new();

    s.merge(File::with_name(CONFIG_FILE_DEFAULTS))?;

    s.merge(File::with_name(CONFIG_FILE).required(false))?;

    // Add in settings from the environment (with a prefix of LEMMY)
    // Eg.. `LEMMY_DEBUG=1 ./target/app` would set the `debug` key
    // Note: we need to use double underscore here, because otherwise variables containing
    //       underscore cant be set from environmnet.
    // https://github.com/mehcode/config-rs/issues/73
    s.merge(Environment::with_prefix("LEMMY").separator("__"))?;

    s.try_into()
  }

  /// Returns the config as a struct.
  pub fn get() -> &'static Self {
    &SETTINGS
  }

  /// Returns the postgres connection url. If LEMMY_DATABASE_URL is set, that is used,
  /// otherwise the connection url is generated from the config.
  pub fn get_database_url(&self) -> String {
    match env::var("LEMMY_DATABASE_URL") {
      Ok(url) => url,
      Err(_) => format!(
        "postgres://{}:{}@{}:{}/{}",
        self.database.user,
        self.database.password,
        self.database.host,
        self.database.port,
        self.database.database
      ),
    }
  }

  pub fn api_endpoint(&self) -> String {
    format!("{}/api/v1", self.hostname)
  }

  pub fn read_config_file() -> Result<String, Error> {
    Ok(fs::read_to_string(CONFIG_FILE)?)
  }

  pub fn save_config_file(data: &str) -> Result<String, Error> {
    fs::write(CONFIG_FILE, data)?;
    Self::init()?;
    Self::read_config_file()
  }
}