Files
FishServer/fish_api/app/main.py

75 lines
2.4 KiB
Python
Raw Normal View History

from __future__ import annotations
import asyncio
from contextlib import asynccontextmanager
from typing import List
from fastapi import FastAPI
from app.logging_config import setup_logging
2026-04-10 18:16:15 +08:00
from app.media_static import MediaStaticFiles
from app.db import init_db
from app.routers import biomass, debug, ingest, zed
from app.services.action_watch import run_action_watch_loop
from app.services.measure_watch import run_measure_watch_loop
from app.services.sonar_video import run_sonar_video_watch_loop
from app.settings import get_settings
setup_logging()
@asynccontextmanager
async def lifespan(app: FastAPI):
setup_logging()
s = get_settings()
init_db(s)
s.media_root.mkdir(parents=True, exist_ok=True)
s.stream_tmp_dir.mkdir(parents=True, exist_ok=True)
tasks = [] # type: List[asyncio.Task]
if s.action_watch_dir is not None:
tasks.append(asyncio.create_task(run_action_watch_loop(s)))
if s.measure_watch_dir is not None:
tasks.append(asyncio.create_task(run_measure_watch_loop(s)))
if s.biomass_sonar_video_dir is not None:
tasks.append(asyncio.create_task(run_sonar_video_watch_loop(s)))
yield
for t in tasks:
t.cancel()
for t in tasks:
try:
await t
except asyncio.CancelledError:
pass
app = FastAPI(title="Fish API", lifespan=lifespan)
app.include_router(ingest.router)
app.include_router(biomass.router)
app.include_router(debug.router)
app.include_router(zed.router)
_settings = get_settings()
_settings.media_root.mkdir(parents=True, exist_ok=True)
app.mount(
"/media",
2026-04-10 18:16:15 +08:00
MediaStaticFiles(directory=str(_settings.media_root)),
name="media",
)
@app.get("/")
async def root():
return {
"service": "fish-api",
"docs": "/docs",
"ingest": "/api/v1/ingest/",
"biomass_camera": "/api/v1/biomass/real/camera/",
"biomass_health": "/api/v1/biomass/health/result/",
2026-04-13 14:50:44 +08:00
"biomass_water_video": "/api/v1/biomass/water/video/",
"biomass_sonar_video": "/api/v1/biomass/sonar/video/",
"debug_measure": "/api/v1/debug/meause",
"zed_recording": "/api/v1/zed/recording/start|stop|status",
"note": "若配置了 ACTION_WATCH_DIR / MEASURE_WATCH_DIR启动后会后台监控对应目录。ZED 分段录制由独立进程/脚本负责,不由 fish_api 启停;可选 HTTP /api/v1/zed/recording/start|stop|status。",
}