本次提交完成了一项重要的架构重构,将所有外部服务的API凭证管理从环境变量迁移到了中心化的数据库配置中。
主要变更:
1. **统一配置源**:
- `data-persistence-service` 现已提供 `/api/v1/configs/data_sources` 端点,用于统一管理数据源配置。
- 所有配置(LLM 和数据源)现在都通过数据库的 `system_config` 表进行管理,实现了“单一事实源”。
2. **增强服务韧性**:
- 重构了 `finnhub-`, `tushare-`, `alphavantage-provider-service`。
- 这些服务在启动时不再强制要求 API Key。
- 引入了动态配置轮询器 (`config_poller`),服务现在可以定期从数据库获取最新配置。
- 实现了“降级模式”:当配置缺失时,服务会进入 `Degraded` 状态并暂停处理消息,而不是直接崩溃。配置恢复后,服务会自动回到 `Active` 状态。
- `/health` 端点现在能准确反映服务的真实运行状态。
3. **前端易用性提升**:
- 您在 `/config` 页面上增加了“数据源配置”面板,允许用户通过 UI 动态更新所有 API Token。
4. **部署简化**:
- 从 `docker-compose.yml` 中移除了所有已废弃的 `_API_KEY` 环境变量,消除了启动时的警告。
这项重构显著提升了系统的可维护性、健壮性和用户体验,为未来的功能扩展奠定了坚实的基础。
76 lines
2.6 KiB
Rust
76 lines
2.6 KiB
Rust
use axum::{extract::State, Json};
|
|
use common_contracts::config_models::{
|
|
AnalysisModulesConfig, DataSourceConfig, LlmProvidersConfig,
|
|
};
|
|
use service_kit::api;
|
|
use std::collections::HashMap;
|
|
|
|
use crate::{db::system_config, AppState, ServerError};
|
|
|
|
#[api(GET, "/api/v1/configs/llm_providers", output(detail = "LlmProvidersConfig"))]
|
|
pub async fn get_llm_providers_config(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<LlmProvidersConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let config = system_config::get_config::<LlmProvidersConfig>(pool, "llm_providers").await?;
|
|
Ok(Json(config))
|
|
}
|
|
|
|
#[api(PUT, "/api/v1/configs/llm_providers", output(detail = "LlmProvidersConfig"))]
|
|
pub async fn update_llm_providers_config(
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<LlmProvidersConfig>,
|
|
) -> Result<Json<LlmProvidersConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let updated_config = system_config::update_config(pool, "llm_providers", &payload).await?;
|
|
Ok(Json(updated_config))
|
|
}
|
|
|
|
#[api(GET, "/api/v1/configs/analysis_modules", output(detail = "AnalysisModulesConfig"))]
|
|
pub async fn get_analysis_modules_config(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<AnalysisModulesConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let config = system_config::get_config::<AnalysisModulesConfig>(pool, "analysis_modules").await?;
|
|
Ok(Json(config))
|
|
}
|
|
|
|
#[api(PUT, "/api/v1/configs/analysis_modules", output(detail = "AnalysisModulesConfig"))]
|
|
pub async fn update_analysis_modules_config(
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<AnalysisModulesConfig>,
|
|
) -> Result<Json<AnalysisModulesConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let updated_config = system_config::update_config(pool, "analysis_modules", &payload).await?;
|
|
Ok(Json(updated_config))
|
|
}
|
|
|
|
pub type DataSourcesConfig = HashMap<String, DataSourceConfig>;
|
|
|
|
#[api(
|
|
GET,
|
|
"/api/v1/configs/data_sources",
|
|
output(detail = "DataSourcesConfig")
|
|
)]
|
|
pub async fn get_data_sources_config(
|
|
State(state): State<AppState>,
|
|
) -> Result<Json<DataSourcesConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let config = system_config::get_config::<DataSourcesConfig>(pool, "data_sources").await?;
|
|
Ok(Json(config))
|
|
}
|
|
|
|
#[api(
|
|
PUT,
|
|
"/api/v1/configs/data_sources",
|
|
output(detail = "DataSourcesConfig")
|
|
)]
|
|
pub async fn update_data_sources_config(
|
|
State(state): State<AppState>,
|
|
Json(payload): Json<DataSourcesConfig>,
|
|
) -> Result<Json<DataSourcesConfig>, ServerError> {
|
|
let pool = state.pool();
|
|
let updated_config = system_config::update_config(pool, "data_sources", &payload).await?;
|
|
Ok(Json(updated_config))
|
|
}
|