48 lines
1.2 KiB
Rust
48 lines
1.2 KiB
Rust
use std::env;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Config {
|
|
pub host: String,
|
|
pub port: u16,
|
|
pub db_path: String,
|
|
pub max_body_mb: usize,
|
|
pub max_results: i64,
|
|
}
|
|
|
|
impl Default for Config {
|
|
fn default() -> Self {
|
|
Self {
|
|
host: "127.0.0.1".into(),
|
|
port: 3000,
|
|
db_path: "data/aresnext.db".into(),
|
|
max_body_mb: 2,
|
|
max_results: 50,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Config {
|
|
pub fn from_env() -> Self {
|
|
let def = Self::default();
|
|
Self {
|
|
host: env::var("ARS_NEXT_HOST").unwrap_or(def.host),
|
|
port: env::var("ARS_NEXT_PORT")
|
|
.ok()
|
|
.and_then(|s| s.parse().ok())
|
|
.unwrap_or(def.port),
|
|
db_path: env::var("ARS_NEXT_DB").unwrap_or(def.db_path),
|
|
max_body_mb: env::var("ARS_NEXT_MAX_BODY_MB")
|
|
.ok()
|
|
.and_then(|s| s.parse().ok())
|
|
.unwrap_or(def.max_body_mb),
|
|
max_results: env::var("ARS_NEXT_MAX_RESULTS")
|
|
.ok()
|
|
.and_then(|s| s.parse().ok())
|
|
.unwrap_or(def.max_results),
|
|
}
|
|
}
|
|
|
|
pub fn max_body_bytes(&self) -> usize {
|
|
self.max_body_mb * 1024 * 1024
|
|
}
|
|
} |