From 2b800c2df431fa3e310f502010d61414042af768 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Sat, 27 Jun 2026 15:26:22 -0400 Subject: [PATCH] Add key-value store setup function --- src/server/key_val_store.rs | 29 +++++++++++++++++++++++++++++ src/server/mod.rs | 1 + 2 files changed, 30 insertions(+) create mode 100644 src/server/key_val_store.rs diff --git a/src/server/key_val_store.rs b/src/server/key_val_store.rs new file mode 100644 index 0000000..fa1f24f --- /dev/null +++ b/src/server/key_val_store.rs @@ -0,0 +1,29 @@ +use fred::prelude::*; + +use crate::util::error::{Contextualize, Error, ErrorType}; + +const KEY_VAL_POOL_SIZE: usize = 4; + +pub type KeyValPool = Pool; + +pub async fn setup(connection_uri: &str) -> Result { + let config = Config::from_url(connection_uri) + .map_err(|e| ErrorType::KeyValStore(e.to_string())) + .err_context("Error creating key-value store config")?; + + let pool = Builder::from_config(config) + .build_pool(KEY_VAL_POOL_SIZE) + // At time of writing the only error that could occur here is if config is not provided. + // Since we're building a pool `from_config`, this shouldn't be possible + .map_err(|e| ErrorType::KeyValStore(e.to_string())) + .err_context("Error creating pool for key-value store")?; + + tracing::debug!("Establishing connection to key-value store..."); + + pool.init() + .await + .map_err(|e| ErrorType::KeyValStore(e.to_string())) + .err_context("Error connecting to key-value store")?; + + Ok(pool) +} diff --git a/src/server/mod.rs b/src/server/mod.rs index 550751f..83e024a 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,5 +1,6 @@ pub mod config; pub mod database; +pub mod key_val_store; pub mod main; pub use main::main;