Spaces:
Runtime error
Runtime error
allow local-only reindex without token
Browse files- bot_app.py +3 -3
- main.py +17 -12
bot_app.py
CHANGED
|
@@ -15,8 +15,6 @@ REINDEX_API_TOKEN = os.getenv("REINDEX_API_TOKEN")
|
|
| 15 |
|
| 16 |
if not DISCORD_TOKEN:
|
| 17 |
raise RuntimeError("DISCORD_TOKEN nao definido.")
|
| 18 |
-
if not REINDEX_API_TOKEN:
|
| 19 |
-
raise RuntimeError("REINDEX_API_TOKEN nao definido.")
|
| 20 |
|
| 21 |
index_rt = LocalIndexRuntime()
|
| 22 |
|
|
@@ -68,7 +66,9 @@ async def rag_cmd(ctx, *, question: str):
|
|
| 68 |
async def reindex_cmd(ctx):
|
| 69 |
await ctx.reply("Iniciando reindex... (pode demorar)")
|
| 70 |
try:
|
| 71 |
-
headers = {
|
|
|
|
|
|
|
| 72 |
r = await asyncio.to_thread(
|
| 73 |
requests.post,
|
| 74 |
"http://127.0.0.1:7860/reindex",
|
|
|
|
| 15 |
|
| 16 |
if not DISCORD_TOKEN:
|
| 17 |
raise RuntimeError("DISCORD_TOKEN nao definido.")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
index_rt = LocalIndexRuntime()
|
| 20 |
|
|
|
|
| 66 |
async def reindex_cmd(ctx):
|
| 67 |
await ctx.reply("Iniciando reindex... (pode demorar)")
|
| 68 |
try:
|
| 69 |
+
headers = {}
|
| 70 |
+
if REINDEX_API_TOKEN:
|
| 71 |
+
headers["Authorization"] = f"Bearer {REINDEX_API_TOKEN}"
|
| 72 |
r = await asyncio.to_thread(
|
| 73 |
requests.post,
|
| 74 |
"http://127.0.0.1:7860/reindex",
|
main.py
CHANGED
|
@@ -5,7 +5,7 @@ import time
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
import uvicorn
|
| 8 |
-
from fastapi import FastAPI, Header, HTTPException
|
| 9 |
from fastapi.responses import PlainTextResponse
|
| 10 |
|
| 11 |
from bot_app import run_bot
|
|
@@ -83,17 +83,22 @@ def logs():
|
|
| 83 |
|
| 84 |
|
| 85 |
@app.post("/reindex", response_class=PlainTextResponse)
|
| 86 |
-
def reindex(authorization: str | None = Header(default=None)):
|
| 87 |
-
if
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
return run_ingest()
|
| 98 |
|
| 99 |
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
import uvicorn
|
| 8 |
+
from fastapi import FastAPI, Header, HTTPException, Request
|
| 9 |
from fastapi.responses import PlainTextResponse
|
| 10 |
|
| 11 |
from bot_app import run_bot
|
|
|
|
| 83 |
|
| 84 |
|
| 85 |
@app.post("/reindex", response_class=PlainTextResponse)
|
| 86 |
+
def reindex(request: Request, authorization: str | None = Header(default=None)):
|
| 87 |
+
if REINDEX_API_TOKEN:
|
| 88 |
+
if not authorization:
|
| 89 |
+
raise HTTPException(status_code=401, detail="Missing Authorization header")
|
| 90 |
+
|
| 91 |
+
expected = f"Bearer {REINDEX_API_TOKEN}"
|
| 92 |
+
if authorization != expected:
|
| 93 |
+
raise HTTPException(status_code=403, detail="Invalid token")
|
| 94 |
+
return run_ingest()
|
| 95 |
+
|
| 96 |
+
client_host = request.client.host if request.client else ""
|
| 97 |
+
if client_host not in {"127.0.0.1", "::1", "localhost"}:
|
| 98 |
+
raise HTTPException(
|
| 99 |
+
status_code=403,
|
| 100 |
+
detail="External /reindex disabled when REINDEX_API_TOKEN is not set",
|
| 101 |
+
)
|
| 102 |
return run_ingest()
|
| 103 |
|
| 104 |
|