Newer
Older
use std::path::PathBuf;
#[derive(Clone)]
pub enum AppContext {
Debug,
Production,
}
#[derive(Clone)]
pub struct AppConfig {
pub exec_path: PathBuf,
pub exec_name: String,
pub context: AppContext,
pub storage_dir: PathBuf,
pub host: String,
pub port: u16,
pub port_tls: u16,
pub load: Option<PathBuf>,
pub ssl_certs_dir: PathBuf,
pub admin_id: String,
pub admin_pwd: String,
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
let exec_path = std::env::current_exe().unwrap();
let exec_name = exec_path
.file_name()
.expect("Unable to get executable name")
.to_str()
.unwrap()
.to_string();
let context = match app_args.context.as_str() {
"prod" => AppContext::Production,
_ => AppContext::Debug,
};
let storage_dir = match &app_args.app_storage_root {
Some(dir) => dir.join(&exec_name),
None => match context {
AppContext::Production => PathBuf::from("/var").join(&exec_name),
AppContext::Debug => dirs::home_dir()
.expect("Unable to get home dir")
.join(&exec_name),
},
};
let ssl_certs_dir = app_args.ssl_certs_dir.join(&app_args.host);
AppConfig {
exec_path: std::path::PathBuf::from(exec_path),
exec_name,
context,
storage_dir,
host: app_args.host,
port: app_args.port,
port_tls: app_args.port_tls,
load: app_args.load,
ssl_certs_dir,
admin_id: app_args.admin_id,
admin_pwd: app_args.admin_pwd,
pub fn get_log_level(&self) -> String {
match self.context {
AppContext::Debug => "debug".to_string(),
AppContext::Production => "info".to_string(),
}
}