72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
from fastapi import FastAPI, HTTPException, Depends, BackgroundTasks
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from typing import Optional, Dict, List
|
|
import uvicorn
|
|
from pathlib import Path
|
|
import logging
|
|
import yaml
|
|
from datetime import datetime
|
|
|
|
# 导入API路由
|
|
from api.data_api import router as data_router
|
|
from api.model_api import router as model_router
|
|
from api.system_api import router as system_router
|
|
|
|
# 创建FastAPI应用
|
|
app = FastAPI(
|
|
title="机器学习平台API",
|
|
description="提供数据处理、模型训练和系统监控功能的API服务",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# 配置CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 设置日志
|
|
logging.basicConfig(
|
|
level=logging.INFO,
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
handlers=[
|
|
logging.FileHandler(f'.log/server_{datetime.now():%Y%m%d_%H%M%S}.log'),
|
|
logging.StreamHandler()
|
|
]
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# 加载配置
|
|
def load_config():
|
|
try:
|
|
with open('config/config.yaml', 'r', encoding='utf-8') as f:
|
|
return yaml.safe_load(f)
|
|
except Exception as e:
|
|
logger.error(f"Error loading config: {str(e)}")
|
|
return {}
|
|
|
|
config = load_config()
|
|
|
|
# 注册路由
|
|
app.include_router(data_router, prefix="/api", tags=["数据处理"])
|
|
app.include_router(model_router, prefix="/api", tags=["模型管理"])
|
|
app.include_router(system_router, prefix="/api", tags=["系统监控"])
|
|
|
|
# 健康检查
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy", "timestamp": datetime.now().isoformat()}
|
|
|
|
if __name__ == "__main__":
|
|
uvicorn.run(
|
|
"main:app",
|
|
host=config.get('host', '0.0.0.0'),
|
|
port=config.get('port', 8000),
|
|
reload=True,
|
|
workers=config.get('workers', 4)
|
|
) |