use async_trait::async_trait;
use axum::Router;
use tokio_util::sync::CancellationToken;
pub use crate::api::openapi_registry::OpenApiRegistry;
#[async_trait]
pub trait SystemCapability: Send + Sync {
fn pre_init(&self, _sys: &crate::runtime::SystemContext) -> anyhow::Result<()> {
Ok(())
}
async fn post_init(&self, _sys: &crate::runtime::SystemContext) -> anyhow::Result<()> {
Ok(())
}
}
#[async_trait]
pub trait Module: Send + Sync + 'static {
async fn init(&self, ctx: &crate::context::ModuleCtx) -> anyhow::Result<()>;
}
#[cfg(feature = "db")]
pub trait DatabaseCapability: Send + Sync {
fn migrations(&self) -> Vec<Box<dyn sea_orm_migration::MigrationTrait>>;
}
pub trait RestApiCapability: Send + Sync {
fn register_rest(
&self,
ctx: &crate::context::ModuleCtx,
router: Router,
openapi: &dyn OpenApiRegistry,
) -> anyhow::Result<Router>;
}
#[allow(dead_code)]
pub trait ApiGatewayCapability: Send + Sync + 'static {
fn rest_prepare(
&self,
ctx: &crate::context::ModuleCtx,
router: Router,
) -> anyhow::Result<Router>;
fn rest_finalize(
&self,
ctx: &crate::context::ModuleCtx,
router: Router,
) -> anyhow::Result<Router>;
fn as_registry(&self) -> &dyn OpenApiRegistry;
}
#[async_trait]
pub trait RunnableCapability: Send + Sync {
async fn start(&self, cancel: CancellationToken) -> anyhow::Result<()>;
async fn stop(&self, deadline_token: CancellationToken) -> anyhow::Result<()>;
}
#[cfg(feature = "otel")]
pub struct RegisterGrpcServiceFn {
pub service_name: &'static str,
pub register: Box<dyn Fn(&mut tonic::service::RoutesBuilder) + Send + Sync>,
}
#[cfg(not(feature = "otel"))]
pub struct RegisterGrpcServiceFn {
pub service_name: &'static str,
}
#[async_trait]
pub trait GrpcServiceCapability: Send + Sync {
async fn get_grpc_services(
&self,
ctx: &crate::context::ModuleCtx,
) -> anyhow::Result<Vec<RegisterGrpcServiceFn>>;
}
pub trait GrpcHubCapability: Send + Sync {
fn bound_endpoint(&self) -> Option<String>;
}