39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
import os
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from contextlib import asynccontextmanager
|
|
from dotenv import load_dotenv
|
|
|
|
from database import init_db
|
|
from routes import stock, collections, shares, sectors
|
|
|
|
load_dotenv()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
init_db()
|
|
yield
|
|
|
|
|
|
app = FastAPI(title="AUV API", version="1.0.0", lifespan=lifespan)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(stock.router, prefix="/api/stock")
|
|
app.include_router(collections.router, prefix="/api/collections")
|
|
app.include_router(shares.router, prefix="/api/share")
|
|
app.include_router(sectors.router, prefix="/api/sectors")
|
|
|
|
# 生产模式:后端同时托管前端静态文件
|
|
dist_path = os.path.join(os.path.dirname(__file__), "dist")
|
|
if os.path.isdir(dist_path):
|
|
app.mount("/", StaticFiles(directory=dist_path, html=True), name="static")
|