58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
|
|
import logging
|
||
|
|
from contextlib import asynccontextmanager
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
from fastapi import FastAPI
|
||
|
|
from fastapi.middleware.cors import CORSMiddleware
|
||
|
|
|
||
|
|
from app.config import get_settings
|
||
|
|
from app.routers import admin, health, jobs, lookups, ncrs, reports, users
|
||
|
|
|
||
|
|
|
||
|
|
@asynccontextmanager
|
||
|
|
async def lifespan(app: FastAPI):
|
||
|
|
settings = get_settings()
|
||
|
|
logging.basicConfig(
|
||
|
|
level=getattr(logging, settings.log_level.upper(), logging.INFO),
|
||
|
|
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||
|
|
)
|
||
|
|
Path(settings.attachments_dir).mkdir(parents=True, exist_ok=True)
|
||
|
|
if settings.auth_mode == "dev":
|
||
|
|
logging.getLogger(__name__).warning(
|
||
|
|
"AUTH_MODE=dev — authentication is BYPASSED. Never use in production."
|
||
|
|
)
|
||
|
|
# Fail fast on a misconfigured job-lookup provider.
|
||
|
|
from app.services.job_lookup import get_job_lookup_service
|
||
|
|
|
||
|
|
get_job_lookup_service()
|
||
|
|
yield
|
||
|
|
|
||
|
|
|
||
|
|
app = FastAPI(
|
||
|
|
title="PESCO NCR API",
|
||
|
|
version="1.0.0",
|
||
|
|
docs_url="/api/docs",
|
||
|
|
openapi_url="/api/openapi.json",
|
||
|
|
redoc_url=None,
|
||
|
|
lifespan=lifespan,
|
||
|
|
)
|
||
|
|
|
||
|
|
# In production nginx serves the SPA and proxies /api same-origin, so CORS is
|
||
|
|
# only exercised by the Vite dev server.
|
||
|
|
app.add_middleware(
|
||
|
|
CORSMiddleware,
|
||
|
|
allow_origins=["http://localhost:5173", "http://127.0.0.1:5173"],
|
||
|
|
allow_credentials=True,
|
||
|
|
allow_methods=["*"],
|
||
|
|
allow_headers=["*"],
|
||
|
|
)
|
||
|
|
|
||
|
|
API = "/api"
|
||
|
|
app.include_router(health.router, prefix=API)
|
||
|
|
app.include_router(users.router, prefix=API)
|
||
|
|
app.include_router(lookups.router, prefix=API)
|
||
|
|
app.include_router(jobs.router, prefix=API)
|
||
|
|
app.include_router(ncrs.router, prefix=API)
|
||
|
|
app.include_router(reports.router, prefix=API)
|
||
|
|
app.include_router(admin.router, prefix=API)
|