30 lines
924 B
Rust
30 lines
924 B
Rust
use std::time::Duration;
|
|
|
|
use anyhow::Context;
|
|
use redis::Client as RedisClient;
|
|
use sqlx::{postgres::PgPoolOptions, PgPool};
|
|
|
|
pub async fn connect_postgres(database_url: Option<&str>) -> anyhow::Result<Option<PgPool>> {
|
|
match database_url {
|
|
Some(url) if !url.trim().is_empty() => {
|
|
let pool = PgPoolOptions::new()
|
|
.max_connections(5)
|
|
.acquire_timeout(Duration::from_secs(5))
|
|
.connect(url)
|
|
.await
|
|
.with_context(|| format!("failed to connect to postgres at {url}"))?;
|
|
Ok(Some(pool))
|
|
}
|
|
_ => Ok(None),
|
|
}
|
|
}
|
|
|
|
pub fn connect_redis(redis_url: Option<&str>) -> anyhow::Result<Option<RedisClient>> {
|
|
match redis_url {
|
|
Some(url) if !url.trim().is_empty() => {
|
|
Ok(Some(RedisClient::open(url).context("failed to open redis client")?))
|
|
}
|
|
_ => Ok(None),
|
|
}
|
|
}
|