/
storeksfeed
/
RAG-Bot
Обзор
Документация
Войти
/
storeksfeed
/
RAG-Bot
Код
Запросы
0
Задачи
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
app/core/proxy_views.py
143 строки
4 KB
Semyon Glazyrin
Fixed LightRAG swagger's pages proxying
01 май 2026, 11:08
Верифицирован
01 май 2026, 11:08
41ac45d
Код
Авторство
О чём код?
""" Reverse proxy views for LightRAG integration. """ import os from urllib.parse import urlparse import requests from django.http import HttpResponse, StreamingHttpResponse from django.views.decorators.csrf import csrf_exempt from django.conf import settings LIGHTRAG_BASE_URL = os.getenv('LIGHTRAG_BASE_URL', 'http://lightrag:9621') def _is_lightrag_referer(referer: str) -> bool: """Return True if request referer is a proxied LightRAG page.""" if not referer: return False referer_path = urlparse(referer).path or '' return referer_path.startswith('/lightrag') or referer_path.startswith('/docs') @csrf_exempt def lightrag_proxy(request, path=''): """ Proxy requests to LightRAG service. Handles: 1. Direct requests to /lightrag/* -> lightrag:9621/* 2. Requests with referer from /lightrag pages """ # Build target URL target_url = f"{LIGHTRAG_BASE_URL}/{path}" # Copy headers, excluding some that shouldn't be forwarded headers = {} for key, value in request.headers.items(): # Skip hop-by-hop headers if key.lower() not in ['host', 'connection', 'accept-encoding', 'content-length']: headers[key] = value # Rewrite referer host from Django app URL to LightRAG URL. if 'Referer' in headers and _is_lightrag_referer(headers['Referer']): parsed = urlparse(headers['Referer']) headers['Referer'] = f"{LIGHTRAG_BASE_URL}{parsed.path}" if parsed.query: headers['Referer'] += f"?{parsed.query}" # Prepare request parameters params = dict(request.GET.items()) try: # Forward the request if request.method == 'GET': resp = requests.get( target_url, params=params, headers=headers, stream=True, timeout=600 ) elif request.method == 'POST': resp = requests.post( target_url, params=params, data=request.body, headers=headers, stream=True, timeout=600 ) elif request.method == 'PUT': resp = requests.put( target_url, params=params, data=request.body, headers=headers, stream=True, timeout=600 ) elif request.method == 'PATCH': resp = requests.patch( target_url, params=params, data=request.body, headers=headers, stream=True, timeout=600 ) elif request.method == 'DELETE': resp = requests.delete( target_url, params=params, data=request.body, headers=headers, stream=True, timeout=600 ) else: return HttpResponse(f"Method {request.method} not supported", status=405) # Create response excluded_headers = ['content-encoding', 'content-length', 'transfer-encoding', 'connection'] response_headers = { key: value for key, value in resp.headers.items() if key.lower() not in excluded_headers } # Stream the response response = StreamingHttpResponse( resp.iter_content(chunk_size=8192), status=resp.status_code ) # Copy headers to response for key, value in response_headers.items(): response[key] = value return response except requests.exceptions.RequestException as e: return HttpResponse(f"Proxy error: {str(e)}", status=502) @csrf_exempt def check_and_proxy(request, path=''): """ Check if request should be proxied based on referer header. This view is used for paths that might need proxying based on referer. """ referer = request.headers.get('Referer', '') # Check if referer is from a proxied LightRAG page. if _is_lightrag_referer(referer): # Use the path parameter if provided, otherwise extract from request if not path: path = request.path.lstrip('/') return lightrag_proxy(request, path) # If not from lightrag, return 404 return HttpResponse("Not Found", status=404)