use std::path::{Path, PathBuf}; use figment::{ providers::{Env, Format, Toml}, Figment, }; use serde::Deserialize; #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "lowercase")] #[serde(tag = "type")] pub enum FsConfig { Local { data_dir: PathBuf }, } #[derive(Deserialize, Debug, Clone)] #[serde(rename_all = "lowercase")] #[serde(tag = "type")] pub enum DbConfig { Sqlite { db_dir: PathBuf, #[serde(default = "default_db_sqlite_max_connections")] max_connections: u32, }, Postgres { host: String, #[serde(default = "default_db_postgres_port")] port: u16, user: String, password: String, db: String, #[serde(default)] schema: String, #[serde(default = "default_db_postgres_max_connections")] max_connections: u32, }, } #[derive(Deserialize, Debug, Clone)] pub struct Config { pub api_key: String, #[serde(default = "default_domain")] pub domain: String, #[serde(default = "default_port")] pub port: u16, #[serde(default = "default_log_level")] pub log_level: String, pub fs: FsConfig, pub db: DbConfig, #[serde(default = "default_pkg_workers")] pub pkg_workers: u32, } impl Config { pub fn figment(config_file: impl AsRef) -> Figment { Figment::new() .merge(Toml::file(config_file)) .merge(Env::prefixed("RIETER_")) } } fn default_domain() -> String { String::from("0.0.0.0") } fn default_port() -> u16 { 8000 } fn default_log_level() -> String { String::from("tower_http=debug,rieterd=debug,sea_orm=debug") } fn default_db_sqlite_max_connections() -> u32 { 16 } fn default_db_postgres_port() -> u16 { 5432 } fn default_db_postgres_max_connections() -> u32 { 16 } fn default_pkg_workers() -> u32 { 1 }