- 新增 AiConfig 到 AppConfig - 新增 FromRef<AppState> for AiState - 注册 AiModule 到 ModuleRegistry - 合并 AI protected routes - 修复 sync_module_permissions 只同步 health.% 的 bug Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
146 lines
4.8 KiB
Rust
146 lines
4.8 KiB
Rust
use axum::extract::FromRef;
|
||
use sea_orm::DatabaseConnection;
|
||
|
||
use crate::config::AppConfig;
|
||
use erp_core::events::EventBus;
|
||
use erp_core::module::ModuleRegistry;
|
||
|
||
/// Axum shared application state.
|
||
/// All handlers access database connections, configuration, etc. through `State<AppState>`.
|
||
#[derive(Clone)]
|
||
pub struct AppState {
|
||
pub db: DatabaseConnection,
|
||
pub config: AppConfig,
|
||
pub event_bus: EventBus,
|
||
pub module_registry: ModuleRegistry,
|
||
pub redis: redis::Client,
|
||
/// 实际的默认租户 ID,从数据库种子数据中获取。
|
||
pub default_tenant_id: uuid::Uuid,
|
||
/// 插件引擎
|
||
pub plugin_engine: erp_plugin::engine::PluginEngine,
|
||
/// 插件实体缓存
|
||
pub plugin_entity_cache: moka::sync::Cache<String, erp_plugin::state::EntityInfo>,
|
||
}
|
||
|
||
/// Allow handlers to extract `DatabaseConnection` directly from `State<AppState>`.
|
||
impl FromRef<AppState> for DatabaseConnection {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
state.db.clone()
|
||
}
|
||
}
|
||
|
||
/// Allow handlers to extract `EventBus` directly from `State<AppState>`.
|
||
impl FromRef<AppState> for EventBus {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
state.event_bus.clone()
|
||
}
|
||
}
|
||
|
||
/// Allow erp-auth handlers to extract their required state without depending on erp-server.
|
||
///
|
||
/// This bridges the gap: erp-auth defines `AuthState` with the fields it needs,
|
||
/// and erp-server fills them from `AppState`.
|
||
impl FromRef<AppState> for erp_auth::AuthState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
use erp_auth::auth_state::parse_ttl;
|
||
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
jwt_secret: state.config.jwt.secret.clone(),
|
||
access_ttl_secs: parse_ttl(&state.config.jwt.access_token_ttl),
|
||
refresh_ttl_secs: parse_ttl(&state.config.jwt.refresh_token_ttl),
|
||
default_tenant_id: state.default_tenant_id,
|
||
wechat_appid: state.config.wechat.appid.clone(),
|
||
wechat_secret: state.config.wechat.secret.clone(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-config handlers to extract their required state without depending on erp-server.
|
||
impl FromRef<AppState> for erp_config::ConfigState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-workflow handlers to extract their required state without depending on erp-server.
|
||
impl FromRef<AppState> for erp_workflow::WorkflowState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-message handlers to extract their required state without depending on erp-server.
|
||
impl FromRef<AppState> for erp_message::MessageState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-plugin handlers to extract their required state.
|
||
impl FromRef<AppState> for erp_plugin::state::PluginState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
engine: state.plugin_engine.clone(),
|
||
entity_cache: state.plugin_entity_cache.clone(),
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-health handlers to extract their required state.
|
||
impl FromRef<AppState> for erp_health::HealthState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
let crypto = erp_health::HealthCrypto::from_keys(
|
||
&state.config.health.aes_key,
|
||
&state.config.health.hmac_key,
|
||
)
|
||
.expect("Health encryption keys must be valid 32-byte hex strings. Set ERP__HEALTH__AES_KEY and ERP__HEALTH__HMAC_KEY");
|
||
|
||
Self {
|
||
db: state.db.clone(),
|
||
event_bus: state.event_bus.clone(),
|
||
crypto,
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Allow erp-ai handlers to extract their required state.
|
||
impl FromRef<AppState> for erp_ai::AiState {
|
||
fn from_ref(state: &AppState) -> Self {
|
||
let mut provider = erp_ai::provider::claude::ClaudeProvider::new(
|
||
state.config.ai.api_key.clone(),
|
||
);
|
||
if let Some(ref base_url) = state.config.ai.base_url {
|
||
provider = provider.with_base_url(base_url.clone());
|
||
}
|
||
let db = state.db.clone();
|
||
let event_bus = state.event_bus.clone();
|
||
|
||
let analysis = std::sync::Arc::new(
|
||
erp_ai::service::analysis::AnalysisService::new(Box::new(provider), db.clone()),
|
||
);
|
||
let prompt = std::sync::Arc::new(erp_ai::service::prompt::PromptService::new(db.clone()));
|
||
let usage = std::sync::Arc::new(erp_ai::service::usage::UsageService::new(db.clone()));
|
||
|
||
Self {
|
||
db,
|
||
event_bus,
|
||
analysis,
|
||
prompt,
|
||
usage,
|
||
}
|
||
}
|
||
}
|