rust_web_server/entry_point/command_line_args/
mod.rs

1use std::env;
2use crate::entry_point::Config;
3
4#[cfg(test)]
5mod tests;
6
7pub fn override_environment_variables_from_command_line_args() {
8    println!("\n  Start of Reading Command Line Arguments Section");
9
10    let args = env::args().collect();
11    let params = CommandLineArgument::get_command_line_arg_list();
12    CommandLineArgument::_parse(args, params);
13
14    println!("  End of Reading Command Line Arguments\n");
15}
16
17pub struct CommandLineArgument {
18    pub short_form: String,
19    pub long_form: String,
20    pub environment_variable: String,
21    pub _hint: Option<String>,
22}
23
24impl CommandLineArgument {
25    pub fn get_command_line_arg_list() -> Vec<CommandLineArgument> {
26        let mut argument_list : Vec<CommandLineArgument> = vec![];
27
28        let argument = CommandLineArgument {
29            short_form: "p".to_string(),
30            long_form: "port".to_string(),
31            environment_variable: Config::RWS_CONFIG_PORT.to_string(),
32            _hint: Some("Port".to_string())
33        };
34        argument_list.push(argument);
35
36        let argument = CommandLineArgument {
37            short_form: "i".to_string(),
38            long_form: "ip".to_string(),
39            environment_variable: Config::RWS_CONFIG_IP.to_string(),
40            _hint: Some("IP or domain".to_string())
41        };
42        argument_list.push(argument);
43
44        let argument = CommandLineArgument {
45            short_form: "t".to_string(),
46            long_form: "thread-count".to_string(),
47            environment_variable: Config::RWS_CONFIG_THREAD_COUNT.to_string(),
48            _hint: Some("Number of threads".to_string())
49        };
50        argument_list.push(argument);
51
52
53        let argument = CommandLineArgument {
54            short_form: "a".to_string(),
55            long_form: "cors-allow-all".to_string(),
56            environment_variable: Config::RWS_CONFIG_CORS_ALLOW_ALL.to_string(),
57            _hint: Some("If set to true, will allow all CORS requests, other CORS properties will be ignored".to_string())
58        };
59        argument_list.push(argument);
60
61        let argument = CommandLineArgument {
62            short_form: "o".to_string(),
63            long_form: "cors-allow-origins".to_string(),
64            environment_variable: Config::RWS_CONFIG_CORS_ALLOW_ORIGINS.to_string(),
65            _hint: Some("Comma separated list of allowed origins, example: https://foo.example,https://bar.example".to_string())
66        };
67        argument_list.push(argument);
68
69        let argument = CommandLineArgument {
70            short_form: "m".to_string(),
71            long_form: "cors-allow-methods".to_string(),
72            environment_variable: Config::RWS_CONFIG_CORS_ALLOW_METHODS.to_string(),
73            _hint: Some("Comma separated list of allowed methods, example: POST,PUT".to_string())
74        };
75        argument_list.push(argument);
76
77        let argument = CommandLineArgument {
78            short_form: "h".to_string(),
79            long_form: "cors-allow-headers".to_string(),
80            environment_variable: Config::RWS_CONFIG_CORS_ALLOW_HEADERS.to_string(),
81            _hint: Some("Comma separated list of allowed request headers, in lowercase, example: content-type,x-custom-header".to_string())
82        };
83        argument_list.push(argument);
84
85        let argument = CommandLineArgument {
86            short_form: "c".to_string(),
87            long_form: "cors-allow-credentials".to_string(),
88            environment_variable: Config::RWS_CONFIG_CORS_ALLOW_CREDENTIALS.to_string(),
89            _hint: Some("If set to true, will allow to transmit credentials via CORS requests".to_string())
90        };
91        argument_list.push(argument);
92
93        let argument = CommandLineArgument {
94            short_form: "e".to_string(),
95            long_form: "cors-expose-headers".to_string(),
96            environment_variable: Config::RWS_CONFIG_CORS_EXPOSE_HEADERS.to_string(),
97            _hint: Some("Comma separated list of allowed response headers, in lowercase, example: content-type,x-custom-header".to_string())
98        };
99        argument_list.push(argument);
100
101        let argument = CommandLineArgument {
102            short_form: "g".to_string(),
103            long_form: "cors-max-age".to_string(),
104            environment_variable: Config::RWS_CONFIG_CORS_MAX_AGE.to_string(),
105            _hint: Some("How long results of preflight requests can be cached (in seconds)".to_string())
106        };
107        argument_list.push(argument);
108
109        let argument = CommandLineArgument {
110            short_form: "r".to_string(),
111            long_form: "request-allocation-size-in-bytes".to_string(),
112            environment_variable: Config::RWS_CONFIG_REQUEST_ALLOCATION_SIZE_IN_BYTES.to_string(),
113            _hint: Some("In bytes, how much memory to allocate for each request".to_string())
114        };
115        argument_list.push(argument);
116
117        argument_list
118    }
119
120    pub fn _parse(args: Vec<String>, argument_list : Vec<CommandLineArgument>) -> Vec<CommandLineArgument> {
121        for unparsed_argument in args.iter() {
122
123            let boxed_split = unparsed_argument.split_once('=');
124            if boxed_split.is_some() {
125
126                let (parameter, value) = boxed_split.unwrap();
127                println!("\n    {}={}", parameter, value);
128                let boxed_predefined_argument =
129                    argument_list
130                        .iter()
131                        .find(
132                            |predefined_argument| {
133                                let short_form = predefined_argument.short_form.to_string();
134                                let long_form = predefined_argument.long_form.to_string();
135                                let _param_short_form = ["-", &short_form].join("");
136                                let _param_long_form = ["--", &long_form].join("");
137
138                                parameter.eq(_param_short_form.as_str()) ||
139                                    parameter.eq(_param_long_form.as_str())
140                });
141                if boxed_predefined_argument.is_some() {
142                    let predefined_argument = boxed_predefined_argument.unwrap();
143                    CommandLineArgument::set_environment_variable(predefined_argument, value.to_string());
144                }
145
146            }
147        }
148        argument_list
149    }
150
151    pub fn set_environment_variable(argument: &CommandLineArgument, value: String) {
152        env::set_var(&argument.environment_variable, &value);
153        println!("    Set env variable '{}' to value '{}'", argument.environment_variable, &value);
154    }
155}