2024-06-13 09:21:56 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use figment::{
|
|
|
|
providers::{Env, Format, Toml},
|
|
|
|
Figment,
|
|
|
|
};
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
2024-06-16 18:14:56 +02:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2024-06-13 09:21:56 +02:00
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum FsConfig {
|
|
|
|
Local { data_dir: PathBuf },
|
|
|
|
}
|
|
|
|
|
2024-06-16 18:14:56 +02:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2024-06-13 09:21:56 +02:00
|
|
|
#[serde(rename_all = "lowercase")]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum DbConfig {
|
|
|
|
Sqlite {
|
2024-06-13 18:40:24 +02:00
|
|
|
db_dir: PathBuf,
|
2024-06-16 18:14:56 +02:00
|
|
|
#[serde(default = "default_db_sqlite_max_connections")]
|
|
|
|
max_connections: u32,
|
2024-06-13 09:21:56 +02:00
|
|
|
},
|
|
|
|
Postgres {
|
|
|
|
host: String,
|
2024-06-16 18:14:56 +02:00
|
|
|
#[serde(default = "default_db_postgres_port")]
|
|
|
|
port: u16,
|
2024-06-13 09:21:56 +02:00
|
|
|
user: String,
|
|
|
|
password: String,
|
2024-06-16 18:14:56 +02:00
|
|
|
db: String,
|
|
|
|
#[serde(default)]
|
|
|
|
schema: String,
|
|
|
|
#[serde(default = "default_db_postgres_max_connections")]
|
|
|
|
max_connections: u32,
|
2024-06-13 09:21:56 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-06-16 18:14:56 +02:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2024-06-13 09:21:56 +02:00
|
|
|
pub struct Config {
|
2024-06-16 18:14:56 +02:00
|
|
|
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,
|
2024-06-13 09:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn figment(config_file: impl AsRef<Path>) -> Figment {
|
|
|
|
Figment::new()
|
|
|
|
.merge(Toml::file(config_file))
|
|
|
|
.merge(Env::prefixed("RIETER_"))
|
|
|
|
}
|
|
|
|
}
|
2024-06-16 18:14:56 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|