use oiseau::config::{Configuration, DatabaseConfig}; use pathbufd::PathBufD; use serde::{Deserialize, Serialize}; use tetratto_shared::hash::random_id; #[derive(Clone, Debug, Serialize, Deserialize)] pub struct ServiceHostsConfig { pub tetratto: String, pub buckets: String, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Config { /// The name of the site. Shown in the UI. #[serde(default = "default_name")] pub name: String, /// The (CSS) theme color of the site. Shown in the UI. #[serde(default = "default_theme_color")] pub theme_color: String, /// The slug of the instance's information page. /// /// Should be the pathname WITHOUT the leading slash. #[serde(default = "default_what_page_slug")] pub what_page_slug: String, /// The username of the handler account in charge of this instance on the /// linked Tetratto host. #[serde(default = "default_tetratto_handler_account_username")] pub tetratto_handler_account_username: String, /// Database configuration. #[serde(default = "default_database")] pub database: DatabaseConfig, /// Real IP header (for reverse proxy). #[serde(default = "default_real_ip_header")] pub real_ip_header: String, /// The host URL of required services. #[serde(default = "default_service_hosts")] pub service_hosts: ServiceHostsConfig, /// The master password which is allowed to do anything without password checks. pub master_pass: String, } fn default_name() -> String { "Fluffle".to_string() } fn default_theme_color() -> String { "#a3b3ff".to_string() } fn default_what_page_slug() -> String { "what".to_string() } fn default_tetratto_handler_account_username() -> String { "fluffle".to_string() } fn default_database() -> DatabaseConfig { DatabaseConfig::default() } fn default_real_ip_header() -> String { "CF-Connecting-IP".to_string() } fn default_service_hosts() -> ServiceHostsConfig { ServiceHostsConfig { tetratto: "https://tetratto.com".to_string(), buckets: "https://assetdelivery.tetratto.com".to_string(), } } impl Configuration for Config { fn db_config(&self) -> DatabaseConfig { self.database.to_owned() } } impl Default for Config { fn default() -> Self { Self { name: default_name(), theme_color: default_theme_color(), what_page_slug: default_what_page_slug(), tetratto_handler_account_username: default_tetratto_handler_account_username(), database: default_database(), real_ip_header: default_real_ip_header(), service_hosts: default_service_hosts(), master_pass: random_id(), } } } impl Config { /// Read the configuration file. pub fn read() -> Self { toml::from_str( &match std::fs::read_to_string(PathBufD::current().join("fluffle.toml")) { Ok(x) => x, Err(_) => { let x = Config::default(); std::fs::write( PathBufD::current().join("fluffle.toml"), &toml::to_string_pretty(&x).expect("failed to serialize config"), ) .expect("failed to write config"); return x; } }, ) .expect("failed to deserialize config") } }