diff options
| author | rtkay123 <dev@kanjala.com> | 2026-02-09 17:54:46 +0200 |
|---|---|---|
| committer | rtkay123 <dev@kanjala.com> | 2026-02-09 17:54:46 +0200 |
| commit | a9630ecdc459068ca51ee2d7be3837d609840842 (patch) | |
| tree | 3bf6d1da4aefbaa351a36a0c63228bcdcf6b4917 /lib/shared-svc | |
| parent | d2339ca8869af12c0fd8cf6fc87986f06b487de9 (diff) | |
| download | sellershut-a9630ecdc459068ca51ee2d7be3837d609840842.tar.bz2 sellershut-a9630ecdc459068ca51ee2d7be3837d609840842.zip | |
feat: connect to database
Diffstat (limited to 'lib/shared-svc')
| -rw-r--r-- | lib/shared-svc/Cargo.toml | 6 | ||||
| -rw-r--r-- | lib/shared-svc/src/cache/key.rs | 27 | ||||
| -rw-r--r-- | lib/shared-svc/src/cache/mod.rs | 3 | ||||
| -rw-r--r-- | lib/shared-svc/src/database/mod.rs | 16 | ||||
| -rw-r--r-- | lib/shared-svc/src/lib.rs | 9 |
5 files changed, 58 insertions, 3 deletions
diff --git a/lib/shared-svc/Cargo.toml b/lib/shared-svc/Cargo.toml index d95d3ef..85a1c82 100644 --- a/lib/shared-svc/Cargo.toml +++ b/lib/shared-svc/Cargo.toml @@ -11,18 +11,20 @@ bb8-redis = { version = "0.26.0", optional = true } log = "0.4.29" redis = { version = "1.0.3", optional = true } secrecy.workspace = true +sqlx = { workspace = true, optional = true } thiserror.workspace = true tokio = { workspace = true, optional = true } tracing.workspace = true url.workspace = true [features] -default = [] +default = ["cache", "database"] cache = [ - "bb8-redis", + "dep:bb8-redis", "redis/cluster-async", "redis/connection-manager", "redis/sentinel", "redis/tokio-comp", "tokio/sync" ] +database = ["dep:sqlx"] diff --git a/lib/shared-svc/src/cache/key.rs b/lib/shared-svc/src/cache/key.rs new file mode 100644 index 0000000..756b7d0 --- /dev/null +++ b/lib/shared-svc/src/cache/key.rs @@ -0,0 +1,27 @@ +use redis::{ToRedisArgs, ToSingleRedisArg}; + +#[derive(Clone, Copy)] +pub enum CacheKey<'a> { + Session(&'a str), +} + +impl CacheKey<'_> { + pub fn key(&self) -> &str { + match self { + CacheKey::Session(_) => "session:*", + } + } +} + +impl ToRedisArgs for CacheKey<'_> { + fn write_redis_args<W>(&self, out: &mut W) + where + W: ?Sized + redis::RedisWrite, + { + out.write_arg_fmt(match self { + CacheKey::Session(id) => format!("session:{id}"), + }) + } +} + +impl ToSingleRedisArg for CacheKey<'_> {} diff --git a/lib/shared-svc/src/cache/mod.rs b/lib/shared-svc/src/cache/mod.rs index cd15463..90cab04 100644 --- a/lib/shared-svc/src/cache/mod.rs +++ b/lib/shared-svc/src/cache/mod.rs @@ -1,6 +1,9 @@ mod cluster; mod config; +mod key; mod sentinel; +pub use key::*; +pub use redis; pub use sentinel::SentinelConfig; pub use config::*; diff --git a/lib/shared-svc/src/database/mod.rs b/lib/shared-svc/src/database/mod.rs new file mode 100644 index 0000000..bbc1ba3 --- /dev/null +++ b/lib/shared-svc/src/database/mod.rs @@ -0,0 +1,16 @@ +use sqlx::PgPool; +use tracing::{debug, error}; +use url::Url; + +use crate::ServiceError; + +pub async fn connect(url: &Url, pool_size: u32) -> Result<PgPool, ServiceError> { + let host = url.host_str(); + debug!(host = host, "connecting to database"); + + Ok(sqlx::postgres::PgPoolOptions::new() + .max_connections(pool_size) + .connect(url.as_str()) + .await + .inspect_err(|e| error!("{e}"))?) +} diff --git a/lib/shared-svc/src/lib.rs b/lib/shared-svc/src/lib.rs index 92d9c32..d4852a5 100644 --- a/lib/shared-svc/src/lib.rs +++ b/lib/shared-svc/src/lib.rs @@ -1,12 +1,19 @@ #[cfg(feature = "cache")] pub mod cache; +#[cfg(feature = "database")] +pub mod database; + use thiserror::Error; #[derive(Error, Debug)] pub enum ServiceError { - #[error("data store disconnected")] + #[error("cache error")] + #[cfg(feature = "cache")] Cache(#[from] redis::RedisError), + #[error("database error")] + #[cfg(feature = "database")] + Database(#[from] sqlx::Error), #[error("the data for key `{0}` is not available")] Redaction(String), #[error("invalid header (expected {expected:?}, found {found:?})")] |
