from fastapi import FastAPI, HTTPException from pydantic import BaseModel from typing import Optional from fastapi.middleware.cors import CORSMiddleware import threading # Suponha que quebrar_captcha_base64 está definido em outro módulo ou arquivo from solve_captcha import quebrar_captcha_base64 app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], # Ajuste conforme necessário allow_credentials=True, allow_methods=["*"], # Ajuste conforme necessário allow_headers=["*"], ) # Criando um bloqueio para gerenciamento de concorrência lock = threading.Lock() class CaptchaRequest(BaseModel): captchafile: str authtoken: str @app.post("/solve_captcha") async def solve_captcha(request: CaptchaRequest): expected_token = "j7MgdTy4LX05RpE1XY0ykLTexu9LuOE4zg5AEMNymL6LAgCUNN8JGjM5wu26FDGV4j0Xz3km8ra51c3ihToa3fj48XpcsWr45g043bLVQA1sTBkJP4j823DNX67mbjqNR8p117sU580153Id0IavORjkCXRP" if request.authtoken != expected_token: # Se o token não corresponder, retorne erro 401 Unauthorized raise HTTPException(status_code=401, detail="Unauthorized") if not request.captchafile: raise HTTPException(status_code=400, detail="captchafile is required") try: lock.acquire() resultado_captcha = quebrar_captcha_base64(request.captchafile) lock.release() return {"captcha": "", "text": resultado_captcha, "is_correct": True, "status": 200} except Exception as e: lock.release() raise HTTPException(status_code=500, detail=str(e))