sysmonk/squire/
settings.rs

1use std::net::ToSocketAddrs;
2use std::thread;
3
4/// Represents the configuration parameters for SysMonk.
5pub struct Config {
6    /// Username for login.
7    pub username: String,
8    /// Password for login.
9    pub password: String,
10
11    /// Debug flag to enable debug level logging.
12    pub debug: bool,
13    /// Boolean flag to enable UTC timezone in logging. Defaults to local timezone.
14    pub utc_logging: bool,
15    /// Host IP address for the API.
16    pub host: String,
17    /// Port number for hosting the application.
18    pub port: u16,
19    /// Duration of a session in seconds.
20    pub session_duration: i64,
21
22    /// Number of worker threads to spin up the server.
23    pub workers: usize,
24    /// Maximum number of concurrent connections.
25    pub max_connections: usize,
26    /// List of websites (supports regex) to add to CORS configuration.
27    pub websites: Vec<String>,
28}
29
30/// Returns the default value for debug flag.
31pub fn default_debug() -> bool { false }
32
33/// Returns the default value for UTC logging.
34pub fn default_utc_logging() -> bool { false }
35
36/// Returns the default host based on the local machine's IP address.
37pub fn default_host() -> String {
38    let hostname = "localhost";
39    match (hostname, 0).to_socket_addrs() {
40        Ok(mut addrs) => {
41            if let Some(addr) = addrs.find(|a| a.is_ipv4()) {
42                return addr.ip().to_string();
43            }
44        }
45        Err(err) => {
46            log::error!("Error resolving hostname: {}", err);
47        }
48    }
49    "localhost".to_string()
50}
51
52/// Returns the default port (8000)
53pub fn default_port() -> u16 { 8000 }
54
55/// Returns the default session duration (900 seconds)
56pub fn default_session_duration() -> i64 { 900 }
57
58/// Returns the default number of worker threads (half of logical cores)
59pub fn default_workers() -> usize {
60    let logical_cores = thread::available_parallelism();
61    match logical_cores {
62        Ok(cores) => cores.get() / 2,
63        Err(err) => {
64            log::error!("{}", err);
65            3
66        }
67    }
68}
69
70/// Returns the default maximum number of concurrent connections (3)
71pub fn default_max_connections() -> usize { 3 }
72
73/// Returns an empty list as the default website (CORS configuration)
74pub fn default_websites() -> Vec<String> { Vec::new() }