Initial commit
This commit is contained in:
7
proxy/__init__.py
Executable file
7
proxy/__init__.py
Executable file
@@ -0,0 +1,7 @@
|
||||
from flask import Flask
|
||||
from .config import Config
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.from_object(Config)
|
||||
|
||||
from proxy import rss_proxy, healthcheck
|
||||
3
proxy/config.py
Executable file
3
proxy/config.py
Executable file
@@ -0,0 +1,3 @@
|
||||
class Config:
|
||||
"""Configuration class for Flask"""
|
||||
PROXY_URL = None # Set proxy URL if needed
|
||||
10
proxy/healthcheck.py
Executable file
10
proxy/healthcheck.py
Executable file
@@ -0,0 +1,10 @@
|
||||
from flask import Response
|
||||
from proxy import app
|
||||
|
||||
@app.route("/health")
|
||||
def healthcheck():
|
||||
"""Health check route to monitor service status"""
|
||||
try:
|
||||
return Response("OK", status=200)
|
||||
except Exception as e:
|
||||
return f"Error: {e}", 500
|
||||
27
proxy/rss_proxy.py
Executable file
27
proxy/rss_proxy.py
Executable file
@@ -0,0 +1,27 @@
|
||||
import urllib.parse
|
||||
from flask import request, Response
|
||||
import requests
|
||||
import os
|
||||
from proxy import app
|
||||
|
||||
PROXY_URL = os.getenv("PROXY_URL")
|
||||
|
||||
@app.route("/proxy")
|
||||
def proxy():
|
||||
"""Proxy RSS feed with forced re-encoding to UTF-8"""
|
||||
raw_query = request.query_string.decode()
|
||||
if raw_query.startswith("url="):
|
||||
url = urllib.parse.unquote(raw_query[4:])
|
||||
else:
|
||||
return "Missing URL", 400
|
||||
|
||||
try:
|
||||
proxies = {"http": PROXY_URL, "https": PROXY_URL} if PROXY_URL else None
|
||||
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
|
||||
response_text = r.text.replace('<?xml version="1.0" encoding="windows-1251"?>', '<?xml version="1.0" encoding="UTF-8"?>')
|
||||
|
||||
return Response(response_text, content_type="application/xml; charset=utf-8")
|
||||
except Exception as e:
|
||||
return f"Error: {e}", 500
|
||||
18
proxy/test.py
Executable file
18
proxy/test.py
Executable file
@@ -0,0 +1,18 @@
|
||||
import requests
|
||||
import sys
|
||||
|
||||
def check_health(url="http://localhost:5050/health"):
|
||||
try:
|
||||
response = requests.get(url, timeout=10)
|
||||
if response.status_code == 200:
|
||||
print("Health check passed")
|
||||
sys.exit(0) # Успешная проверка
|
||||
else:
|
||||
print(f"Health check failed: {response.status_code}")
|
||||
sys.exit(1) # Ошибка
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Health check failed: {e}")
|
||||
sys.exit(1) # Ошибка
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_health()
|
||||
22
proxy/tests/test_rss_proxy.py
Executable file
22
proxy/tests/test_rss_proxy.py
Executable file
@@ -0,0 +1,22 @@
|
||||
import unittest
|
||||
from proxy import app
|
||||
|
||||
|
||||
class FlaskTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.app = app.test_client()
|
||||
self.app.testing = True
|
||||
|
||||
def test_healthcheck(self):
|
||||
response = self.app.get('/health')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_proxy_missing_url(self):
|
||||
response = self.app.get('/proxy')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_proxy_with_url(self):
|
||||
test_url = 'https://tapochek.net/rss/rssdg.xml'
|
||||
response = self.app.get(f'/proxy?url={test_url}')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(response.data)
|
||||
Reference in New Issue
Block a user