generated from t/malachite
76 lines
2 KiB
Rust
76 lines
2 KiB
Rust
|
use oiseau::config::{Configuration, DatabaseConfig};
|
||
|
use pathbufd::PathBufD;
|
||
|
use serde::{Deserialize, Serialize};
|
||
|
|
||
|
#[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,
|
||
|
/// Real IP header (for reverse proxy).
|
||
|
#[serde(default = "default_real_ip_header")]
|
||
|
pub real_ip_header: String,
|
||
|
/// Database configuration.
|
||
|
#[serde(default = "default_database")]
|
||
|
pub database: DatabaseConfig,
|
||
|
}
|
||
|
|
||
|
fn default_name() -> String {
|
||
|
"App".to_string()
|
||
|
}
|
||
|
|
||
|
fn default_theme_color() -> String {
|
||
|
"#6ee7b7".to_string()
|
||
|
}
|
||
|
|
||
|
fn default_real_ip_header() -> String {
|
||
|
"CF-Connecting-IP".to_string()
|
||
|
}
|
||
|
|
||
|
fn default_database() -> DatabaseConfig {
|
||
|
DatabaseConfig::default()
|
||
|
}
|
||
|
|
||
|
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(),
|
||
|
real_ip_header: default_real_ip_header(),
|
||
|
database: default_database(),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Config {
|
||
|
/// Read the configuration file.
|
||
|
pub fn read() -> Self {
|
||
|
toml::from_str(
|
||
|
&match std::fs::read_to_string(PathBufD::current().join("app.toml")) {
|
||
|
Ok(x) => x,
|
||
|
Err(_) => {
|
||
|
let x = Config::default();
|
||
|
|
||
|
std::fs::write(
|
||
|
PathBufD::current().join("app.toml"),
|
||
|
&toml::to_string_pretty(&x).expect("failed to serialize config"),
|
||
|
)
|
||
|
.expect("failed to write config");
|
||
|
|
||
|
return x;
|
||
|
}
|
||
|
},
|
||
|
)
|
||
|
.expect("failed to deserialize config")
|
||
|
}
|
||
|
}
|