-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdotenv.rs
More file actions
50 lines (45 loc) · 1.31 KB
/
dotenv.rs
File metadata and controls
50 lines (45 loc) · 1.31 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
use crate::common::types::DatabaseType;
use dotenv;
#[derive(Clone, Debug)]
pub struct Dotenv {
pub db_type: Option<DatabaseType>,
pub db_user: Option<String>,
pub db_host: Option<String>,
pub db_port: Option<u16>,
pub db_pass: Option<String>,
pub db_name: Option<String>,
pub db_url: Option<String>,
pub pg_search_path: Option<String>,
}
impl Default for Dotenv {
fn default() -> Self {
Self::new(None)
}
}
impl Dotenv {
fn get_var(key: &str) -> Option<String> {
dotenv::var(key).ok()
}
pub fn new(path_to_dotenv: Option<String>) -> Dotenv {
if let Some(value) = path_to_dotenv {
dotenv::from_path(value).ok();
}
Dotenv {
db_type: match Self::get_var("DB_TYPE") {
None => None,
Some(val) => match val.as_str() {
"mysql" => Some(DatabaseType::Mysql),
"sqlite" => Some(DatabaseType::Sqlite),
_ => Some(DatabaseType::Postgres),
},
},
db_user: Self::get_var("DB_USER"),
db_host: Self::get_var("DB_HOST"),
db_port: Self::get_var("DB_PORT").map(|val| val.parse::<u16>().expect("DB_PORT is not a valid integer")),
db_pass: Self::get_var("DB_PASS"),
db_name: Self::get_var("DB_NAME"),
db_url: Self::get_var("DB_URL"),
pg_search_path: Self::get_var("PG_SEARCH_PATH"),
}
}
}