Redis config
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
use crate::domain::SubscriberEmail;
|
||||
use anyhow::Context;
|
||||
use secrecy::{ExposeSecret, SecretString};
|
||||
use serde::Deserialize;
|
||||
use serde_aux::field_attributes::deserialize_number_from_string;
|
||||
use sqlx::postgres::{PgConnectOptions, PgSslMode};
|
||||
use tower_sessions_redis_store::{
|
||||
RedisStore,
|
||||
fred::prelude::{ClientLike, Pool},
|
||||
};
|
||||
|
||||
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
|
||||
let base_path = std::env::current_dir().expect("Failed to determine the current directory");
|
||||
@@ -60,7 +65,7 @@ pub struct Settings {
|
||||
pub application: ApplicationSettings,
|
||||
pub database: DatabaseSettings,
|
||||
pub email_client: EmailClientSettings,
|
||||
pub redis_uri: SecretString,
|
||||
pub kv_store: RedisSettings,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
@@ -100,6 +105,35 @@ impl EmailClientSettings {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct RedisSettings {
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
}
|
||||
|
||||
impl RedisSettings {
|
||||
pub fn connection_string(&self) -> String {
|
||||
format!("redis://{}:{}", self.host, self.port)
|
||||
}
|
||||
|
||||
pub async fn session_store(&self) -> Result<RedisStore<Pool>, anyhow::Error> {
|
||||
let pool = Pool::new(
|
||||
tower_sessions_redis_store::fred::prelude::Config::from_url(&self.connection_string())
|
||||
.context("Failed to parse Redis URL string.")?,
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
6,
|
||||
)
|
||||
.unwrap();
|
||||
pool.connect();
|
||||
pool.wait_for_connect()
|
||||
.await
|
||||
.context("Failed to connect to the Redis server.")?;
|
||||
Ok(RedisStore::new(pool))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct DatabaseSettings {
|
||||
pub username: String,
|
||||
|
||||
Reference in New Issue
Block a user