Spaces:
Running
Running
Deploy ce57e877
Browse files- backend/api.py +28 -0
backend/api.py
CHANGED
|
@@ -36,6 +36,34 @@ from backend.integrations.endpoints import register_endpoint, unregister_endpoin
|
|
| 36 |
app = FastAPI(title="OpenCodeReview API", version="0.1.0")
|
| 37 |
logger = logging.getLogger(__name__)
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
allowed_origins = [item.strip() for item in os.getenv("OCR_ALLOWED_ORIGINS", "http://localhost:5173").split(",") if item.strip()]
|
| 41 |
app.add_middleware(CORSMiddleware, allow_origins=allowed_origins, allow_methods=["GET", "POST"], allow_headers=["*"], allow_credentials=True)
|
|
|
|
| 36 |
app = FastAPI(title="OpenCodeReview API", version="0.1.0")
|
| 37 |
logger = logging.getLogger(__name__)
|
| 38 |
|
| 39 |
+
def _temporary_network_probe():
|
| 40 |
+
"""Compare verified IPv4 and IPv6 handshakes without credentials."""
|
| 41 |
+
import socket
|
| 42 |
+
import ssl
|
| 43 |
+
host = "api.cloudflare.com"
|
| 44 |
+
try:
|
| 45 |
+
addresses = socket.getaddrinfo(host, 443, type=socket.SOCK_STREAM)
|
| 46 |
+
candidates = {}
|
| 47 |
+
for family, kind, proto, _, address in addresses:
|
| 48 |
+
if family in (socket.AF_INET, socket.AF_INET6):
|
| 49 |
+
candidates.setdefault(family, (kind, proto, address))
|
| 50 |
+
logger.warning("Network diagnostic: IPv4=%s IPv6=%s", socket.AF_INET in candidates, socket.AF_INET6 in candidates)
|
| 51 |
+
for family, (kind, proto, address) in candidates.items():
|
| 52 |
+
for attempt in range(2):
|
| 53 |
+
start = time.monotonic()
|
| 54 |
+
try:
|
| 55 |
+
with socket.socket(family, kind, proto) as sock:
|
| 56 |
+
sock.settimeout(12)
|
| 57 |
+
sock.connect(address)
|
| 58 |
+
with ssl.create_default_context().wrap_socket(sock, server_hostname=host):
|
| 59 |
+
logger.warning("Network diagnostic: family=%s attempt=%d TLS=ok duration=%.2fs", family.name, attempt + 1, time.monotonic() - start)
|
| 60 |
+
except Exception as exc:
|
| 61 |
+
logger.warning("Network diagnostic: family=%s attempt=%d error=%s duration=%.2fs", family.name, attempt + 1, type(exc).__name__, time.monotonic() - start)
|
| 62 |
+
except Exception as exc:
|
| 63 |
+
logger.warning("Network diagnostic: DNS error=%s", type(exc).__name__)
|
| 64 |
+
|
| 65 |
+
threading.Thread(target=_temporary_network_probe, daemon=True).start()
|
| 66 |
+
|
| 67 |
|
| 68 |
allowed_origins = [item.strip() for item in os.getenv("OCR_ALLOWED_ORIGINS", "http://localhost:5173").split(",") if item.strip()]
|
| 69 |
app.add_middleware(CORSMiddleware, allow_origins=allowed_origins, allow_methods=["GET", "POST"], allow_headers=["*"], allow_credentials=True)
|