58 lines
2.1 KiB
Python
58 lines
2.1 KiB
Python
from fastapi import FastAPI, Request
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from routers import algorithms, events, devices, dashboard, monitors, alarms, scenes, upload, auth
|
|
from core.database import engine
|
|
from models.base import Base
|
|
import os
|
|
|
|
# 创建数据库表
|
|
Base.metadata.create_all(bind=engine)
|
|
|
|
app = FastAPI(
|
|
title="边检CV算法接口服务",
|
|
description="边检计算机视觉算法管理系统API",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# 配置CORS
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"], # 生产环境中应该指定具体域名
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# 静态文件服务
|
|
app.mount("/uploads", StaticFiles(directory="uploads"), name="uploads")
|
|
app.mount("/assets", StaticFiles(directory="dist/assets"), name="assets")
|
|
app.mount("/img", StaticFiles(directory="dist/assets"), name="img")
|
|
|
|
|
|
# 注册路由
|
|
app.include_router(algorithms.router, prefix="/api/algorithms", tags=["算法管理"])
|
|
app.include_router(events.router, prefix="/api/events", tags=["事件管理"])
|
|
app.include_router(devices.router, prefix="/api/devices", tags=["设备管理"])
|
|
app.include_router(dashboard.router, prefix="/api/dashboard", tags=["仪表板"])
|
|
app.include_router(monitors.router, prefix="/api/monitors", tags=["监控管理"])
|
|
app.include_router(alarms.router, prefix="/api/alarms", tags=["告警管理"])
|
|
app.include_router(scenes.router, prefix="/api/scenes", tags=["场景管理"])
|
|
app.include_router(upload.router, prefix="/api/upload", tags=["文件上传"])
|
|
app.include_router(auth.router, prefix="/api/auth", tags=["用户认证"])
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
index_path = os.path.join("dist", "index.html")
|
|
if os.path.exists(index_path):
|
|
return FileResponse(index_path, media_type="text/html")
|
|
return {"message": "边检CV算法接口服务"}
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {"status": "healthy"}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run('app:app', host="0.0.0.0", port=6789, reload=True, workers=1) |