暴雨地震灾害链HTTP请求
This commit is contained in:
@@ -51,12 +51,30 @@ class AppLauncher:
|
||||
|
||||
def start():
|
||||
"""启动应用服务"""
|
||||
import threading
|
||||
from config import settings
|
||||
from app.core.rainfall_manager import rainfall_manager
|
||||
from app.utils.logger import get_logger
|
||||
from app.utils.thread_pool_manager import block_main_thread, thread_pool_manager
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
# 启动 FastAPI 服务(守护线程)
|
||||
def run_api_server():
|
||||
import uvicorn
|
||||
from app.core.server import create_app
|
||||
api_app = create_app()
|
||||
uvicorn.run(
|
||||
api_app,
|
||||
host=getattr(settings, "API_HOST", "127.0.0.1"),
|
||||
port=int(getattr(settings, "API_PORT", 8082)),
|
||||
log_level="info",
|
||||
)
|
||||
|
||||
api_thread = threading.Thread(target=run_api_server, daemon=True, name="api-server")
|
||||
api_thread.start()
|
||||
logger.info(f"FastAPI 服务已启动: http://{getattr(settings, 'API_HOST', '127.0.0.1')}:{getattr(settings, 'API_PORT', 8082)}")
|
||||
|
||||
# 启动降雨站点监测
|
||||
logger.info("启动降雨站点监测服务...")
|
||||
rainfall_manager.monitoring_rainfall_station_id('2025-09-16 20:00:00')
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
"""
|
||||
FastAPI 服务创建与配置
|
||||
"""
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
|
||||
from app.utils.api_deps import get_rainfall_model, get_earthquake_model, is_model_loaded
|
||||
from app.schemas.api_schemas import HealthResponse
|
||||
from app.config.paths import get_logger
|
||||
|
||||
logger = get_logger("api")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
"""应用生命周期:启动时预加载模型"""
|
||||
logger.info("正在预加载DBN模型...")
|
||||
get_rainfall_model()
|
||||
get_earthquake_model()
|
||||
logger.info("DBN模型预加载完成")
|
||||
yield
|
||||
logger.info("应用关闭")
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""创建 FastAPI 应用实例"""
|
||||
application = FastAPI(
|
||||
title="西安灾害链预测服务",
|
||||
description="基于动态贝叶斯网络的暴雨/地震灾害链预测API",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
)
|
||||
|
||||
@application.middleware("http")
|
||||
async def log_requests(request: Request, call_next):
|
||||
"""请求日志中间件"""
|
||||
start = time.time()
|
||||
response = await call_next(request)
|
||||
elapsed = time.time() - start
|
||||
logger.info(f"{request.method} {request.url.path} -> {response.status_code} ({elapsed:.3f}s)")
|
||||
return response
|
||||
|
||||
# 注册路由
|
||||
from app.api import register_routers
|
||||
register_routers(application)
|
||||
|
||||
@application.get("/health", response_model=HealthResponse, tags=["系统"])
|
||||
async def health_check():
|
||||
"""健康检查"""
|
||||
status = is_model_loaded()
|
||||
return HealthResponse(status="ok", **status)
|
||||
|
||||
return application
|
||||
Reference in New Issue
Block a user