first
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user