Spaces:
Sleeping
Sleeping
File size: 1,526 Bytes
70520f0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import os
from pathlib import Path
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from api.routes import router
app = FastAPI(title='DocForensics API', version='1.0.0')
# CORS β defaults to permissive for local dev; lock down in prod via env var,
# e.g. ALLOWED_ORIGINS="https://myapp.com,https://www.myapp.com"
_origins = os.getenv('ALLOWED_ORIGINS', '*').split(',')
app.add_middleware(
CORSMiddleware,
allow_origins=[o.strip() for o in _origins],
allow_methods=['*'],
allow_headers=['*'],
)
app.include_router(router, prefix='/api')
# ββ Serve the built React frontend (single-container deployment) ββββββββββββββ
# If frontend/dist exists (created by `npm run build`), serve it at the root so
# the whole app runs from one origin with no CORS configuration required.
_DIST = Path(__file__).resolve().parent.parent / 'frontend' / 'dist'
if _DIST.is_dir():
app.mount('/assets', StaticFiles(directory=_DIST / 'assets'), name='assets')
@app.get('/')
def _index():
return FileResponse(_DIST / 'index.html')
@app.get('/{full_path:path}')
def _spa_fallback(full_path: str):
# Serve real files if present, otherwise fall back to index.html (SPA routing).
candidate = _DIST / full_path
if candidate.is_file():
return FileResponse(candidate)
return FileResponse(_DIST / 'index.html')
|