fixes
This commit is contained in:
@@ -1,29 +1,52 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from alembic.config import Config
|
||||
from alembic import command
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from app.database import engine
|
||||
# uvicorn's dictConfig only configures uvicorn.* loggers; the root logger
|
||||
# ends up with no handler, so app.* records are silently discarded.
|
||||
# Give the app namespace its own StreamHandler to guarantee output.
|
||||
_app_logger = logging.getLogger("app")
|
||||
_app_logger.setLevel(logging.INFO)
|
||||
if not _app_logger.handlers:
|
||||
_h = logging.StreamHandler()
|
||||
_h.setFormatter(logging.Formatter("%(levelname)-8s [%(name)s] %(message)s"))
|
||||
_app_logger.addHandler(_h)
|
||||
_app_logger.propagate = False
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from app.database import engine, Base
|
||||
from app.routers import libraries, media, tags, search
|
||||
from app.services import watcher as watcher_service
|
||||
|
||||
|
||||
def run_migrations():
|
||||
alembic_cfg = Config("/backend/alembic.ini")
|
||||
command.upgrade(alembic_cfg, "head")
|
||||
import app.models # noqa: F401 — registers models with Base.metadata
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
run_migrations()
|
||||
log.info("Creating database tables...")
|
||||
async with engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
|
||||
log.info("Starting library watchers...")
|
||||
await watcher_service.start_all()
|
||||
|
||||
log.info("Startup complete.")
|
||||
yield
|
||||
|
||||
await watcher_service.stop_all()
|
||||
|
||||
|
||||
app = FastAPI(title="MediaLore", lifespan=lifespan)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unhandled_exception_handler(request: Request, exc: Exception):
|
||||
log.exception("Unhandled error on %s %s", request.method, request.url)
|
||||
return JSONResponse(status_code=500, content={"detail": str(exc)})
|
||||
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
|
||||
Reference in New Issue
Block a user