use crate::handler::IHostedService;
use crate::middleware::IMiddleware;
use crate::pipeline::IPipelineBehavior;
use lrdi::{ServiceCollection, ServiceLifetime};
use std::sync::Arc;
pub trait IServiceCollectionExt: Sized {
fn add_mediator(self) -> Self;
fn add_request_endpoints(self) -> Self;
fn add_controllers(self) -> Self;
fn add_middleware<T>(self) -> Self
where
T: IMiddleware + Default + Send + Sync + 'static;
fn add_pipeline<T>(self) -> Self
where
T: IPipelineBehavior + Default + Send + Sync + 'static;
fn add_hosted_service<T>(self) -> Self
where
T: IHostedService + Default + Send + Sync + 'static;
}
impl IServiceCollectionExt for ServiceCollection {
fn add_mediator(self) -> Self {
MEDIATOR_ACTIVE.store(true, std::sync::atomic::Ordering::SeqCst);
self
}
fn add_request_endpoints(self) -> Self {
ENDPOINT_SCAN.store(true, std::sync::atomic::Ordering::SeqCst);
self
}
fn add_controllers(self) -> Self {
self
}
fn add_middleware<T>(self) -> Self
where
T: IMiddleware + Default + Send + Sync + 'static,
{
self.singleton::<dyn IMiddleware>(|_| Arc::new(T::default()))
}
fn add_pipeline<T>(self) -> Self
where
T: IPipelineBehavior + Default + Send + Sync + 'static,
{
self.add(ServiceLifetime::Singleton, |_| {
Arc::new(T::default()) as Arc<dyn IPipelineBehavior>
})
}
fn add_hosted_service<T>(self) -> Self
where
T: IHostedService + Default + Send + Sync + 'static,
{
self.singleton::<dyn IHostedService>(|_| Arc::new(T::default()))
}
}
use std::sync::atomic::AtomicBool;
static ENDPOINT_SCAN: AtomicBool = AtomicBool::new(false);
static MEDIATOR_ACTIVE: AtomicBool = AtomicBool::new(false);
pub fn should_scan_endpoints() -> bool {
ENDPOINT_SCAN.load(std::sync::atomic::Ordering::SeqCst)
}
pub fn is_mediator_active() -> bool {
MEDIATOR_ACTIVE.load(std::sync::atomic::Ordering::SeqCst)
}