hermod_api/services/
configuration.rs

1//! Contains structs and helpers related to server configuration.
2use serde_aux::field_attributes::deserialize_number_from_string;
3use sqlx::postgres::{PgConnectOptions, PgSslMode};
4use std::convert::{TryFrom, TryInto};
5
6/// Top-level configuration struct.
7#[derive(serde::Deserialize, Clone)]
8pub struct Settings {
9    pub database: DatabaseSettings,
10    pub application: ApplicationSettings,
11    pub twilio: TwilioSettings,
12}
13
14/// Contains settings relevant at the application level.
15#[derive(serde::Deserialize, Clone)]
16pub struct ApplicationSettings {
17    #[serde(deserialize_with = "deserialize_number_from_string")]
18    pub port: u16,
19    pub host: String,
20    pub base_url: String,
21    pub jwt_signing_key: String,
22    pub honeycomb_url: String,
23}
24
25/// Contains settings for interacting with Twilio's API.
26#[derive(serde::Deserialize, Clone)]
27pub struct TwilioSettings {
28    pub account_sid: String,
29    pub auth_token: String,
30    pub base_url: String,
31    pub from: String,
32}
33
34/// Contains settings for the database connection.
35#[derive(serde::Deserialize, Clone)]
36pub struct DatabaseSettings {
37    pub username: String,
38    pub password: String,
39    #[serde(deserialize_with = "deserialize_number_from_string")]
40    pub port: u16,
41    pub host: String,
42    pub database_name: String,
43    pub require_ssl: bool,
44}
45
46impl DatabaseSettings {
47    pub fn without_db(&self) -> PgConnectOptions {
48        let ssl_mode = if self.require_ssl {
49            PgSslMode::Require
50        } else {
51            PgSslMode::Prefer
52        };
53        PgConnectOptions::new()
54            .host(&self.host)
55            .username(&self.username)
56            .password(&self.password)
57            .port(self.port)
58            .ssl_mode(ssl_mode)
59    }
60
61    pub fn with_db(&self) -> PgConnectOptions {
62        self.without_db().database(&self.database_name)
63    }
64}
65
66/// Load settings from the configuration directory and environment variables.
67pub fn get_configuration() -> Result<Settings, config::ConfigError> {
68    let mut settings = config::Config::default();
69    let base_path = std::env::current_dir().expect("Failed to determine the current directory");
70    let configuration_directory = base_path.join("configuration");
71
72    // Read the "default" configuration file
73    settings.merge(config::File::from(configuration_directory.join("base")).required(true))?;
74
75    // Detect the running environment.
76    // Default to `local` if unspecified.
77    let environment: Environment = std::env::var("APP_ENVIRONMENT")
78        .unwrap_or_else(|_| "local".into())
79        .try_into()
80        .expect("Failed to parse APP_ENVIRONMENT.");
81
82    // Layer on the environment-specific values.
83    settings.merge(
84        config::File::from(configuration_directory.join(environment.as_str())).required(true),
85    )?;
86
87    // Add in settings from environment variables (with a prefix of APP and '__' as separator)
88    // E.g. `APP_APPLICATION__PORT=5001 would set `Settings.application.port`
89    settings.merge(config::Environment::with_prefix("app").separator("__"))?;
90
91    settings.try_into()
92}
93
94/// The possible runtime environment for our application.
95pub enum Environment {
96    Local,
97    Production,
98}
99
100impl Environment {
101    pub fn as_str(&self) -> &'static str {
102        match self {
103            Environment::Local => "local",
104            Environment::Production => "production",
105        }
106    }
107}
108
109impl TryFrom<String> for Environment {
110    type Error = String;
111
112    fn try_from(s: String) -> Result<Self, Self::Error> {
113        match s.to_lowercase().as_str() {
114            "local" => Ok(Self::Local),
115            "production" => Ok(Self::Production),
116            other => Err(format!(
117                "{} is not a supported environment. Use either `local` or `production`.",
118                other
119            )),
120        }
121    }
122}