Add database auto-creation on startup
This commit is contained in:
parent
53e2f85b47
commit
496fbc4aeb
1 changed files with 17 additions and 1 deletions
|
|
@ -1,19 +1,35 @@
|
||||||
"""BIM Twin Viewer — FastAPI application entry point."""
|
"""BIM Twin Viewer — FastAPI application entry point."""
|
||||||
|
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.middleware.cors import CORSMiddleware
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
|
||||||
from app.api import health, elements, upload
|
from app.api import health, elements, upload
|
||||||
|
from app.models.database import engine, Base
|
||||||
|
|
||||||
|
# Import all models so Base.metadata knows about them
|
||||||
|
from app.models import element # noqa: F401
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
"""Create database tables on startup."""
|
||||||
|
async with engine.begin() as conn:
|
||||||
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
yield
|
||||||
|
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
title="BIM Twin Viewer API",
|
title="BIM Twin Viewer API",
|
||||||
description="REST API for IFC-based 3D building model inspection",
|
description="REST API for IFC-based 3D building model inspection",
|
||||||
version="0.1.0",
|
version="0.1.0",
|
||||||
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
allow_origins=["*"], # tighten in production
|
allow_origins=["*"],
|
||||||
allow_credentials=True,
|
allow_credentials=True,
|
||||||
allow_methods=["*"],
|
allow_methods=["*"],
|
||||||
allow_headers=["*"],
|
allow_headers=["*"],
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue