125 lines
3.6 KiB
Python
125 lines
3.6 KiB
Python
|
|
"""
|
||
|
|
FastAPI 应用主入口
|
||
|
|
时间序列数据分析系统 FastAPI 版本
|
||
|
|
"""
|
||
|
|
|
||
|
|
import logging
|
||
|
|
from contextlib import asynccontextmanager
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
from fastapi.middleware.gzip import GZipMiddleware
|
||
|
|
|
||
|
|
from app.core.config import settings, setup_logging, ENVIRONMENT, DEBUG
|
||
|
|
from app.services.font_manager import setup_fonts_for_app
|
||
|
|
from app.services.linux_adapter import init_linux_environment
|
||
|
|
|
||
|
|
# 设置日志
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
# 应用生命周期
|
||
|
|
@asynccontextmanager
|
||
|
|
async def lifespan(app: FastAPI):
|
||
|
|
"""应用生命周期管理"""
|
||
|
|
# 启动时
|
||
|
|
logger.info("=" * 60)
|
||
|
|
logger.info(f"应用启动: {settings.APP_TITLE}")
|
||
|
|
logger.info(f"版本: {settings.APP_VERSION}")
|
||
|
|
logger.info(f"环境: {ENVIRONMENT}")
|
||
|
|
logger.info(f"调试: {DEBUG}")
|
||
|
|
logger.info(f"监听: {settings.HOST}:{settings.PORT}")
|
||
|
|
logger.info("=" * 60)
|
||
|
|
|
||
|
|
# 初始化 Linux 环境
|
||
|
|
try:
|
||
|
|
init_linux_environment()
|
||
|
|
except Exception as e:
|
||
|
|
logger.warning(f"Linux 环境初始化失败: {e}")
|
||
|
|
|
||
|
|
# 初始化字体
|
||
|
|
try:
|
||
|
|
fonts_config = setup_fonts_for_app(['zh', 'en'])
|
||
|
|
logger.info(f"字体配置完成: {fonts_config}")
|
||
|
|
except Exception as e:
|
||
|
|
logger.error(f"字体配置失败: {e}")
|
||
|
|
|
||
|
|
yield
|
||
|
|
|
||
|
|
# 关闭时
|
||
|
|
logger.info("应用关闭")
|
||
|
|
|
||
|
|
|
||
|
|
# 创建 FastAPI 应用
|
||
|
|
app = FastAPI(
|
||
|
|
title=settings.APP_TITLE,
|
||
|
|
description=settings.APP_DESCRIPTION,
|
||
|
|
version=settings.APP_VERSION,
|
||
|
|
lifespan=lifespan
|
||
|
|
)
|
||
|
|
|
||
|
|
# 添加中间件
|
||
|
|
# CORS 中间件
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=settings.CORS_ORIGINS,
|
||
|
|
allow_credentials=settings.CORS_ALLOW_CREDENTIALS,
|
||
|
|
allow_methods=settings.CORS_ALLOW_METHODS,
|
||
|
|
allow_headers=settings.CORS_ALLOW_HEADERS,
|
||
|
|
)
|
||
|
|
|
||
|
|
# 压缩中间件
|
||
|
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||
|
|
|
||
|
|
# 导入和包含路由
|
||
|
|
from app.api.routes import upload, analysis, analysis_v2, files
|
||
|
|
|
||
|
|
# v2 模式:仅暴露 v2 分析接口 + 基础状态接口
|
||
|
|
if settings.API_MODE == "v2":
|
||
|
|
logger.info("API_MODE=v2: 禁用 v1 上传/文件接口,仅启用 /api/v2")
|
||
|
|
app.include_router(analysis_v2.router, prefix="/api/v2", tags=["analysis-v2"])
|
||
|
|
else:
|
||
|
|
app.include_router(upload.router, prefix="/api", tags=["upload"])
|
||
|
|
app.include_router(analysis.router, prefix="/api", tags=["analysis"])
|
||
|
|
app.include_router(analysis_v2.router, prefix="/api/v2", tags=["analysis-v2"])
|
||
|
|
app.include_router(files.router, prefix="/api", tags=["files"])
|
||
|
|
|
||
|
|
# 根路由
|
||
|
|
@app.get("/")
|
||
|
|
async def root():
|
||
|
|
"""根路径"""
|
||
|
|
return {
|
||
|
|
"message": "Lazy Stat Backend API",
|
||
|
|
"version": settings.APP_VERSION,
|
||
|
|
"docs": "/docs"
|
||
|
|
}
|
||
|
|
|
||
|
|
@app.get("/health")
|
||
|
|
async def health():
|
||
|
|
"""健康检查"""
|
||
|
|
return {
|
||
|
|
"status": "healthy",
|
||
|
|
"app": settings.APP_TITLE,
|
||
|
|
"version": settings.APP_VERSION
|
||
|
|
}
|
||
|
|
|
||
|
|
@app.get("/api/config")
|
||
|
|
async def get_config():
|
||
|
|
"""获取应用配置"""
|
||
|
|
return {
|
||
|
|
"title": settings.APP_TITLE,
|
||
|
|
"version": settings.APP_VERSION,
|
||
|
|
"max_upload_size": settings.MAX_UPLOAD_SIZE,
|
||
|
|
"allowed_extensions": list(settings.ALLOWED_EXTENSIONS),
|
||
|
|
"language_default": settings.LANGUAGE_DEFAULT
|
||
|
|
}
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
import uvicorn
|
||
|
|
|
||
|
|
uvicorn.run(
|
||
|
|
"app.main:app",
|
||
|
|
host=settings.HOST,
|
||
|
|
port=settings.PORT,
|
||
|
|
reload=settings.RELOAD,
|
||
|
|
log_level=settings.LOG_LEVEL.lower()
|
||
|
|
)
|