Files
rust-imager/src/config.rs
lychang 093924ea99
Some checks failed
Rust CI / Build Release (push) Has been cancelled
Rust CI / Cargo Check (push) Has been cancelled
update
2026-07-19 16:34:04 +08:00

75 lines
1.6 KiB
Rust

use serde::Deserialize;
use std::path::PathBuf;
use config;
#[derive(Debug, Deserialize, Clone)]
pub struct Config {
pub database: DatabaseConfig,
pub server: ServerConfig,
pub storage: StorageConfig,
pub queue: QueueConfig,
pub api_server: Option<ApiServerConfig>,
#[serde(default)]
pub old_db: Option<OldDbConfig>,
}
#[derive(Debug, Deserialize, Clone)]
pub struct DatabaseConfig {
pub url: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct ServerConfig {
pub host: String,
pub port: u16,
}
#[derive(Debug, Deserialize, Clone)]
pub struct StorageConfig {
pub images_dir: PathBuf,
}
#[derive(Debug, Deserialize, Clone)]
pub struct QueueConfig {
pub max_concurrent_tasks: usize,
pub queue_size_limit: usize,
pub high_request_rate_threshold: f64,
pub idle_request_rate_threshold: f64,
}
#[derive(Debug, Deserialize, Clone)]
pub struct ApiServerConfig {
pub url: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct OldDbConfig {
pub path: String,
#[serde(default = "default_auto_migrate")]
pub auto_migrate: bool,
#[serde(default = "default_auto_parse")]
pub auto_parse: bool,
}
fn default_auto_migrate() -> bool {
false
}
fn default_auto_parse() -> bool {
true
}
impl Config {
pub fn load() -> Result<Self, config::ConfigError> {
let config = config::Config::builder()
.add_source(config::File::with_name("config"))
.build()?;
config.try_deserialize()
}
pub fn server_address(&self) -> String {
format!("{}:{}", self.server.host, self.server.port)
}
}