You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.5 KiB
95 lines
2.5 KiB
"""
|
|
AmazingData 数据服务平台 - FastAPI 主应用
|
|
"""
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from contextlib import asynccontextmanager
|
|
import os
|
|
|
|
from backend.config import settings
|
|
from backend.models.database import init_db
|
|
from backend.api import api_router
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
"""应用生命周期管理"""
|
|
# 启动时执行
|
|
print("Starting AmazingData Platform...")
|
|
|
|
# 初始化数据库
|
|
try:
|
|
init_db()
|
|
print("Database initialized successfully")
|
|
except Exception as e:
|
|
print(f"Database initialization warning: {e}")
|
|
|
|
# 创建数据目录
|
|
os.makedirs(os.path.join(settings.DATA_SAVE_PATH, "single"), exist_ok=True)
|
|
os.makedirs(os.path.join(settings.DATA_SAVE_PATH, "stock"), exist_ok=True)
|
|
os.makedirs(os.path.join(settings.DATA_SAVE_PATH, "future"), exist_ok=True)
|
|
os.makedirs(os.path.join(settings.DATA_SAVE_PATH, "realtime"), exist_ok=True)
|
|
os.makedirs(os.path.join(settings.DATA_SAVE_PATH, "batch"), exist_ok=True)
|
|
|
|
print("AmazingData Platform started successfully!")
|
|
|
|
yield
|
|
|
|
# 关闭时执行
|
|
print("Shutting down AmazingData Platform...")
|
|
|
|
|
|
# 创建 FastAPI 应用
|
|
app = FastAPI(
|
|
title="AmazingData Platform",
|
|
description="AmazingData 数据服务平台 - 提供股票、期货K线数据获取和实时订阅服务",
|
|
version="1.0.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS 中间件
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 注册路由
|
|
app.include_router(api_router, prefix="/api/v1")
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""根路径"""
|
|
return {
|
|
"name": "AmazingData Platform",
|
|
"version": "1.0.0",
|
|
"status": "running",
|
|
"docs": "/docs"
|
|
}
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""健康检查"""
|
|
return {"status": "healthy"}
|
|
|
|
|
|
# 挂载静态文件(前端构建产物)
|
|
frontend_dist = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "frontend", "dist")
|
|
if os.path.exists(frontend_dist):
|
|
app.mount("/", StaticFiles(directory=frontend_dist, html=True), name="frontend")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"backend.main:app",
|
|
host=settings.BACKEND_HOST,
|
|
port=settings.BACKEND_PORT,
|
|
reload=settings.DEBUG
|
|
) |