pub(crate) use crate::foundation::Application;
use crate::{
configuration::{Middleware, Routing},
console, http,
routing::Router,
support::ServiceProvider,
};
use std::{any::type_name, sync::Arc};
#[derive(Default)]
pub struct Scheduler {}
type RoutingCallback = Box<dyn FnOnce(&mut Router)>;
pub struct ApplicationBuilder {
app: Application,
routing_callbacks: Vec<RoutingCallback>,
}
impl ApplicationBuilder {
pub fn new(app: Application) -> Self {
Self {
app,
routing_callbacks: Vec::new(),
}
}
pub fn with_provider<P: ServiceProvider + 'static>(mut self, provider: P) -> Self {
self.app.register_provider(provider);
self
}
pub fn with_providers(mut self, mut providers: Vec<Box<dyn ServiceProvider>>) -> Self {
self.app.providers.append(&mut providers);
self
}
pub fn with_routing<F>(mut self, callback: F) -> Self
where
F: FnOnce(&mut Routing) + 'static,
{
let mut routing = Routing::new();
callback(&mut routing);
self.routing_callbacks.push(Box::new(move |router| {
routing.register(router);
}));
self
}
pub fn with_events(self) -> Self {
todo!("Not implemented");
}
pub fn with_commands(self) -> Self {
todo!("Not implemented");
}
pub fn with_kernels(self) -> Self {
self.app.container.singleton(|_| console::Kernel::default());
self.app.container.singleton(|c| {
http::Kernel::new(c.make::<Router>().expect("Router Not found in Container"))
});
self
}
pub fn with_middleware<F>(self, callback: F) -> Self
where
F: FnOnce(&mut Middleware) + Send + Sync + 'static,
{
let callback_cell = std::sync::Mutex::new(Some(callback));
self.app
.container
.after_resolving::<http::Kernel, _>(move |kernel, _| {
if let Some(cb) = callback_cell.lock().unwrap().take() {
kernel.configure_middleware(cb);
}
});
self
}
pub fn with_scheduler<F>(self, _callback: F) -> Self
where
F: FnOnce(&mut Scheduler),
{
todo!("Not yet implemented");
}
pub fn create(mut self) -> Application {
let kernel = self
.app
.make::<http::Kernel>()
.unwrap_or_else(|_| panic!("{:?} must be registered", type_name::<http::Kernel>()));
let mut router = (*kernel.get_router()).clone();
for callback in self.routing_callbacks.drain(..) {
callback(&mut router);
}
let router_arc = Arc::new(router);
kernel.set_router(Arc::clone(&router_arc));
self.app.container.instance((*router_arc).clone());
self.app
}
}