feat: 初始化ERP平台底座项目结构
- 添加基础crate结构(erp-core, erp-common) - 实现核心模块trait和事件总线 - 配置Docker开发环境(PostgreSQL+Redis) - 添加Tauri桌面端基础框架 - 设置CI/CD工作流 - 编写项目协作规范文档(CLAUDE.md)
This commit is contained in:
76
crates/erp-core/src/module.rs
Normal file
76
crates/erp-core/src/module.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use axum::Router;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::error::AppResult;
|
||||
use crate::events::EventBus;
|
||||
|
||||
/// 模块注册接口
|
||||
/// 所有业务模块(Auth, Workflow, Message, Config, 行业模块)都实现此 trait
|
||||
pub trait ErpModule: Send + Sync {
|
||||
/// 模块名称(唯一标识)
|
||||
fn name(&self) -> &str;
|
||||
|
||||
/// 模块版本
|
||||
fn version(&self) -> &str {
|
||||
env!("CARGO_PKG_VERSION")
|
||||
}
|
||||
|
||||
/// 依赖的其他模块名称
|
||||
fn dependencies(&self) -> Vec<&str> {
|
||||
vec![]
|
||||
}
|
||||
|
||||
/// 注册 Axum 路由
|
||||
fn register_routes(&self, router: Router) -> Router;
|
||||
|
||||
/// 注册事件处理器
|
||||
fn register_event_handlers(&self, _bus: &EventBus) {}
|
||||
|
||||
/// 租户创建时的初始化钩子
|
||||
fn on_tenant_created(
|
||||
&self,
|
||||
_tenant_id: Uuid,
|
||||
) -> impl std::future::Future<Output = AppResult<()>> + Send {
|
||||
async { Ok(()) }
|
||||
}
|
||||
|
||||
/// 租户删除时的清理钩子
|
||||
fn on_tenant_deleted(
|
||||
&self,
|
||||
_tenant_id: Uuid,
|
||||
) -> impl std::future::Future<Output = AppResult<()>> + Send {
|
||||
async { Ok(()) }
|
||||
}
|
||||
}
|
||||
|
||||
/// 模块注册器
|
||||
pub struct ModuleRegistry {
|
||||
modules: Vec<Box<dyn ErpModule>>,
|
||||
}
|
||||
|
||||
impl ModuleRegistry {
|
||||
pub fn new() -> Self {
|
||||
Self { modules: vec![] }
|
||||
}
|
||||
|
||||
pub fn register(&mut self, module: Box<dyn ErpModule>) {
|
||||
tracing::info!(module = module.name(), version = module.version(), "Module registered");
|
||||
self.modules.push(module);
|
||||
}
|
||||
|
||||
pub fn build_router(&self, base: Router) -> Router {
|
||||
self.modules
|
||||
.iter()
|
||||
.fold(base, |router, m| m.register_routes(router))
|
||||
}
|
||||
|
||||
pub fn register_handlers(&self, bus: &EventBus) {
|
||||
for module in &self.modules {
|
||||
module.register_event_handlers(bus);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn modules(&self) -> &[Box<dyn ErpModule>] {
|
||||
&self.modules
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user