trifid/trifid-api/src/config.rs

83 lines
2.6 KiB
Rust
Raw Normal View History

2023-04-02 16:08:36 +00:00
use std::fs;
2023-04-02 17:06:16 +00:00
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
2023-04-02 16:08:36 +00:00
use log::error;
use once_cell::sync::Lazy;
use serde::{Serialize, Deserialize};
2023-02-28 01:50:31 +00:00
2023-04-02 16:08:36 +00:00
pub static CONFIG: Lazy<TrifidConfig> = Lazy::new(|| {
let config_str = match fs::read_to_string("/etc/trifid/config.toml") {
Ok(str) => str,
Err(e) => {
error!("Unable to read config file: {}", e);
std::process::exit(1);
}
};
2023-02-03 02:39:41 +00:00
2023-04-02 16:08:36 +00:00
match toml::from_str(&config_str) {
Ok(cfg) => cfg,
Err(e) => {
error!("Unable to parse config file: {}", e);
std::process::exit(1);
}
}
});
#[derive(Serialize, Debug, Deserialize)]
pub struct TrifidConfig {
2023-04-02 17:06:16 +00:00
pub database: TrifidConfigDatabase,
2023-04-02 19:25:52 +00:00
pub server: TrifidConfigServer,
2023-04-04 01:53:14 +00:00
pub tokens: TrifidConfigTokens,
pub crypto: TrifidConfigCryptography
2023-04-02 16:08:36 +00:00
}
#[derive(Serialize, Deserialize, Debug)]
pub struct TrifidConfigDatabase {
pub url: String,
#[serde(default = "max_connections_default")]
pub max_connections: u32,
#[serde(default = "min_connections_default")]
pub min_connections: u32,
#[serde(default = "time_defaults")]
pub connect_timeout: u64,
#[serde(default = "time_defaults")]
pub acquire_timeout: u64,
#[serde(default = "time_defaults")]
pub idle_timeout: u64,
#[serde(default = "time_defaults")]
pub max_lifetime: u64,
#[serde(default = "sqlx_logging_default")]
pub sqlx_logging: bool
}
2023-04-02 17:06:16 +00:00
#[derive(Serialize, Deserialize, Debug)]
pub struct TrifidConfigServer {
#[serde(default = "socketaddr_8080")]
pub bind: SocketAddr
}
2023-04-02 19:25:52 +00:00
#[derive(Serialize, Deserialize, Debug)]
pub struct TrifidConfigTokens {
#[serde(default = "magic_link_expiry_time")]
2023-04-02 23:12:08 +00:00
pub magic_link_expiry_time_seconds: u64,
#[serde(default = "session_token_expiry_time")]
pub session_token_expiry_time_seconds: u64,
#[serde(default = "totp_setup_timeout_time")]
2023-04-03 01:47:32 +00:00
pub totp_setup_timeout_time_seconds: u64,
#[serde(default = "mfa_tokens_expiry_time")]
pub mfa_tokens_expiry_time_seconds: u64
2023-04-02 19:25:52 +00:00
}
2023-04-04 01:53:14 +00:00
#[derive(Serialize, Deserialize, Debug)]
pub struct TrifidConfigCryptography {
pub data_encryption_key: String
}
2023-04-02 16:08:36 +00:00
fn max_connections_default() -> u32 { 100 }
fn min_connections_default() -> u32 { 5 }
fn time_defaults() -> u64 { 8 }
2023-04-02 17:06:16 +00:00
fn sqlx_logging_default() -> bool { true }
2023-04-02 19:25:52 +00:00
fn socketaddr_8080() -> SocketAddr { SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::from([0, 0, 0, 0]), 8080)) }
2023-04-02 23:12:08 +00:00
fn magic_link_expiry_time() -> u64 { 3600 } // 1 hour
fn session_token_expiry_time() -> u64 { 15780000 } // 6 months
2023-04-03 01:47:32 +00:00
fn totp_setup_timeout_time() -> u64 { 600 } // 10 minutes
fn mfa_tokens_expiry_time() -> u64 { 600 } // 10 minutes