diff options
| author | rtkay123 <dev@kanjala.com> | 2026-03-29 16:36:13 +0200 |
|---|---|---|
| committer | rtkay123 <dev@kanjala.com> | 2026-03-29 16:36:13 +0200 |
| commit | ff3b9fbaf400c344cae67ef9bdc9ba7d5f27b976 (patch) | |
| tree | ce417252c6ea56c540e4c78f43c9ad4af9b76c64 /lib | |
| parent | 57c4a5251c30d3dc2b78059fd208d8948d999056 (diff) | |
| download | warden-ff3b9fbaf400c344cae67ef9bdc9ba7d5f27b976.tar.bz2 warden-ff3b9fbaf400c344cae67ef9bdc9ba7d5f27b976.zip | |
refactor: move state to core
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/api-config/Cargo.toml | 26 | ||||
| -rw-r--r-- | lib/api-config/src/error.rs | 13 | ||||
| -rw-r--r-- | lib/api-config/src/lib.rs | 3 | ||||
| -rw-r--r-- | lib/api-config/src/schema/create.rs | 15 | ||||
| -rw-r--r-- | lib/api-config/src/schema/mod.rs | 33 | ||||
| -rw-r--r-- | lib/warden-core/src/lib.rs | 1 | ||||
| -rw-r--r-- | lib/warden-core/src/state/database.rs | 16 | ||||
| -rw-r--r-- | lib/warden-core/src/state/mod.rs | 24 |
8 files changed, 131 insertions, 0 deletions
diff --git a/lib/api-config/Cargo.toml b/lib/api-config/Cargo.toml new file mode 100644 index 0000000..ec31262 --- /dev/null +++ b/lib/api-config/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "api-config" +version = "0.1.0" +edition = "2024" +license.workspace = true +readme.workspace = true +documentation.workspace = true +homepage.workspace = true +publish.workspace = true + +[dependencies] +serde.workspace = true +serde_json.workspace = true +thiserror.workspace = true +time = { workspace = true, features = ["serde"] } +tracing.workspace = true +utoipa = { workspace = true, optional = true } +warden-core.workspace = true + +[features] +default = [] +utoipa = ["dep:utoipa", "utoipa/time"] + +[dependencies.sqlx] +workspace = true +features = ["json", "runtime-tokio-rustls", "time"] diff --git a/lib/api-config/src/error.rs b/lib/api-config/src/error.rs new file mode 100644 index 0000000..6f7b099 --- /dev/null +++ b/lib/api-config/src/error.rs @@ -0,0 +1,13 @@ +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum ConfigurationError { + #[error("data store disconnected")] + Disconnect(#[from] sqlx::Error), + #[error("the data for key `{0}` is not available")] + Redaction(String), + #[error("invalid header (expected {expected:?}, found {found:?})")] + InvalidHeader { expected: String, found: String }, + #[error("unknown data store error")] + Unknown, +} diff --git a/lib/api-config/src/lib.rs b/lib/api-config/src/lib.rs new file mode 100644 index 0000000..84366c1 --- /dev/null +++ b/lib/api-config/src/lib.rs @@ -0,0 +1,3 @@ +mod error; +pub mod schema; +pub use error::ConfigurationError; diff --git a/lib/api-config/src/schema/create.rs b/lib/api-config/src/schema/create.rs new file mode 100644 index 0000000..5c91f64 --- /dev/null +++ b/lib/api-config/src/schema/create.rs @@ -0,0 +1,15 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] +/// Transaction to monitor +pub struct CreateSchema { + #[serde(rename = "type")] + /// Transaction schema type + pub kind: String, + /// The schema's version + pub version: String, + /// Transaction data + #[serde(rename = "json_schema")] + pub schema: serde_json::Value, +} diff --git a/lib/api-config/src/schema/mod.rs b/lib/api-config/src/schema/mod.rs new file mode 100644 index 0000000..654e6ad --- /dev/null +++ b/lib/api-config/src/schema/mod.rs @@ -0,0 +1,33 @@ +pub mod create; + +use serde::{Deserialize, Serialize}; +use time::OffsetDateTime; + +use crate::ConfigurationError; + +/// Transaction to monitor +#[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))] +pub struct TransactionSchema { + #[serde(rename = "type")] + /// Transaction schema type + pub kind: String, + /// The schema's version + pub version: String, + /// JSON schema for transcation + #[serde(rename = "json_schema")] + pub schema: serde_json::Value, + #[serde(with = "time::serde::rfc3339")] + pub created_at: OffsetDateTime, + #[serde(with = "time::serde::rfc3339")] + pub updated_at: OffsetDateTime, +} + +pub trait SchemaDriver { + fn create( + &self, + name: impl AsRef<str>, + version: impl AsRef<str>, + schema: serde_json::Value, + ) -> impl std::future::Future<Output = Result<TransactionSchema, ConfigurationError>> + Send + Sync; +} diff --git a/lib/warden-core/src/lib.rs b/lib/warden-core/src/lib.rs index f200ba1..413087b 100644 --- a/lib/warden-core/src/lib.rs +++ b/lib/warden-core/src/lib.rs @@ -1,3 +1,4 @@ mod error; pub use error::WardenError; pub mod config; +pub mod state; diff --git a/lib/warden-core/src/state/database.rs b/lib/warden-core/src/state/database.rs new file mode 100644 index 0000000..cf34484 --- /dev/null +++ b/lib/warden-core/src/state/database.rs @@ -0,0 +1,16 @@ +use sqlx::PgPool; +use tracing::{debug, error}; + +use crate::{WardenError, config::cli::database::Database}; + +pub async fn connect(config: &Database) -> Result<PgPool, WardenError> { + let url = config.get_url()?; + let host = url.host_str(); + debug!(host = host, "connecting to database"); + + Ok(sqlx::postgres::PgPoolOptions::new() + .max_connections(config.database_pool_size.unwrap_or(10)) + .connect(url.as_str()) + .await + .inspect_err(|e| error!("{e}"))?) +} diff --git a/lib/warden-core/src/state/mod.rs b/lib/warden-core/src/state/mod.rs new file mode 100644 index 0000000..f4692c2 --- /dev/null +++ b/lib/warden-core/src/state/mod.rs @@ -0,0 +1,24 @@ +pub(crate) mod database; +use sqlx::PgPool; +use tracing_subscriber::EnvFilter; + +use crate::{WardenError, config::Configuration}; + +pub type LogHandle = tracing_subscriber::reload::Handle<EnvFilter, tracing_subscriber::Registry>; + +#[derive(Debug, Clone)] +pub struct AppState { + pub log_handle: LogHandle, + pub database: PgPool, +} + +impl AppState { + pub async fn new(log_handle: LogHandle, config: &Configuration) -> Result<Self, WardenError> { + let database = database::connect(&config.database).await?; + + Ok(Self { + log_handle, + database, + }) + } +} |
