use std::sync::Arc;
use crate::{Environment, config::Config, health::Checks, store::Store};
#[cfg(feature = "sea-orm")]
pub type Db = sea_orm::DatabaseConnection;
#[cfg(all(feature = "sqlx", not(feature = "sea-orm")))]
pub type Db = axa_sqlx::sqlx::PgPool;
#[cfg(not(any(feature = "sea-orm", feature = "sqlx")))]
pub type Db = ();
#[cfg(all(test, feature = "sea-orm"))]
fn test_db() -> Db {
sea_orm::DatabaseConnection::default()
}
#[cfg(all(test, feature = "sqlx", not(feature = "sea-orm")))]
fn test_db() -> Db {
static RUNTIME: std::sync::LazyLock<tokio::runtime::Runtime> =
std::sync::LazyLock::new(|| tokio::runtime::Runtime::new().expect("a runtime"));
let _entered = RUNTIME.enter();
axa_sqlx::sqlx::postgres::PgPoolOptions::new()
.acquire_timeout(std::time::Duration::from_millis(50))
.connect_lazy("postgres://axa:axa@127.0.0.1:1/axa")
.expect("a well-formed address")
}
#[cfg(all(test, not(any(feature = "sea-orm", feature = "sqlx"))))]
fn test_db() -> Db {}
#[derive(Clone, Debug)]
pub struct AppContext {
pub environment: Environment,
pub db: Db,
pub config: Arc<Config>,
pub shared_store: Arc<Store>,
pub health_checks: Checks,
#[cfg(feature = "queue")]
pub queue_provider: Option<Arc<crate::queue::Queue>>,
}
impl AppContext {
#[must_use]
pub fn new(environment: Environment, db: Db, config: Config) -> Self {
Self {
environment,
db,
config: Arc::new(config),
shared_store: Arc::new(Store::default()),
health_checks: Checks::new(),
#[cfg(feature = "queue")]
queue_provider: None,
}
}
#[cfg(test)]
#[allow(clippy::unit_arg, dead_code)]
pub(crate) fn for_test(environment: Environment, config: Config) -> Self {
Self::new(environment, test_db(), config)
}
}