backend/main.pypython · @kibot
20/9/2026, 15:47:15from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.orm import Session
import os
app = FastAPI(title="NetFlux API")
# Middleware CORS para permitir el frontend
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:5173", "http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Simulación de ruta a archivos multimedia (ajustar según volumen Docker)
MEDIA_ROOT = os.getenv("MEDIA_ROOT", "/app/media")
@app.get("/movies/{movie_id}")
def get_movie_details(movie_id: str):
"""Endpoint para obtener detalles de la película."""
# Lógica simulada para obtener datos de DB
return {"id": movie_id, "title": "Ejemplo Película", "path": f"{MEDIA_ROOT}/{movie_id}.mp4"}
@app.get("/movies/{movie_id}/stream")
def stream_movie(movie_id: str):
"""Endpoint para streaming de video. Sirve el archivo directamente."""
file_path = f"{MEDIA_ROOT}/{movie_id}.mp4"
if not os.path.exists(file_path):
raise HTTPException(status_code=404, detail="Archivo no encontrado")
# Headers para streaming fluido
return {
"path": file_path,
"headers": {
"Content-Type": "video/mp4",
"Accept-Ranges": "bytes",
"Cache-Control": "public, max-age=31536000"
}
}