109 lines
3.0 KiB
Python
109 lines
3.0 KiB
Python
|
|
"""
|
|||
|
|
Data Service FastAPI 应用
|
|||
|
|
|
|||
|
|
数据服务,提供:
|
|||
|
|
- 行情数据接收(从 QMT Adapter)
|
|||
|
|
- 行情数据分发(到 Strategy Engine)
|
|||
|
|
- K线数据存储与查询
|
|||
|
|
- 回测数据回放
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
from contextlib import asynccontextmanager
|
|||
|
|
from typing import AsyncIterator
|
|||
|
|
|
|||
|
|
from fastapi import FastAPI, Request
|
|||
|
|
from fastapi.responses import JSONResponse
|
|||
|
|
|
|||
|
|
from src.core.config import settings
|
|||
|
|
from src.core.logger import get_logger
|
|||
|
|
from src.core.database import db
|
|||
|
|
from src.core.redis_client import redis_client
|
|||
|
|
|
|||
|
|
from .routes import health, market, kline, backtest
|
|||
|
|
|
|||
|
|
logger = get_logger("data_service")
|
|||
|
|
|
|||
|
|
|
|||
|
|
@asynccontextmanager
|
|||
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|||
|
|
"""应用生命周期管理"""
|
|||
|
|
from .services.market_receiver import market_receiver
|
|||
|
|
from .services.kline_service import kline_service
|
|||
|
|
|
|||
|
|
logger.info("Data Service 启动中...")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
await redis_client.connect()
|
|||
|
|
logger.info("Redis 连接成功")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"Redis 连接失败: {e}")
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
await db.connect()
|
|||
|
|
logger.info("数据库连接成功")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"数据库连接失败: {e}")
|
|||
|
|
|
|||
|
|
# 启动行情接收器
|
|||
|
|
try:
|
|||
|
|
await market_receiver.start()
|
|||
|
|
logger.info("行情接收器已启动")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"行情接收器启动失败: {e}")
|
|||
|
|
|
|||
|
|
# 启动K线服务
|
|||
|
|
try:
|
|||
|
|
await kline_service.start()
|
|||
|
|
logger.info("K线服务已启动")
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.warning(f"K线服务启动失败: {e}")
|
|||
|
|
|
|||
|
|
logger.info("Data Service 启动完成")
|
|||
|
|
|
|||
|
|
yield
|
|||
|
|
|
|||
|
|
logger.info("Data Service 关闭中...")
|
|||
|
|
await market_receiver.stop()
|
|||
|
|
await kline_service.stop()
|
|||
|
|
await redis_client.disconnect()
|
|||
|
|
await db.disconnect()
|
|||
|
|
logger.info("Data Service 已关闭")
|
|||
|
|
|
|||
|
|
|
|||
|
|
def create_app() -> FastAPI:
|
|||
|
|
"""创建 FastAPI 应用实例"""
|
|||
|
|
|
|||
|
|
application = FastAPI(
|
|||
|
|
title="QTrade Data Service",
|
|||
|
|
description="量化交易系统数据服务",
|
|||
|
|
version=settings.app.version,
|
|||
|
|
lifespan=lifespan,
|
|||
|
|
docs_url="/docs" if settings.app.debug else None,
|
|||
|
|
redoc_url="/redoc" if settings.app.debug else None,
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
# 注册路由
|
|||
|
|
application.include_router(health.router, tags=["健康检查"])
|
|||
|
|
application.include_router(market.router, prefix="/market", tags=["行情"])
|
|||
|
|
application.include_router(kline.router, prefix="/kline", tags=["K线"])
|
|||
|
|
application.include_router(backtest.router, prefix="/backtest", tags=["回测"])
|
|||
|
|
|
|||
|
|
# 全局异常处理
|
|||
|
|
@application.exception_handler(Exception)
|
|||
|
|
async def global_exception_handler(request: Request, exc: Exception):
|
|||
|
|
logger.error(f"未处理的异常: {exc}", exc_info=True)
|
|||
|
|
return JSONResponse(
|
|||
|
|
status_code=500,
|
|||
|
|
content={
|
|||
|
|
"code": 500,
|
|||
|
|
"message": "服务器内部错误",
|
|||
|
|
"detail": str(exc) if settings.app.debug else None,
|
|||
|
|
},
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
return application
|
|||
|
|
|
|||
|
|
|
|||
|
|
# 创建应用实例
|
|||
|
|
app = create_app()
|