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
#[cfg(test)]
mod tests;
pub mod command_line_args;
pub mod config_file;
pub mod environment_variables;
use std::{env};
use crate::entry_point::command_line_args::{override_environment_variables_from_command_line_args};
use crate::entry_point::config_file::override_environment_variables_from_config;
use crate::entry_point::environment_variables::read_system_environment_variables;
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Config {}
impl Config {
pub const RWS_CONFIG_IP: &'static str = "RWS_CONFIG_IP";
pub const RWS_CONFIG_PORT: &'static str = "RWS_CONFIG_PORT";
pub const RWS_CONFIG_THREAD_COUNT: &'static str = "RWS_CONFIG_THREAD_COUNT";
pub const RWS_CONFIG_CORS_ALLOW_ALL: &'static str = "RWS_CONFIG_CORS_ALLOW_ALL";
pub const RWS_CONFIG_CORS_ALLOW_ORIGINS: &'static str = "RWS_CONFIG_CORS_ALLOW_ORIGINS";
pub const RWS_CONFIG_CORS_ALLOW_CREDENTIALS: &'static str = "RWS_CONFIG_CORS_ALLOW_CREDENTIALS";
pub const RWS_CONFIG_CORS_ALLOW_HEADERS: &'static str = "RWS_CONFIG_CORS_ALLOW_HEADERS";
pub const RWS_CONFIG_CORS_ALLOW_METHODS: &'static str = "RWS_CONFIG_CORS_ALLOW_METHODS";
pub const RWS_CONFIG_CORS_EXPOSE_HEADERS: &'static str = "RWS_CONFIG_CORS_EXPOSE_HEADERS";
pub const RWS_CONFIG_CORS_MAX_AGE: &'static str = "RWS_CONFIG_CORS_MAX_AGE";
pub const RWS_DEFAULT_IP: &'static str = "127.0.0.1";
pub const RWS_DEFAULT_PORT: &'static i32 = &7878;
pub const RWS_DEFAULT_THREAD_COUNT: &'static i32 = &200;
}
pub fn bootstrap() {
read_system_environment_variables();
override_environment_variables_from_config(None);
override_environment_variables_from_command_line_args();
}
pub fn get_ip_port_thread_count() -> (String, i32, i32) {
let mut ip : String = Config::RWS_DEFAULT_IP.to_string();
let mut port: i32 = *Config::RWS_DEFAULT_PORT;
let mut thread_count: i32 = *Config::RWS_DEFAULT_THREAD_COUNT;
let boxed_ip = env::var(Config::RWS_CONFIG_IP);
if boxed_ip.is_ok() {
ip = boxed_ip.unwrap()
}
let boxed_port = env::var(Config::RWS_CONFIG_PORT);
if boxed_port.is_ok() {
let _port = boxed_port.unwrap();
let boxed_parse = _port.parse::<i32>();
if boxed_parse.is_ok() {
port = boxed_parse.unwrap();
} else {
eprintln!("unable to parse port value, expected number, got {}, variable: {}",
_port, Config::RWS_CONFIG_PORT);
}
} else {
eprintln!("unable to parse port value, variable: {}", Config::RWS_CONFIG_PORT);
}
let boxed_thread_count = env::var(Config::RWS_CONFIG_THREAD_COUNT);
if boxed_thread_count.is_ok() {
let _thread_count = boxed_thread_count.unwrap();
let boxed_parse = _thread_count.parse();
if boxed_parse.is_ok() {
thread_count = boxed_parse.unwrap()
} else {
eprintln!("unable to parse thread count value, expected number, got {}, variable: {}",
thread_count, Config::RWS_CONFIG_THREAD_COUNT);
}
} else {
eprintln!("unable to parse thread count value, variable: {}", Config::RWS_CONFIG_THREAD_COUNT);
}
(ip, port, thread_count)
}