server/process.rs
1//! # use server::process::{ [CliArgs] }
2//!
3//! A library that hanldes process related concepts for the web-server.
4
5use std::env;
6
7const ALLOWED_ARGS: [&str; 3] = ["--port", "--threads", "--dir-path"];
8
9/// The options to start the web-server are collected as command line arguments and parsed into a
10/// convinient data structure which is used throughout the program.
11/// The only supported arguments are `--port`, `--thread`, and `--dir-path`.
12pub struct CliArgs {
13 /// This represents the `--port` option which binds the application to the specified port.
14 /// When not provided, a random port is automatically assigned and displayed on the command line.
15 pub port: Option<String>,
16
17 /// This represents the `--thread` option which specifies the number of threads to be used for the server.
18 /// When not provided, the program defaults to a thread count of 10.
19 pub thread_count: usize,
20
21 /// This represents the `--dir-path` option which specifies the root directory of the static files to be served.
22 /// It could be relative to the current directory path or an absolute path.
23 /// When not provided, the current directory acts as the root directory for the static files.
24 pub dir_path: String
25}
26
27impl CliArgs {
28
29 /// Returns a struct with options provided to run the server in the command line.
30 ///
31 /// # Allowed command line arguments
32 ///
33 /// * `--port` - Specifies the port to bind the application to.
34 /// * `--threads` - Specifies the number of threads to serve concurrent requests.
35 /// * `--dir-path` - Specifies the path where the static files are located.
36 ///
37 /// # Example
38 ///
39 /// ```
40 /// use server::process::CliArgs;
41 /// use std::env;
42 ///
43 /// // Default port
44 /// let args: CliArgs = CliArgs::get();
45 /// assert_eq!(args.thread_count, 10);
46 ///
47 /// // Default file path
48 /// let env_args: Vec<String> = env::args().collect();
49 /// assert_eq!(args.dir_path, env_args.get(0).unwrap().as_str());
50 ///
51 /// // No port
52 /// assert_eq!(args.port, None);
53 /// ```
54 ///
55 /// # Panics
56 ///
57 /// - Panics if a command line argument is not allowed. Allowed args include `--threads`, `--port`, `--dir-path`.
58 /// - Panics if a value is not provided for an argument.
59 /// - Panics if the value for `--threads` is not an integer.
60 ///
61 pub fn get() -> CliArgs {
62 let args: Vec<String> = env::args().collect();
63 let mut return_args = CliArgs { port: None, thread_count: 10, dir_path: String::from(args[0].as_str()) };
64
65 let mut i = 1;
66
67 while i < args.len() {
68 let arg = &args[i];
69 assert!(
70 ALLOWED_ARGS.contains(&arg.as_str()),
71 "Invalid argument {}", &arg.as_str()
72 );
73
74 assert!(i + 1 < args.len(), "Missing value for {}", arg);
75
76 if arg == "--port" {
77 return_args.port = Some(String::from(args[i + 1].as_str()));
78 } else if arg == "--threads" {
79 let pool_size = args[i + 1].as_str().parse();
80 match pool_size {
81 Ok(size) => {return_args.thread_count = size}
82 Err(_) => {
83 panic!("Unrecognized thread pool size. Please pass in a number")
84 }
85 }
86 } else if arg == "--dir-path" {
87 return_args.dir_path = String::from(args[i + 1].as_str());
88 }
89
90 i += 2;
91 }
92
93 return_args
94 }
95}