rustream/squire/settings.rs
1use std::{path, thread};
2use std::collections::HashMap;
3use std::net::ToSocketAddrs;
4
5/// Represents the configuration parameters for RuStream.
6pub struct Config {
7 /// Dictionary of key-value pairs for authorization (username and password).
8 pub authorization: HashMap<String, String>,
9 /// Source path for media files.
10 pub media_source: path::PathBuf,
11
12 /// Debug flag to enable debug level logging.
13 pub debug: bool,
14 /// Boolean flag to enable UTC timezone in logging. Defaults to local timezone.
15 pub utc_logging: bool,
16 /// Host IP address for media streaming.
17 pub media_host: String,
18 /// Port number for hosting the application.
19 pub media_port: u16,
20 /// Duration of a session in seconds.
21 pub session_duration: i64,
22 /// List of supported file formats.
23 pub file_formats: Vec<String>,
24
25 /// Number of worker threads to spin up the server.
26 pub workers: usize,
27 /// Maximum number of concurrent connections.
28 pub max_connections: usize,
29 /// Max payload allowed by the server in request body.
30 pub max_payload_size: usize,
31 /// List of websites (supports regex) to add to CORS configuration.
32 pub websites: Vec<String>,
33
34 /// Boolean flag to restrict session_token to be sent only via HTTPS
35 pub secure_session: bool,
36
37 /// Path to the private key file for SSL certificate
38 pub key_file: path::PathBuf,
39 /// Path to the full certificate chain file for SSL certificate
40 pub cert_file: path::PathBuf,
41}
42
43/// Returns the default value for debug flag.
44pub fn default_debug() -> bool { false }
45
46/// Returns the default value for UTC logging.
47pub fn default_utc_logging() -> bool { true }
48
49/// Returns the default value for SSL files.
50pub fn default_ssl() -> path::PathBuf { path::PathBuf::new() }
51
52/// Returns the default media host based on the local machine's IP address.
53pub fn default_media_host() -> String {
54 let hostname = "localhost";
55 match (hostname, 0).to_socket_addrs() {
56 Ok(mut addrs) => {
57 if let Some(addr) = addrs.find(|a| a.is_ipv4()) {
58 return addr.ip().to_string();
59 }
60 }
61 Err(err) => {
62 log::error!("Error resolving hostname: {}", err);
63 }
64 }
65 "localhost".to_string()
66}
67
68/// Returns the default media port (8000)
69pub fn default_media_port() -> u16 { 8000 }
70
71/// Returns the default session duration (3600 seconds)
72pub fn default_session_duration() -> i64 { 3600 }
73
74/// Returns the file formats supported by default.
75pub fn default_file_formats() -> Vec<String> {
76 vec!["mp4".to_string(), "mov".to_string(), "jpg".to_string(), "jpeg".to_string()]
77}
78
79/// Returns the default number of worker threads (half of logical cores)
80pub fn default_workers() -> usize {
81 let logical_cores = thread::available_parallelism();
82 match logical_cores {
83 Ok(cores) => cores.get() / 2,
84 Err(err) => {
85 log::error!("{}", err);
86 3
87 }
88 }
89}
90
91/// Returns the default maximum number of concurrent connections (3)
92pub fn default_max_connections() -> usize { 3 }
93
94/// Returns the default max payload size (100 MB)
95pub fn default_max_payload_size() -> usize { 100 * 1024 * 1024 }
96
97/// Returns an empty list as the default website (CORS configuration)
98pub fn default_websites() -> Vec<String> { Vec::new() }
99
100/// Returns the default value for secure_session
101pub fn default_secure_session() -> bool { false }