""" Config Service - provides read-only access to static configuration files. """ from __future__ import annotations import json import os from typing import Any, Dict from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware APP_NAME = "config-service" API_V1 = "/api/v1" # 在容器内挂载了项目根到 /workspace PROJECT_ROOT = os.environ.get("PROJECT_ROOT", "/workspace") CONFIG_DIR = os.path.join(PROJECT_ROOT, "config") SYSTEM_CONFIG_PATH = os.path.join(CONFIG_DIR, "config.json") ANALYSIS_CONFIG_PATH = os.path.join(CONFIG_DIR, "analysis-config.json") app = FastAPI(title=APP_NAME, version="0.1.0") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) def _read_json_file(path: str) -> Dict[str, Any]: if not os.path.exists(path): raise HTTPException(status_code=404, detail=f"配置文件不存在: {os.path.basename(path)}") try: with open(path, "r", encoding="utf-8") as f: return json.load(f) except json.JSONDecodeError as e: raise HTTPException(status_code=500, detail=f"配置文件格式错误: {e}") from e except OSError as e: raise HTTPException(status_code=500, detail=f"读取配置文件失败: {e}") from e @app.get("/") async def root() -> Dict[str, Any]: return {"status": "ok", "name": APP_NAME} @app.get(f"{API_V1}/system") async def get_system_config() -> Dict[str, Any]: """ 返回系统基础配置(纯文件内容,不包含数据库覆盖)。 """ return _read_json_file(SYSTEM_CONFIG_PATH) @app.get(f"{API_V1}/analysis-modules") async def get_analysis_modules() -> Dict[str, Any]: """ 返回分析模块配置(原样透传)。 """ return _read_json_file(ANALYSIS_CONFIG_PATH)