forked from innovacion/Mayacontigo
add healthcheck to remaining apps
This commit is contained in:
42
apps/pyme/.k8s/deployment.yaml
Normal file
42
apps/pyme/.k8s/deployment.yaml
Normal file
@@ -0,0 +1,42 @@
|
||||
apiVersion: apps/v1
|
||||
kind: Deployment
|
||||
metadata:
|
||||
name: mayacontigo-pyme
|
||||
namespace: apps
|
||||
labels:
|
||||
app: mayacontigo-pyme
|
||||
spec:
|
||||
replicas: 1
|
||||
selector:
|
||||
matchLabels:
|
||||
app: mayacontigo-pyme
|
||||
template:
|
||||
metadata:
|
||||
labels:
|
||||
app: mayacontigo-pyme
|
||||
spec:
|
||||
imagePullSecrets:
|
||||
- name: gitea-registry-cred
|
||||
containers:
|
||||
- name: mayacontigo-pyme
|
||||
image: gitea.ia-innovacion.work/innovacion/mayacontigo-pyme:latest
|
||||
env:
|
||||
- name: VAULT_TOKEN
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: mayacontigo-pyme-secret
|
||||
key: VAULT_TOKEN
|
||||
ports:
|
||||
- containerPort: 80
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /
|
||||
port: 80
|
||||
initialDelaySeconds: 5
|
||||
periodSeconds: 10
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /
|
||||
port: 80
|
||||
initialDelaySeconds: 15
|
||||
periodSeconds: 20
|
||||
19
apps/pyme/.k8s/ingress.yaml
Normal file
19
apps/pyme/.k8s/ingress.yaml
Normal file
@@ -0,0 +1,19 @@
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: Ingress
|
||||
metadata:
|
||||
name: mayacontigo-pyme-ingress
|
||||
annotations:
|
||||
nginx.ingress.kubernetes.io/rewrite-target: /
|
||||
spec:
|
||||
ingressClassName: nginx
|
||||
rules:
|
||||
- host: mayacontigo-pyme.app.ia-innovacion.work
|
||||
http:
|
||||
paths:
|
||||
- path: /
|
||||
pathType: Prefix
|
||||
backend:
|
||||
service:
|
||||
name: mayacontigo-pyme-service
|
||||
port:
|
||||
number: 80
|
||||
17
apps/pyme/.k8s/secrets.yaml
Normal file
17
apps/pyme/.k8s/secrets.yaml
Normal file
@@ -0,0 +1,17 @@
|
||||
apiVersion: external-secrets.io/v1
|
||||
kind: ExternalSecret
|
||||
metadata:
|
||||
name: mayacontigo-pyme-vault
|
||||
namespace: apps
|
||||
spec:
|
||||
refreshInterval: "15s"
|
||||
secretStoreRef:
|
||||
name: vault-backend
|
||||
kind: ClusterSecretStore
|
||||
target:
|
||||
name: mayacontigo-pyme-secret
|
||||
data:
|
||||
- secretKey: VAULT_TOKEN
|
||||
remoteRef:
|
||||
key: mayacontigo-pyme
|
||||
property: VAULT_TOKEN
|
||||
14
apps/pyme/.k8s/service.yaml
Normal file
14
apps/pyme/.k8s/service.yaml
Normal file
@@ -0,0 +1,14 @@
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
metadata:
|
||||
name: mayacontigo-pyme-service
|
||||
labels:
|
||||
app: mayacontigo-pyme
|
||||
spec:
|
||||
selector:
|
||||
app: mayacontigo-pyme
|
||||
ports:
|
||||
- port: 80
|
||||
targetPort: 80
|
||||
protocol: TCP
|
||||
type: ClusterIP
|
||||
@@ -1,15 +1,14 @@
|
||||
import uuid
|
||||
import os
|
||||
import uuid
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from dotenv import load_dotenv # ← Agregar este import
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import StreamingResponse, FileResponse, RedirectResponse
|
||||
from pydantic import BaseModel
|
||||
from fastapi.responses import FileResponse, RedirectResponse, StreamingResponse
|
||||
from langfuse import Langfuse
|
||||
|
||||
from dotenv import load_dotenv # ← Agregar este import
|
||||
from pydantic import BaseModel
|
||||
|
||||
from api import services
|
||||
from api.agent import Agent
|
||||
@@ -21,14 +20,13 @@ load_dotenv()
|
||||
# Configurar Langfuse desde variables de entorno
|
||||
langfuse = Langfuse(
|
||||
public_key=os.getenv("LANGFUSE_PUBLIC_KEY"),
|
||||
secret_key=os.getenv("LANGFUSE_SECRET_KEY"),
|
||||
host=os.getenv("LANGFUSE_HOST")
|
||||
secret_key=os.getenv("LANGFUSE_SECRET_KEY"),
|
||||
host=os.getenv("LANGFUSE_HOST"),
|
||||
)
|
||||
|
||||
|
||||
# Mapeo completo de archivos a URLs públicas
|
||||
PDF_PUBLIC_URLS = {
|
||||
}
|
||||
PDF_PUBLIC_URLS = {}
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -41,17 +39,17 @@ app = FastAPI(lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
expose_headers=["*"]
|
||||
expose_headers=["*"],
|
||||
)
|
||||
|
||||
agent = Agent()
|
||||
|
||||
PDF_FOLDER = Path(__file__).parent / "agent" / "pdf"
|
||||
PDF_FOLDER.mkdir(parents=True, exist_ok=True)
|
||||
PDF_FOLDER.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
@app.post("/api/v1/conversation")
|
||||
@@ -72,21 +70,21 @@ async def send(message: Message):
|
||||
trace = langfuse.trace(
|
||||
name="rag_chat",
|
||||
session_id=str(message.conversation_id),
|
||||
input={"prompt": message.prompt}
|
||||
input={"prompt": message.prompt},
|
||||
)
|
||||
|
||||
|
||||
def b64_sse(func):
|
||||
async def wrapper(*args, **kwargs):
|
||||
response_parts = []
|
||||
|
||||
|
||||
async for chunk in func(*args, **kwargs):
|
||||
if chunk.type == "text" and chunk.content:
|
||||
response_parts.append(str(chunk.content))
|
||||
|
||||
|
||||
content = chunk.model_dump_json()
|
||||
data = f"data: {content}\n\n"
|
||||
yield data
|
||||
|
||||
|
||||
# Solo registrar input y output
|
||||
full_response = "".join(response_parts)
|
||||
trace.update(output={"response": full_response})
|
||||
@@ -101,17 +99,21 @@ async def send(message: Message):
|
||||
@app.get("/api/pdf/{filename}")
|
||||
async def get_pdf(filename: str):
|
||||
print(f"🔍 Solicitud PDF para: {filename}")
|
||||
|
||||
if not filename.lower().endswith('.pdf'):
|
||||
|
||||
if not filename.lower().endswith(".pdf"):
|
||||
print(f"❌ Archivo no es PDF: {filename}")
|
||||
raise HTTPException(status_code=400, detail="El archivo debe ser un PDF")
|
||||
|
||||
if '..' in filename or ('/' in filename and not filename.startswith('http')) or '\\' in filename:
|
||||
|
||||
if (
|
||||
".." in filename
|
||||
or ("/" in filename and not filename.startswith("http"))
|
||||
or "\\" in filename
|
||||
):
|
||||
print(f"❌ Nombre de archivo inválido: {filename}")
|
||||
raise HTTPException(status_code=400, detail="Nombre de archivo inválido")
|
||||
|
||||
|
||||
public_url = PDF_PUBLIC_URLS.get(filename)
|
||||
|
||||
|
||||
if public_url:
|
||||
print(f"✅ Redirigiendo a URL pública: {public_url}")
|
||||
return RedirectResponse(
|
||||
@@ -121,39 +123,41 @@ async def get_pdf(filename: str):
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET, OPTIONS",
|
||||
"Access-Control-Allow-Headers": "*"
|
||||
}
|
||||
"Access-Control-Allow-Headers": "*",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
pdf_path = PDF_FOLDER / filename
|
||||
|
||||
|
||||
if not pdf_path.exists():
|
||||
print(f"❌ PDF no encontrado: {pdf_path}")
|
||||
raise HTTPException(status_code=404, detail=f"PDF no encontrado. Archivo: {filename}")
|
||||
|
||||
raise HTTPException(
|
||||
status_code=404, detail=f"PDF no encontrado. Archivo: {filename}"
|
||||
)
|
||||
|
||||
if not pdf_path.is_file():
|
||||
print(f"❌ No es un archivo: {pdf_path}")
|
||||
raise HTTPException(status_code=404, detail="El recurso no es un archivo")
|
||||
|
||||
|
||||
file_size = pdf_path.stat().st_size
|
||||
print(f"📄 Sirviendo archivo local: {filename} ({file_size} bytes)")
|
||||
|
||||
|
||||
if file_size == 0:
|
||||
print(f"❌ Archivo vacío: {pdf_path}")
|
||||
raise HTTPException(status_code=500, detail="El archivo PDF está vacío")
|
||||
|
||||
|
||||
return FileResponse(
|
||||
path=str(pdf_path),
|
||||
media_type="application/pdf",
|
||||
filename=filename,
|
||||
headers={
|
||||
"Content-Disposition": f"inline; filename={filename}",
|
||||
"Content-Disposition": f"inline; filename={filename}",
|
||||
"Content-Type": "application/pdf",
|
||||
"Cache-Control": "public, max-age=3600",
|
||||
"X-Frame-Options": "ALLOWALL",
|
||||
"X-Frame-Options": "ALLOWALL",
|
||||
"X-Content-Type-Options": "nosniff",
|
||||
"Access-Control-Allow-Origin": "*"
|
||||
}
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -161,29 +165,33 @@ async def get_pdf(filename: str):
|
||||
async def list_pdfs():
|
||||
try:
|
||||
pdf_files = []
|
||||
|
||||
|
||||
for filename, url in PDF_PUBLIC_URLS.items():
|
||||
pdf_files.append({
|
||||
"filename": filename,
|
||||
"size": "N/A (Público)",
|
||||
"url": f"/api/pdf/{filename}",
|
||||
"public_url": url,
|
||||
"type": "public"
|
||||
})
|
||||
|
||||
pdf_files.append(
|
||||
{
|
||||
"filename": filename,
|
||||
"size": "N/A (Público)",
|
||||
"url": f"/api/pdf/{filename}",
|
||||
"public_url": url,
|
||||
"type": "public",
|
||||
}
|
||||
)
|
||||
|
||||
local_files = []
|
||||
for pattern in ["*.pdf", "*.PDF"]:
|
||||
for file_path in PDF_FOLDER.glob(pattern):
|
||||
if file_path.is_file() and file_path.name not in PDF_PUBLIC_URLS:
|
||||
local_files.append({
|
||||
"filename": file_path.name,
|
||||
"size": file_path.stat().st_size,
|
||||
"url": f"/api/pdf/{file_path.name}",
|
||||
"type": "local"
|
||||
})
|
||||
|
||||
local_files.append(
|
||||
{
|
||||
"filename": file_path.name,
|
||||
"size": file_path.stat().st_size,
|
||||
"url": f"/api/pdf/{file_path.name}",
|
||||
"type": "local",
|
||||
}
|
||||
)
|
||||
|
||||
pdf_files.extend(local_files)
|
||||
|
||||
|
||||
debug_info = {
|
||||
"current_working_directory": str(Path.cwd()),
|
||||
"pdf_folder_path": str(PDF_FOLDER.absolute()),
|
||||
@@ -192,52 +200,49 @@ async def list_pdfs():
|
||||
"local_files_count": len(local_files),
|
||||
"public_files": list(PDF_PUBLIC_URLS.keys()),
|
||||
}
|
||||
|
||||
return {
|
||||
"pdfs": pdf_files,
|
||||
"debug": debug_info,
|
||||
"total_pdfs": len(pdf_files)
|
||||
}
|
||||
|
||||
return {"pdfs": pdf_files, "debug": debug_info, "total_pdfs": len(pdf_files)}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
|
||||
return {
|
||||
"error": str(e),
|
||||
"traceback": traceback.format_exc(),
|
||||
"debug": {
|
||||
"current_working_directory": str(Path.cwd()),
|
||||
"script_file_path": __file__ if '__file__' in globals() else "unknown"
|
||||
}
|
||||
"script_file_path": __file__ if "__file__" in globals() else "unknown",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@app.get("/api/pdf/{filename}/info")
|
||||
async def get_pdf_info(filename: str):
|
||||
if not filename.lower().endswith('.pdf'):
|
||||
if not filename.lower().endswith(".pdf"):
|
||||
raise HTTPException(status_code=400, detail="El archivo debe ser un PDF")
|
||||
|
||||
if '..' in filename or '/' in filename or '\\' in filename:
|
||||
|
||||
if ".." in filename or "/" in filename or "\\" in filename:
|
||||
raise HTTPException(status_code=400, detail="Nombre de archivo inválido")
|
||||
|
||||
|
||||
public_url = PDF_PUBLIC_URLS.get(filename)
|
||||
if public_url:
|
||||
return {
|
||||
"filename": filename,
|
||||
"size": "N/A",
|
||||
"size_mb": "N/A",
|
||||
"size_mb": "N/A",
|
||||
"modified": "N/A",
|
||||
"url": f"/api/pdf/{filename}",
|
||||
"public_url": public_url,
|
||||
"type": "public"
|
||||
"type": "public",
|
||||
}
|
||||
|
||||
|
||||
pdf_path = PDF_FOLDER / filename
|
||||
|
||||
|
||||
if not pdf_path.exists():
|
||||
raise HTTPException(status_code=404, detail="PDF no encontrado")
|
||||
|
||||
|
||||
if not pdf_path.is_file():
|
||||
raise HTTPException(status_code=404, detail="El recurso no es un archivo")
|
||||
|
||||
|
||||
try:
|
||||
file_stat = pdf_path.stat()
|
||||
return {
|
||||
@@ -246,10 +251,12 @@ async def get_pdf_info(filename: str):
|
||||
"size_mb": round(file_stat.st_size / (1024 * 1024), 2),
|
||||
"modified": file_stat.st_mtime,
|
||||
"url": f"/api/pdf/{filename}",
|
||||
"type": "local"
|
||||
"type": "local",
|
||||
}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Error al obtener información del PDF: {str(e)}")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Error al obtener información del PDF: {str(e)}"
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/health")
|
||||
@@ -258,5 +265,10 @@ async def health_check():
|
||||
"status": "healthy",
|
||||
"pdf_folder": str(PDF_FOLDER),
|
||||
"pdf_folder_exists": PDF_FOLDER.exists(),
|
||||
"public_urls_configured": len(PDF_PUBLIC_URLS)
|
||||
}
|
||||
"public_urls_configured": len(PDF_PUBLIC_URLS),
|
||||
}
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def health():
|
||||
return {"app": "RAG PyME", "status": "OK"}
|
||||
|
||||
Reference in New Issue
Block a user