talos-access / backend /app /main.py
Greg
Claude Opus 4.7 (1M context)
Preparar despliegue en fly.io: monolito dockerizado + seeder demo
6aa6081
Raw History Blame Contribute Delete
1.88 kB
import os
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from app.core.config import settings
from app.api.v1.router import router
from app.middleware.error_logger import system_error_handler
app = FastAPI(
title=settings.APP_NAME,
debug=settings.DEBUG,
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router)
app.add_exception_handler(Exception, system_error_handler)
@app.get("/health")
def health():
return {"status": "ok", "app": settings.APP_NAME}
# --- Serve the built frontend (monolith). Only active when a build is present,
# so local dev (Vite on a separate port) is unaffected. ---
FRONTEND_DIR = os.path.abspath(
os.getenv("FRONTEND_DIST")
or os.path.join(os.path.dirname(os.path.dirname(__file__)), "static")
)
if os.path.isdir(FRONTEND_DIR):
_assets_dir = os.path.join(FRONTEND_DIR, "assets")
if os.path.isdir(_assets_dir):
app.mount("/assets", StaticFiles(directory=_assets_dir), name="assets")
_index = os.path.join(FRONTEND_DIR, "index.html")
@app.get("/{full_path:path}")
def spa_fallback(full_path: str):
# API routes and /health are registered earlier and take precedence.
# Serve a real static file if it exists; otherwise return index.html
# so the client-side router can handle the path.
candidate = os.path.normpath(os.path.join(FRONTEND_DIR, full_path))
within = candidate == FRONTEND_DIR or candidate.startswith(FRONTEND_DIR + os.sep)
if full_path and within and os.path.isfile(candidate):
return FileResponse(candidate)
return FileResponse(_index)