76 lines
1.9 KiB
Python
76 lines
1.9 KiB
Python
import asyncio
|
|
import os
|
|
from contextlib import asynccontextmanager
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from fastapi.responses import FileResponse
|
|
from backend.api.routes import router
|
|
from backend.db.database import init_db
|
|
from backend.services.generation_service import GenerationService
|
|
|
|
|
|
async def periodic_cleanup():
|
|
while True:
|
|
try:
|
|
await asyncio.sleep(3600) # Every hour
|
|
GenerationService.cleanup_old_artifacts(ttl_seconds=7200)
|
|
except asyncio.CancelledError:
|
|
break
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
await init_db()
|
|
cleanup_task = asyncio.create_task(periodic_cleanup())
|
|
yield
|
|
cleanup_task.cancel()
|
|
|
|
|
|
app = FastAPI(
|
|
title="lotusgen API",
|
|
description="High-throughput, low-memory dummy data generator",
|
|
version="1.0.0",
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
app.include_router(router)
|
|
|
|
|
|
@app.get("/api/health")
|
|
async def health_check():
|
|
return {"status": "ok", "service": "lotusgen"}
|
|
|
|
|
|
# Static asset mounting & SPA routing fallback
|
|
dist_dir = os.path.join(os.path.dirname(__file__), "..", "frontend", "dist")
|
|
assets_dir = os.path.join(dist_dir, "assets")
|
|
if os.path.isdir(assets_dir):
|
|
app.mount("/assets", StaticFiles(directory=assets_dir), name="assets")
|
|
|
|
|
|
@app.get("/{full_path:path}")
|
|
async def serve_spa(full_path: str):
|
|
if full_path.startswith("api/"):
|
|
return {"detail": "Not found"}
|
|
index_path = os.path.join(dist_dir, "index.html")
|
|
if os.path.isfile(index_path):
|
|
return FileResponse(index_path)
|
|
return {"status": "ok", "service": "lotusgen"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
|
|
uvicorn.run("backend.main:app", host="0.0.0.0", port=8000, reload=True)
|