-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfiguration.rs
More file actions
124 lines (114 loc) · 3.44 KB
/
configuration.rs
File metadata and controls
124 lines (114 loc) · 3.44 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use secrecy::{ExposeSecret, Secret};
use uuid::Uuid;
#[derive(serde::Deserialize)]
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
pub access_expiration: u32,
pub refresh_expiration: u32,
pub signup_secret: Secret<String>,
pub access_token_secret: Secret<String>,
pub refresh_token_secret: Secret<String>,
pub utility: UtilitySetting,
pub google_service: GoogleServiceSetting,
}
#[derive(serde::Deserialize)]
pub struct UtilitySetting {
pub port: u16,
pub host: String,
}
impl UtilitySetting {
pub fn get_utility_url(&self) -> String {
format!("http://{}:{}", self.host.clone(), self.port.clone())
}
}
#[derive(serde::Deserialize)]
pub struct GoogleServiceSetting {
pub target_user_ex_id: Uuid,
pub host: String,
pub port: u16,
pub task_list_name: String,
}
impl GoogleServiceSetting {
pub fn get_service_url(&self) -> String {
format!("http://{}:{}", self.host.clone(), self.port.clone())
}
}
#[derive(serde::Deserialize)]
pub struct DatabaseSettings {
pub username: String,
pub password: Secret<String>,
pub port: u16,
pub host: String,
pub database_name: String,
}
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let base_path = std::env::current_dir().expect("Failed to determine current directory");
let configuration_path = base_path.join("configuration");
let environment: Environment = std::env::var("APP_ENVIRONMENT")
.unwrap_or_else(|_| "local".into())
.try_into()
.expect("failed to parse APP_ENVIRONMENT");
let env_config = config::Environment::with_prefix("oism").separator("__");
let settings = config::Config::builder()
.add_source(config::File::from(configuration_path.join("base")).required(true))
.add_source(
config::File::from(configuration_path.join(environment.as_str())).required(true),
)
.add_source(env_config)
.build()?;
settings.try_deserialize::<Settings>()
}
impl DatabaseSettings {
pub fn connection_string_local(&self) -> String {
let DatabaseSettings {
username: _,
password: _,
port,
host,
database_name: _,
} = self;
format!("mongodb://{host}:{port}")
}
pub fn connection_string_cloud(&self) -> Secret<String> {
let DatabaseSettings {
username,
password,
port: _,
host,
database_name: _,
} = self;
Secret::new(format!(
"mongodb+srv://{username}:{password}@{host}",
password = password.expose_secret()
))
}
}
pub enum Environment {
Local,
Preview,
Production,
}
impl Environment {
fn as_str(&self) -> &str {
match self {
Environment::Local => "local",
Environment::Preview => "preview",
Environment::Production => "production",
}
}
}
impl TryFrom<String> for Environment {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
match s.to_lowercase().as_str() {
"local" => Ok(Self::Local),
"preview" => Ok(Self::Preview),
"production" => Ok(Self::Production),
other => Err(format!(
"{} is not supported environment. use either `local`,`preview` or `production` instead.",
other
)),
}
}
}