web: add httpx
Local Docker Build (Dev) / build-dev (push) Successful in 5s

This commit is contained in:
2025-10-22 17:12:10 -04:00
parent 93fd3789e2
commit 7261ba8884
+38 -12
View File
@@ -1327,8 +1327,12 @@ def register_web_routes(app, dependencies):
@app.post("/manual/scan") @app.post("/manual/scan")
async def api_manual_scan(request: Request): async def api_manual_scan(request: Request):
"""Proxy manual scan requests to core container""" """Proxy manual scan requests to core container"""
import httpx import urllib.request
import urllib.parse
import urllib.error
import json
import os import os
import socket
# Get request body # Get request body
try: try:
@@ -1342,21 +1346,38 @@ def register_web_routes(app, dependencies):
core_url = f"http://{core_host}:{core_port}/manual/scan" core_url = f"http://{core_host}:{core_port}/manual/scan"
try: try:
async with httpx.AsyncClient(timeout=30.0) as client: # Prepare request data
response = await client.post(core_url, json=body) data = json.dumps(body).encode('utf-8')
return response.json()
except httpx.TimeoutException: # Create request with timeout
raise HTTPException(status_code=504, detail="Core container request timed out") req = urllib.request.Request(
except httpx.RequestError as e: core_url,
data=data,
headers={'Content-Type': 'application/json'}
)
# Make request with timeout
with urllib.request.urlopen(req, timeout=30) as response:
response_data = response.read().decode('utf-8')
return json.loads(response_data)
except urllib.error.HTTPError as e:
raise HTTPException(status_code=e.code, detail=f"Core container HTTP error: {e.reason}")
except urllib.error.URLError as e:
raise HTTPException(status_code=503, detail=f"Could not connect to core container: {str(e)}") raise HTTPException(status_code=503, detail=f"Could not connect to core container: {str(e)}")
except socket.timeout:
raise HTTPException(status_code=504, detail="Core container request timed out")
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=f"Manual scan request failed: {str(e)}") raise HTTPException(status_code=500, detail=f"Manual scan request failed: {str(e)}")
@app.get("/api/scan/status") @app.get("/api/scan/status")
async def api_scan_status(): async def api_scan_status():
"""Proxy scan status requests to core container""" """Proxy scan status requests to core container"""
import httpx import urllib.request
import urllib.error
import json
import os import os
import socket
# Get core container URL # Get core container URL
core_host = os.environ.get("CORE_API_HOST", "nfoguard-core") core_host = os.environ.get("CORE_API_HOST", "nfoguard-core")
@@ -1364,10 +1385,15 @@ def register_web_routes(app, dependencies):
core_url = f"http://{core_host}:{core_port}/api/scan/status" core_url = f"http://{core_host}:{core_port}/api/scan/status"
try: try:
async with httpx.AsyncClient(timeout=10.0) as client: # Create request with timeout
response = await client.get(core_url) req = urllib.request.Request(core_url)
return response.json()
except httpx.RequestError: # Make request with timeout
with urllib.request.urlopen(req, timeout=10) as response:
response_data = response.read().decode('utf-8')
return json.loads(response_data)
except (urllib.error.URLError, socket.timeout):
# If core is unreachable, return default status # If core is unreachable, return default status
return {"scanning": False, "message": "Core container unavailable"} return {"scanning": False, "message": "Core container unavailable"}
except Exception: except Exception: