File size: 2,302 Bytes
84121fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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)