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/warden-core/src | |
| parent | 57c4a5251c30d3dc2b78059fd208d8948d999056 (diff) | |
| download | warden-ff3b9fbaf400c344cae67ef9bdc9ba7d5f27b976.tar.bz2 warden-ff3b9fbaf400c344cae67ef9bdc9ba7d5f27b976.zip | |
refactor: move state to core
Diffstat (limited to 'lib/warden-core/src')
| -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 |
3 files changed, 41 insertions, 0 deletions
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, + }) + } +} |
