56 lines
1.9 KiB
Rust
56 lines
1.9 KiB
Rust
use std::time::Duration;
|
|
|
|
pub struct Config {
|
|
pub max_concurrent_executions: usize,
|
|
pub max_connected_devices: usize,
|
|
pub max_execution_time: Duration,
|
|
pub max_command_length: usize,
|
|
pub max_result_bytes: usize,
|
|
pub ping_interval: Duration,
|
|
pub ping_timeout: Duration,
|
|
}
|
|
|
|
impl Config {
|
|
const DEFAULT_MAX_CONCURRENT_EXECUTIONS: usize = 16;
|
|
const DEFAULT_MAX_CONNECTED_DEVICES: usize = 10;
|
|
const DEFAULT_MAX_EXECUTION_SECS: u64 = 3600;
|
|
const DEFAULT_MAX_COMMAND_LENGTH: usize = 65_536;
|
|
const DEFAULT_MAX_RESULT_BYTES: usize = 10 * 1024 * 1024;
|
|
const DEFAULT_PING_INTERVAL_SECS: u64 = 30;
|
|
const DEFAULT_PING_TIMEOUT_SECS: u64 = 10;
|
|
|
|
pub fn from_env() -> Self {
|
|
Self {
|
|
max_concurrent_executions: parse_env(
|
|
"MAX_CONCURRENT_EXECUTIONS",
|
|
Self::DEFAULT_MAX_CONCURRENT_EXECUTIONS,
|
|
),
|
|
max_connected_devices: parse_env(
|
|
"MAX_CONNECTED_DEVICES",
|
|
Self::DEFAULT_MAX_CONNECTED_DEVICES,
|
|
),
|
|
max_execution_time: Duration::from_secs(parse_env(
|
|
"MAX_EXECUTION_SECS",
|
|
Self::DEFAULT_MAX_EXECUTION_SECS,
|
|
)),
|
|
max_command_length: parse_env("MAX_COMMAND_LENGTH", Self::DEFAULT_MAX_COMMAND_LENGTH),
|
|
max_result_bytes: parse_env("MAX_RESULT_BYTES", Self::DEFAULT_MAX_RESULT_BYTES),
|
|
ping_interval: Duration::from_secs(parse_env(
|
|
"PING_INTERVAL_SECS",
|
|
Self::DEFAULT_PING_INTERVAL_SECS,
|
|
)),
|
|
ping_timeout: Duration::from_secs(parse_env(
|
|
"PING_TIMEOUT_SECS",
|
|
Self::DEFAULT_PING_TIMEOUT_SECS,
|
|
)),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn parse_env<T: std::str::FromStr>(key: &str, default: T) -> T {
|
|
std::env::var(key)
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(default)
|
|
}
|