-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.rs
More file actions
101 lines (89 loc) · 3.04 KB
/
Copy pathconfiguration.rs
File metadata and controls
101 lines (89 loc) · 3.04 KB
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
use secrecy::{ExposeSecret, Secret};
use serde_aux::field_attributes::deserialize_number_from_string;
use sqlx::{postgres::PgConnectOptions, ConnectOptions};
pub enum Environment {
Local,
Production,
}
impl Environment {
pub fn as_str(&self) -> &'static str {
match self {
Environment::Local => "local",
Environment::Production => "production",
}
}
}
impl TryFrom<String> for Environment {
type Error = String;
fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"local" => Ok(Environment::Local),
"production" => Ok(Environment::Production),
_ => Err(format!(
"{} is not a supported environment. Use either `local` or `production`.",
value
)),
}
}
}
#[derive(serde::Deserialize)]
pub struct Settings {
pub application: ApplicationSettings,
pub database: DatabaseSettings,
}
#[derive(serde::Deserialize)]
pub struct DatabaseSettings {
pub username: String,
pub password: Secret<String>,
pub host: String,
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub database_name: String,
pub require_ssl: bool,
}
#[derive(serde::Deserialize)]
pub struct ApplicationSettings {
#[serde(deserialize_with = "deserialize_number_from_string")]
pub port: u16,
pub host: String,
}
impl DatabaseSettings {
pub fn with_db(&self) -> PgConnectOptions {
self.without_db()
.database(&self.database_name)
.log_statements(tracing::log::LevelFilter::Trace)
}
pub fn without_db(&self) -> PgConnectOptions {
PgConnectOptions::new()
.host(&self.host)
.port(self.port)
.username(&self.username)
.password(self.password.expose_secret())
.ssl_mode(match self.require_ssl {
true => sqlx::postgres::PgSslMode::Require,
false => sqlx::postgres::PgSslMode::Prefer,
})
}
}
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let base_path = std::env::current_dir().expect("Failed to determine the current directory.");
let configuration_directory = base_path.join("configuration");
// let environment: Environment = std::env::var("APP_ENVIRONMENT").try_into()?;
let environment: Environment = std::env::var("APP_ENVIRONMENT")
.unwrap_or_else(|_| "local".into())
.try_into()
.expect("Failed to parse APP_ENVIRONMENT");
let environment_filename = format!("{}.yml", environment.as_str());
let settings = config::Config::builder()
.add_source(config::File::from(configuration_directory.join("base.yml")))
.add_source(config::File::from(
configuration_directory.join(environment_filename),
))
.add_source(
config::Environment::with_prefix("APP")
.prefix_separator("_")
.separator("__"),
)
.build()?;
settings.try_deserialize::<Settings>()
}