Browse Source

CORS e concorrencia

main
Guilherme Rudic 6 months ago
parent
commit
2bfaa1741e
1 changed files with 16 additions and 0 deletions
  1. +16
    -0
      main.py

+ 16
- 0
main.py View File

from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from pydantic import BaseModel from pydantic import BaseModel
from typing import Optional from typing import Optional
from fastapi.middleware.cors import CORSMiddleware
import theading
# Suponha que quebrar_captcha_base64 está definido em outro módulo ou arquivo # Suponha que quebrar_captcha_base64 está definido em outro módulo ou arquivo
from solve_captcha import quebrar_captcha_base64 from solve_captcha import quebrar_captcha_base64
app = FastAPI() 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): class CaptchaRequest(BaseModel):
captchafile: str captchafile: str
authtoken: str authtoken: str
raise HTTPException(status_code=400, detail="captchafile is required") raise HTTPException(status_code=400, detail="captchafile is required")
try: try:
lock.acquire()
resultado_captcha = quebrar_captcha_base64(request.captchafile) resultado_captcha = quebrar_captcha_base64(request.captchafile)
lock.release()
return {"captcha": "", "text": resultado_captcha, "is_correct": True, "status": 200} return {"captcha": "", "text": resultado_captcha, "is_correct": True, "status": 200}
except Exception as e: except Exception as e:
lock.release()
raise HTTPException(status_code=500, detail=str(e)) raise HTTPException(status_code=500, detail=str(e))

Loading…
Cancel
Save