Write default config if it doesn't exist
This commit is contained in:
parent
3f5409da60
commit
3c24ad83e1
4 changed files with 35 additions and 6 deletions
|
|
@ -2,6 +2,7 @@ use std::path::Path;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use database_config::DatabaseConfig;
|
use database_config::DatabaseConfig;
|
||||||
|
use log::warn;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use server_config::ServerConfig;
|
use server_config::ServerConfig;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
|
@ -11,24 +12,33 @@ pub mod database_config;
|
||||||
pub mod server_config;
|
pub mod server_config;
|
||||||
pub mod user_config;
|
pub mod user_config;
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
|
#[serde(default)]
|
||||||
pub database: DatabaseConfig,
|
pub database: DatabaseConfig,
|
||||||
|
#[serde(default)]
|
||||||
pub server: ServerConfig,
|
pub server: ServerConfig,
|
||||||
|
#[serde(default)]
|
||||||
pub users: UserConfig,
|
pub users: UserConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub async fn read(path: &Path) -> Result<Self> {
|
pub async fn read(path: &Path) -> Result<Self> {
|
||||||
Self::load_from_file(path)
|
if path.exists() {
|
||||||
.await
|
Self::load_from_file(path).await
|
||||||
.with_context(|| format!("Cannot load configuration from disk from ({path:?})"))
|
} else {
|
||||||
|
warn!("Configuration file not found, writing default configuration to {path:?}");
|
||||||
|
|
||||||
|
let config = Config::default();
|
||||||
|
config.write(path).await?;
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn load_from_file(path: &Path) -> Result<Self> {
|
pub async fn load_from_file(path: &Path) -> Result<Self> {
|
||||||
let contents = fs::read_to_string(path)
|
let contents = fs::read_to_string(path)
|
||||||
.await
|
.await
|
||||||
.context("Failed to read configuration file")?;
|
.with_context(|| format!("Cannot load configuration from disk from ({path:?})"))?;
|
||||||
|
|
||||||
let config = serde_yaml::from_str(&contents).context("Failed to parse configuration")?;
|
let config = serde_yaml::from_str(&contents).context("Failed to parse configuration")?;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,3 +21,12 @@ fn default_max_connections() -> u32 {
|
||||||
debug!("Using default max connections: {}", DEFAULT_MAX_CONNECTIONS);
|
debug!("Using default max connections: {}", DEFAULT_MAX_CONNECTIONS);
|
||||||
DEFAULT_MAX_CONNECTIONS
|
DEFAULT_MAX_CONNECTIONS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for DatabaseConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
sqlite_url: default_sqlite_url(),
|
||||||
|
max_connections: default_max_connections(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,3 +31,13 @@ fn default_max_body_size_mb() -> usize {
|
||||||
);
|
);
|
||||||
DEFAULT_MAX_BODY_SIZE_MB
|
DEFAULT_MAX_BODY_SIZE_MB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for ServerConfig {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
host: default_host(),
|
||||||
|
port: default_port(),
|
||||||
|
max_body_size_mb: default_max_body_size_mb(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
|
||||||
pub struct UserConfig {
|
pub struct UserConfig {
|
||||||
#[serde(default = "Vec::new")]
|
#[serde(default = "Vec::new")]
|
||||||
pub users: Vec<User>,
|
pub users: Vec<User>,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue