Add server config
This commit is contained in:
parent
76a23b9682
commit
1420cf104e
2 changed files with 57 additions and 0 deletions
51
backend/sync_server/src/config.rs
Normal file
51
backend/sync_server/src/config.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
use std::path::Path;
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use database_config::DatabaseConfig;
|
||||
use log::debug;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use server_config::ServerConfig;
|
||||
use tokio::fs;
|
||||
use user_config::UserConfig;
|
||||
|
||||
pub mod database_config;
|
||||
pub mod server_config;
|
||||
pub mod user_config;
|
||||
|
||||
use crate::{
|
||||
consts::{DEFAULT_HOST, DEFAULT_MAX_CONNECTIONS, DEFAULT_PORT, DEFAULT_SQLITE_URL},
|
||||
errors::SyncServerError,
|
||||
};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize, Clone)]
|
||||
pub struct Config {
|
||||
pub database: DatabaseConfig,
|
||||
pub server: ServerConfig,
|
||||
pub users: UserConfig,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub async fn read(path: &Path) -> Result<Self> {
|
||||
Self::load_from_file(path)
|
||||
.await
|
||||
.with_context(|| format!("Cannot load configuration from disk from ({path:?})"))
|
||||
}
|
||||
|
||||
pub async fn load_from_file(path: &Path) -> Result<Self> {
|
||||
let contents = fs::read_to_string(path)
|
||||
.await
|
||||
.context("Failed to read configuration file")?;
|
||||
|
||||
let config = serde_yaml::from_str(&contents).context("Failed to parse configuration")?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
pub async fn write(&self, path: &Path) -> Result<()> {
|
||||
let contents = serde_yaml::to_string(&self).context("Failed to serialize configuration")?;
|
||||
|
||||
fs::write(path, contents)
|
||||
.await
|
||||
.context("Failed to write configuration to disk")
|
||||
}
|
||||
}
|
||||
6
backend/sync_server/src/consts.rs
Normal file
6
backend/sync_server/src/consts.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
pub const CONFIG_PATH: &str = "config.yaml";
|
||||
pub const DEFAULT_SQLITE_URL: &str = "db.sqlite3";
|
||||
pub const DEFAULT_HOST: &str = "127.0.0.1";
|
||||
pub const DEFAULT_PORT: u16 = 3000;
|
||||
pub const DEFAULT_MAX_CONNECTIONS: u32 = 12;
|
||||
pub const DEFAULT_MAX_BODY_SIZE_MB: usize = 4096;
|
||||
Loading…
Add table
Add a link
Reference in a new issue