mendesborges commited on
Commit
efdde13
·
1 Parent(s): 4b7bd18

fix: update main.py.

Browse files
Files changed (1) hide show
  1. main.py +59 -2
main.py CHANGED
@@ -3,7 +3,7 @@ import subprocess
3
  import uuid
4
  import shutil
5
  from fastapi import FastAPI, HTTPException, BackgroundTasks
6
- from fastapi.responses import FileResponse
7
  from pydantic import BaseModel
8
 
9
  app = FastAPI(title="LaTeX to PDF API")
@@ -62,4 +62,61 @@ async def compile_latex(request: LatexRequest, background_tasks: BackgroundTasks
62
 
63
  @app.get("/health")
64
  async def health_check():
65
- return {"status": "healthy"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  import uuid
4
  import shutil
5
  from fastapi import FastAPI, HTTPException, BackgroundTasks
6
+ from fastapi.responses import FileResponse, HTMLResponse
7
  from pydantic import BaseModel
8
 
9
  app = FastAPI(title="LaTeX to PDF API")
 
62
 
63
  @app.get("/health")
64
  async def health_check():
65
+ checks = {}
66
+
67
+ # Verifica se o pdflatex está disponível
68
+ try:
69
+ proc = subprocess.run(
70
+ ["pdflatex", "--version"],
71
+ stdout=subprocess.PIPE,
72
+ stderr=subprocess.PIPE,
73
+ text=True,
74
+ timeout=5,
75
+ )
76
+ checks["pdflatex"] = proc.returncode == 0
77
+ if not checks["pdflatex"]:
78
+ checks["pdflatex_error"] = (proc.stderr or proc.stdout).strip()
79
+ except Exception as e:
80
+ checks["pdflatex"] = False
81
+ checks["pdflatex_error"] = str(e)
82
+
83
+ # Verifica se é possível criar e remover arquivos em TEMP_DIR
84
+ try:
85
+ test_dir = os.path.join(TEMP_DIR, f"healthcheck_{uuid.uuid4().hex}")
86
+ os.makedirs(test_dir, exist_ok=True)
87
+ test_file = os.path.join(test_dir, "test.txt")
88
+ with open(test_file, "w") as f:
89
+ f.write("ok")
90
+ os.remove(test_file)
91
+ os.rmdir(test_dir)
92
+ checks["temp_dir_writable"] = True
93
+ except Exception as e:
94
+ checks["temp_dir_writable"] = False
95
+ checks["temp_dir_error"] = str(e)
96
+
97
+ healthy = checks.get("pdflatex") and checks.get("temp_dir_writable")
98
+ result = {"status": "healthy" if healthy else "unhealthy", "checks": checks}
99
+ if healthy:
100
+ return result
101
+ raise HTTPException(status_code=503, detail=result)
102
+
103
+
104
+ @app.get("/", response_class=HTMLResponse)
105
+ async def root():
106
+ html = """
107
+ <html>
108
+ <head>
109
+ <title>LaTeX to PDF API - Usage</title>
110
+ </head>
111
+ <body>
112
+ <h1>LaTeX to PDF API</h1>
113
+ <p>Exemplo de chamada usando <strong>curl</strong>:</p>
114
+ <pre>curl -X POST "https://thigas88-latex-api.hf.space/compile" \
115
+ -H "Content-Type: application/json" \
116
+ -d '{"content": "\\documentclass{article}\\begin{document}Hello World!\\end{document}"}' \
117
+ --output document.pdf</pre>
118
+ <p>Substitua a URL pela do seu servidor quando necessário.</p>
119
+ </body>
120
+ </html>
121
+ """
122
+ return HTMLResponse(content=html)