abcd / api /main.py
docs4you's picture
Upload 41 files
84121fd verified
from fastapi import FastAPI, HTTPException, Depends, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, PlainTextResponse
from fastapi.staticfiles import StaticFiles
import uvicorn
import os
from dotenv import load_dotenv
# Cargar variables de entorno
load_dotenv()
# Importar módulos
from auth import router as auth_router, get_current_user
from m3u_parser import router as m3u_router
from proxy import router as proxy_router
from viewers import router as viewers_router
from admin import router as admin_router
app = FastAPI(
title="DaddyTV IPTV API",
description="API para reproductor IPTV personal",
version="1.0.0"
)
# CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Servir archivos estáticos del frontend (para Docker)
if os.path.exists("static"):
app.mount("/static", StaticFiles(directory="static"), name="static")
# Incluir routers
app.include_router(auth_router, prefix="/api")
app.include_router(m3u_router, prefix="/api")
app.include_router(proxy_router, prefix="/api")
app.include_router(viewers_router, prefix="/api")
app.include_router(admin_router, prefix="/api/admin")
@app.get("/")
async def root():
# En Docker, servir el frontend
if os.path.exists("static/index.html"):
with open("static/index.html", "r") as f:
return PlainTextResponse(f.read(), media_type="text/html")
return {"message": "DaddyTV IPTV API", "version": "1.0.0"}
@app.get("/api/health")
async def health_check():
return {"status": "healthy", "message": "API funcionando correctamente"}
# Catch-all para servir el frontend en rutas no-API (SPA)
@app.get("/{full_path:path}")
async def serve_frontend(full_path: str):
if full_path.startswith("api/"):
raise HTTPException(status_code=404, detail="API endpoint not found")
if os.path.exists("static/index.html"):
with open("static/index.html", "r") as f:
return PlainTextResponse(f.read(), media_type="text/html")
raise HTTPException(status_code=404, detail="Frontend not found")
if __name__ == "__main__":
port = int(os.getenv("PORT", 8000))
uvicorn.run(app, host="0.0.0.0", port=port)