use std::future::Future;
use axum::{Router, handler::Handler, routing};
use crate::{
AppConfig,
task::{self, Cleanup, Worker, WorkerContext},
};
type ConfigOverride = Box<dyn FnOnce(&mut AppConfig) + Send>;
pub(crate) struct Registration {
pub name: String,
pub metrics: crate::metrics::Registry,
pub config: AppConfig,
pub environment: bool,
pub overrides: Vec<ConfigOverride>,
pub workers: Vec<Worker>,
pub cleanups: Vec<Cleanup>,
pub checks: Vec<crate::health::Check>,
pub http: bool,
pub startup_error: Option<crate::Error>,
pub setup: Option<crate::setup::Factory>,
}
pub struct App<S = ()> {
pub(crate) router: Router<S>,
pub(crate) registration: Registration,
}
impl<S> App<S>
where
S: Clone + Send + Sync + 'static,
{
pub fn new(name: impl Into<String>) -> Self {
Self {
router: Router::new(),
registration: Registration {
name: name.into(),
metrics: crate::metrics::Registry::owned(),
config: AppConfig::default(),
environment: true,
overrides: Vec::new(),
workers: Vec::new(),
cleanups: Vec::new(),
checks: Vec::new(),
http: false,
startup_error: None,
setup: None,
},
}
}
pub fn metrics(mut self, metrics: crate::metrics::Registry) -> Self {
match metrics.attach() {
Ok(()) => self.registration.metrics = metrics,
Err(error) => {
self.registration.startup_error =
Some(crate::Error::Registration(error.to_string()));
}
}
self
}
pub fn config(mut self, config: AppConfig) -> Self {
self.registration.config = config;
self.registration.environment = false;
self
}
pub fn configure(
mut self,
override_config: impl FnOnce(&mut AppConfig) + Send + 'static,
) -> Self {
self.registration.overrides.push(Box::new(override_config));
self
}
pub fn get<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
self.router = self.router.route(path, routing::get(handler));
self.registration.http = true;
self
}
pub fn post<H, T>(mut self, path: &str, handler: H) -> Self
where
H: Handler<T, S>,
T: 'static,
{
self.router = self.router.route(path, routing::post(handler));
self.registration.http = true;
self
}
pub fn http(mut self, router: Router<S>) -> Self {
self.router = self.router.merge(router);
self.registration.http = true;
self
}
pub fn state(self, state: S) -> App<()> {
App {
router: self.router.with_state(state),
registration: self.registration,
}
}
pub fn worker<F, Fut>(mut self, name: impl Into<String>, factory: F) -> Self
where
F: FnOnce(WorkerContext) -> Fut + Send + 'static,
Fut: Future<Output = task::Result> + Send + 'static,
{
self.registration.workers.push(Worker {
name: name.into(),
factory: Box::new(move |context| Box::pin(factory(context))),
});
self
}
pub fn readiness<F, Fut>(
mut self,
name: impl Into<String>,
policy: crate::health::CheckPolicy,
check: F,
) -> Self
where
F: Fn() -> Fut + Send + 'static,
Fut: Future<Output = task::Result> + Send + 'static,
{
self.registration
.checks
.push(crate::health::Check::new(name.into(), policy, check));
self
}
pub fn cleanup<F, Fut>(mut self, name: impl Into<String>, factory: F) -> Self
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = task::Result> + Send + 'static,
{
self.registration.cleanups.push(Cleanup {
name: name.into(),
factory: Box::new(move || Box::pin(factory())),
});
self
}
}