From 6dff65f30d17f95448781af840922e07511bec12 Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 13 Jun 2024 09:21:56 +0200 Subject: [PATCH] wip: cool config stuff --- Cargo.lock | 137 +++++++++++++++++++++++++++++++++++- server/Cargo.toml | 1 + server/rieterd.toml | 3 + server/src/cli.rs | 15 +++- server/src/config.rs | 45 ++++++++++++ server/src/error.rs | 10 ++- server/src/main.rs | 1 + server/src/repo/manager2.rs | 6 +- 8 files changed, 209 insertions(+), 9 deletions(-) create mode 100644 server/rieterd.toml create mode 100644 server/src/config.rs diff --git a/Cargo.lock b/Cargo.lock index d6e9e55..8520e63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -174,6 +174,15 @@ dependencies = [ "num-traits", ] +[[package]] +name = "atomic" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d818003e740b63afc82337e3160717f4f63078720a810b7b903e70a5d1d2994" +dependencies = [ + "bytemuck", +] + [[package]] name = "atomic-waker" version = "1.1.2" @@ -380,6 +389,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "bytemuck" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78834c15cb5d5efe3452d58b1e8ba890dd62d21907f867f383358198e56ebca5" + [[package]] name = "byteorder" version = "1.5.0" @@ -630,6 +645,20 @@ version = "2.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a" +[[package]] +name = "figment" +version = "0.10.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cb01cd46b0cf372153850f4c6c272d9cbea2da513e07538405148f95bd789f3" +dependencies = [ + "atomic", + "pear", + "serde", + "toml", + "uncased", + "version_check", +] + [[package]] name = "flume" version = "0.11.0" @@ -1037,6 +1066,12 @@ dependencies = [ "syn 2.0.66", ] +[[package]] +name = "inlinable_string" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb" + [[package]] name = "is_terminal_polyfill" version = "1.70.0" @@ -1386,6 +1421,29 @@ version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" +[[package]] +name = "pear" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bdeeaa00ce488657faba8ebf44ab9361f9365a97bd39ffb8a60663f57ff4b467" +dependencies = [ + "inlinable_string", + "pear_codegen", + "yansi", +] + +[[package]] +name = "pear_codegen" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bab5b985dc082b345f812b7df84e1bef27e7207b39e448439ba8bd69c93f147" +dependencies = [ + "proc-macro2", + "proc-macro2-diagnostics", + "quote", + "syn 2.0.66", +] + [[package]] name = "pem-rfc7468" version = "0.7.0" @@ -1478,7 +1536,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" dependencies = [ - "toml_edit", + "toml_edit 0.21.1", ] [[package]] @@ -1514,6 +1572,19 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "proc-macro2-diagnostics" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af066a9c399a26e020ada66a034357a868728e72cd426f3adcd35f80d88d88c8" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.66", + "version_check", + "yansi", +] + [[package]] name = "ptr_meta" version = "0.1.4" @@ -1657,6 +1728,7 @@ dependencies = [ "axum", "chrono", "clap", + "figment", "futures", "http-body-util", "libarchive", @@ -2037,6 +2109,15 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_spanned" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79e674e01f999af37c49f70a6ede167a8a60b2503e56c5599532a65baa5969a0" +dependencies = [ + "serde", +] + [[package]] name = "serde_urlencoded" version = "0.7.1" @@ -2624,11 +2705,26 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.8.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f49eb2ab21d2f26bd6db7bf383edc527a7ebaee412d17af4d40fdccd442f335" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.14", +] + [[package]] name = "toml_datetime" version = "0.6.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4badfd56924ae69bcc9039335b2e017639ce3f9b001c393c1b2d1ef846ce2cbf" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -2638,7 +2734,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ "indexmap", "toml_datetime", - "winnow", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.22.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f21c7aaf97f1bd9ca9d4f9e73b0a6c74bd5afef56f2bc931943a6e1c37e04e38" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.13", ] [[package]] @@ -2763,6 +2872,15 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "uncased" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1b88fcfe09e89d3866a5c11019378088af2d24c3fbd4f0543f96b479ec90697" +dependencies = [ + "version_check", +] + [[package]] name = "unicase" version = "2.7.0" @@ -3129,6 +3247,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1" +dependencies = [ + "memchr", +] + [[package]] name = "wyz" version = "0.5.1" @@ -3138,6 +3265,12 @@ dependencies = [ "tap", ] +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" + [[package]] name = "zerocopy" version = "0.7.34" diff --git a/server/Cargo.toml b/server/Cargo.toml index 75b5a09..b1fc688 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -10,6 +10,7 @@ authors = ["Jef Roosens"] axum = { version = "0.7.5", features = ["http2", "macros"] } chrono = { version = "0.4.26", features = ["serde"] } clap = { version = "4.3.12", features = ["env", "derive"] } +figment = { version = "0.10.19", features = ["env", "toml"] } futures = "0.3.28" http-body-util = "0.1.1" libarchive = { path = "../libarchive" } diff --git a/server/rieterd.toml b/server/rieterd.toml new file mode 100644 index 0000000..5e3b8b7 --- /dev/null +++ b/server/rieterd.toml @@ -0,0 +1,3 @@ +api_key = "test" +port = 8000 +log_level = "tower_http=debug,rieterd=debug" diff --git a/server/src/cli.rs b/server/src/cli.rs index 2df7f7c..c0867b9 100644 --- a/server/src/cli.rs +++ b/server/src/cli.rs @@ -1,9 +1,10 @@ use crate::{distro::MetaDistroMgr, Config, Global}; +use std::{io, path::PathBuf}; + use axum::Router; use clap::Parser; use sea_orm_migration::MigratorTrait; -use std::{io, path::PathBuf}; use tower_http::trace::TraceLayer; use tracing::debug; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; @@ -18,6 +19,14 @@ pub struct Cli { #[arg(env = "RIETER_API_KEY")] pub api_key: String, + #[arg( + short, + long, + env = "RIETER_CONFIG_FILE", + default_value = "./rieterd.toml" + )] + pub config_file: PathBuf, + /// Database connection URL; either sqlite:// or postgres://. Defaults to rieter.sqlite in the /// data directory #[arg(short, long, env = "RIETER_DATABASE_URL")] @@ -52,6 +61,10 @@ impl Cli { pub async fn run(&self) -> crate::Result<()> { self.init_tracing(); + tracing::debug!("{:?}", &self.config_file); + let new_config = crate::config::Config::figment(&self.config_file).extract()?; + tracing::debug!("{:?}", new_config); + let db_url = if let Some(url) = &self.database_url { url.clone() } else { diff --git a/server/src/config.rs b/server/src/config.rs new file mode 100644 index 0000000..99e61d3 --- /dev/null +++ b/server/src/config.rs @@ -0,0 +1,45 @@ +use std::path::{Path, PathBuf}; + +use figment::{ + providers::{Env, Format, Toml}, + Figment, +}; +use serde::Deserialize; + +#[derive(Deserialize)] +#[serde(rename_all = "lowercase")] +#[serde(tag = "type")] +pub enum FsConfig { + Local { data_dir: PathBuf }, +} + +#[derive(Deserialize)] +#[serde(rename_all = "lowercase")] +#[serde(tag = "type")] +pub enum DbConfig { + Sqlite { + path: PathBuf, + }, + Postgres { + host: String, + user: String, + password: String, + }, +} + +#[derive(Deserialize)] +pub struct Config { + api_key: String, + port: u16, + log_level: String, + fs: FsConfig, + db: DbConfig, +} + +impl Config { + pub fn figment(config_file: impl AsRef) -> Figment { + Figment::new() + .merge(Toml::file(config_file)) + .merge(Env::prefixed("RIETER_")) + } +} diff --git a/server/src/error.rs b/server/src/error.rs index 5c3e920..cc86445 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -14,6 +14,7 @@ pub enum ServerError { Db(sea_orm::DbErr), Status(StatusCode), Archive(libarchive::error::ArchiveError), + Figment(figment::Error), } impl fmt::Display for ServerError { @@ -24,6 +25,7 @@ impl fmt::Display for ServerError { ServerError::Status(status) => write!(fmt, "{}", status), ServerError::Db(err) => write!(fmt, "{}", err), ServerError::Archive(err) => write!(fmt, "{}", err), + ServerError::Figment(err) => write!(fmt, "{}", err), } } } @@ -41,7 +43,7 @@ impl IntoResponse for ServerError { ServerError::Db(sea_orm::DbErr::RecordNotFound(_)) => { StatusCode::NOT_FOUND.into_response() } - ServerError::Db(_) | ServerError::Archive(_) => { + ServerError::Db(_) | ServerError::Archive(_) | ServerError::Figment(_) => { StatusCode::INTERNAL_SERVER_ERROR.into_response() } } @@ -83,3 +85,9 @@ impl From for ServerError { ServerError::Archive(err) } } + +impl From for ServerError { + fn from(err: figment::Error) -> Self { + ServerError::Figment(err) + } +} diff --git a/server/src/main.rs b/server/src/main.rs index d3cbdf0..5df9e18 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -1,5 +1,6 @@ mod api; mod cli; +mod config; pub mod db; mod distro; mod error; diff --git a/server/src/repo/manager2.rs b/server/src/repo/manager2.rs index d1e7d23..76f7ab0 100644 --- a/server/src/repo/manager2.rs +++ b/server/src/repo/manager2.rs @@ -116,11 +116,7 @@ impl RepoMgr { Ok(repo_id) } - async fn add_pkg_from_path>( - &self, - path: P, - repo: i32, - ) -> crate::Result<()> { + async fn add_pkg_from_path>(&self, path: P, repo: i32) -> crate::Result<()> { let path_clone = path.as_ref().to_path_buf(); let pkg = tokio::task::spawn_blocking(move || package::Package::open(path_clone)) .await