testing changing cache
This commit is contained in:
@@ -6,6 +6,6 @@ def init_healthcheck(app):
|
|||||||
def healthcheck():
|
def healthcheck():
|
||||||
"""Health check route to monitor service status"""
|
"""Health check route to monitor service status"""
|
||||||
try:
|
try:
|
||||||
return Response("Works normally", status=200)
|
return Response("healthy", status=200)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error: {e}", 500
|
return f"Error: {e}", 500
|
||||||
|
|||||||
@@ -3,62 +3,58 @@ import os
|
|||||||
import requests
|
import requests
|
||||||
import redis
|
import redis
|
||||||
import xml.etree.ElementTree as ET
|
import xml.etree.ElementTree as ET
|
||||||
from flask import Flask, request, Response
|
from flask import request, Response
|
||||||
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
PROXY_URL = os.getenv("PROXY_URL")
|
PROXY_URL = os.getenv("PROXY_URL")
|
||||||
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||||
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600)) # По умолчанию 1 час
|
CACHE_TTL = int(os.getenv("CACHE_TTL", 3600))
|
||||||
|
|
||||||
rdb = redis.from_url(REDIS_URL)
|
rdb = redis.from_url(REDIS_URL)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/proxy")
|
def init_proxy(app):
|
||||||
def proxy():
|
@app.route("/proxy")
|
||||||
"""Proxy RSS feed with per-item caching."""
|
def proxy():
|
||||||
raw_query = request.query_string.decode()
|
"""Proxy RSS feed with per-item caching."""
|
||||||
if raw_query.startswith("url="):
|
raw_query = request.query_string.decode()
|
||||||
url = urllib.parse.unquote(raw_query[4:])
|
if raw_query.startswith("url="):
|
||||||
else:
|
url = urllib.parse.unquote(raw_query[4:])
|
||||||
return "Missing URL", 400
|
else:
|
||||||
|
return "Missing URL", 400
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Получаем ленту
|
# Получаем ленту
|
||||||
proxies = {"http": PROXY_URL, "https": PROXY_URL} if PROXY_URL else None
|
proxies = {"http": PROXY_URL, "https": PROXY_URL} if PROXY_URL else None
|
||||||
r = requests.get(url, timeout=10, proxies=proxies)
|
r = requests.get(url, timeout=10, proxies=proxies)
|
||||||
r.encoding = "windows-1251" if "windows-1251" in r.headers.get("content-type", "").lower() else r.apparent_encoding
|
r.encoding = "windows-1251" if "windows-1251" in r.headers.get("content-type", "").lower() else r.apparent_encoding
|
||||||
xml_data = r.text.replace('<?xml version="1.0" encoding="windows-1251"?>', '<?xml version="1.0" encoding="UTF-8"?>')
|
xml_data = r.text.replace('<?xml version="1.0" encoding="windows-1251"?>', '<?xml version="1.0" encoding="UTF-8"?>')
|
||||||
|
|
||||||
# Разбираем XML
|
# Разбираем XML
|
||||||
root = ET.fromstring(xml_data)
|
root = ET.fromstring(xml_data)
|
||||||
items = root.findall(".//item")
|
items = root.findall(".//item")
|
||||||
|
|
||||||
cached_items = []
|
cached_items = []
|
||||||
new_items = []
|
new_items = []
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
guid = item.find("guid").text if item.find("guid") is not None else None
|
guid = item.find("guid").text if item.find("guid") is not None else None
|
||||||
if guid:
|
if guid:
|
||||||
cache_key = f"rss:item:{guid}"
|
cache_key = f"rss:item:{guid}"
|
||||||
cached_item = rdb.get(cache_key)
|
cached_item = rdb.get(cache_key)
|
||||||
|
|
||||||
if cached_item:
|
if cached_item:
|
||||||
cached_items.append(cached_item.decode())
|
cached_items.append(cached_item.decode())
|
||||||
else:
|
else:
|
||||||
item_str = ET.tostring(item, encoding="unicode")
|
item_str = ET.tostring(item, encoding="unicode")
|
||||||
rdb.setex(cache_key, CACHE_TTL, item_str)
|
rdb.setex(cache_key, CACHE_TTL, item_str)
|
||||||
new_items.append(item_str)
|
new_items.append(item_str)
|
||||||
|
|
||||||
# Собираем финальный RSS
|
# Собираем финальный RSS
|
||||||
final_items = cached_items + new_items
|
final_items = cached_items + new_items
|
||||||
response_xml = f'<?xml version="1.0" encoding="UTF-8"?><rss><channel>{"".join(final_items)}</channel></rss>'
|
response_xml = f'<?xml version="1.0" encoding="UTF-8"?><rss><channel>{"".join(final_items)}</channel></rss>'
|
||||||
|
|
||||||
return Response(response_xml, content_type="application/xml; charset=utf-8")
|
return Response(response_xml, content_type="application/xml; charset=utf-8")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Error: {e}", 500
|
return f"Error: {e}", 500
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
app.run(host="0.0.0.0", port=5050)
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class FlaskTestCase(unittest.TestCase):
|
|||||||
"""Check health endpoint."""
|
"""Check health endpoint."""
|
||||||
response = self.app.get('/health')
|
response = self.app.get('/health')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.data, b'Works normally')
|
self.assertEqual(response.data, b'healthy')
|
||||||
|
|
||||||
@patch("proxy.rss_proxy.requests.get")
|
@patch("proxy.rss_proxy.requests.get")
|
||||||
def test_proxy_success(self, mock_get):
|
def test_proxy_success(self, mock_get):
|
||||||
|
|||||||
Reference in New Issue
Block a user