rust_webx_core/route/
ext.rs1use crate::handler::IHostedService;
4use crate::mediator::Mediator;
5use crate::middleware::IMiddleware;
6use crate::pipeline::IPipelineBehavior;
7use rust_dix::{ServiceCollection, ServiceLifetime};
8use std::sync::Arc;
9
10pub trait IServiceCollectionExt: Sized {
13 fn add_mediator(self) -> Self;
18
19 fn add_request_endpoints(self) -> Self;
23
24 fn add_controllers(self) -> Self;
26
27 fn add_middleware<T>(self) -> Self
29 where
30 T: IMiddleware + Default + Send + Sync + 'static;
31
32 fn add_pipeline<T>(self) -> Self
34 where
35 T: IPipelineBehavior + Default + Send + Sync + 'static;
36
37 fn add_hosted_service<T>(self) -> Self
56 where
57 T: IHostedService + Default + Send + Sync + 'static;
58}
59
60impl IServiceCollectionExt for ServiceCollection {
61 fn add_mediator(self) -> Self {
62 MEDIATOR_ACTIVE.store(true, std::sync::atomic::Ordering::SeqCst);
65 self.transient::<Mediator>(|resolver| {
66 let provider = resolver
67 .provider_arc()
68 .expect("ServiceProvider not available from DI resolver");
69 Arc::new(Mediator::new(provider))
70 })
71 }
72
73 fn add_request_endpoints(self) -> Self {
74 ENDPOINT_SCAN.store(true, std::sync::atomic::Ordering::SeqCst);
77 self
78 }
79
80 fn add_controllers(self) -> Self {
81 self
82 }
83
84 fn add_middleware<T>(self) -> Self
85 where
86 T: IMiddleware + Default + Send + Sync + 'static,
87 {
88 self.singleton::<dyn IMiddleware>(|_| Arc::new(T::default()))
89 }
90
91 fn add_pipeline<T>(self) -> Self
92 where
93 T: IPipelineBehavior + Default + Send + Sync + 'static,
94 {
95 self.add(ServiceLifetime::Singleton, |_| {
96 Arc::new(T::default()) as Arc<dyn IPipelineBehavior>
97 })
98 }
99
100 fn add_hosted_service<T>(self) -> Self
101 where
102 T: IHostedService + Default + Send + Sync + 'static,
103 {
104 self.singleton::<dyn IHostedService>(|_| Arc::new(T::default()))
105 }
106}
107
108use std::sync::atomic::AtomicBool;
110static ENDPOINT_SCAN: AtomicBool = AtomicBool::new(false);
111static MEDIATOR_ACTIVE: AtomicBool = AtomicBool::new(false);
112
113pub fn should_scan_endpoints() -> bool {
114 ENDPOINT_SCAN.load(std::sync::atomic::Ordering::SeqCst)
115}
116
117pub fn is_mediator_active() -> bool {
118 MEDIATOR_ACTIVE.load(std::sync::atomic::Ordering::SeqCst)
119}