use crate::app::context::AppContext;
use crate::app::App;
use crate::error::RoadsterResult;
use async_trait::async_trait;
use axum_core::extract::FromRef;
use std::any::Any;
use tokio_util::sync::CancellationToken;
pub mod function;
#[cfg(feature = "grpc")]
pub mod grpc;
#[cfg(feature = "http")]
pub mod http;
pub mod registry;
pub(crate) mod runner;
pub mod worker;
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait AppService<A, S>: Send + Sync + AppServiceAsAny<A, S>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
A: App<S> + 'static,
{
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn before_run(&self, _state: &S) -> RoadsterResult<()> {
Ok(())
}
async fn run(self: Box<Self>, state: &S, cancel_token: CancellationToken)
-> RoadsterResult<()>;
}
#[cfg_attr(test, mockall::automock)]
#[async_trait]
pub trait AppServiceBuilder<A, S, Service>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
A: App<S> + 'static,
Service: AppService<A, S>,
{
fn name(&self) -> String;
fn enabled(&self, state: &S) -> bool;
async fn build(self, state: &S) -> RoadsterResult<Service>;
}
pub trait AppServiceAsAny<A, S>
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
A: App<S> + 'static,
{
fn as_any(&self) -> &dyn Any;
}
impl<T, A, S> AppServiceAsAny<A, S> for T
where
S: Clone + Send + Sync + 'static,
AppContext: FromRef<S>,
A: App<S> + 'static,
T: AppService<A, S> + 'static,
{
fn as_any(&self) -> &dyn Any {
self
}
}