feat: add home route and template for proxy instructions

Integrated a new home route in the application that renders a template with instructions on how to use the Google Fonts proxy service, enhancing user guidance. This change includes the addition of an `index.html` template, which provides a clear example and usage instructions, and minor refactoring of existing routes for improved readability.
This commit is contained in:
Kumi 2024-08-05 16:16:37 +02:00
parent f77ec2c177
commit 5d32aa7c8f
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 138 additions and 19 deletions

92
app.py
View file

@ -1,15 +1,22 @@
from flask import Flask, request, Response
from flask import Flask, request, Response, render_template
import requests
import re
app = Flask(__name__)
FONT_STATIC_URL = 'https://fonts.gstatic.com'
FONT_API_URL = 'https://fonts.googleapis.com'
FONT_STATIC_URL = "https://fonts.gstatic.com"
FONT_API_URL = "https://fonts.googleapis.com"
PATTERN = rb'fonts\.gstatic\.com'
PATTERN = rb"fonts\.gstatic\.com"
@app.route('/s/<path:path>', methods=['GET'])
@app.route("/")
def home():
domain = request.url_root
return render_template("index.html", domain=domain)
@app.route("/s/<path:path>", methods=["GET"])
def proxy_fonts_static(path):
url = f"{FONT_STATIC_URL}/s/{path}"
response = requests.get(url, stream=True)
@ -19,34 +26,81 @@ def proxy_fonts_static(path):
yield chunk
# Remove hop-by-hop headers
headers = {key: value for key, value in response.headers.items() if key.lower() not in [
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'transfer-encoding', 'upgrade']}
headers = {
key: value
for key, value in response.headers.items()
if key.lower()
not in [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
]
}
return Response(generate(), status=response.status_code, headers=headers)
@app.route('/<path:path>', methods=['GET'])
@app.route("/<path:path>", methods=["GET"])
def proxy_fonts_api(path):
query_string = request.query_string.decode('utf-8')
query_string = request.query_string.decode("utf-8")
url = f"{FONT_API_URL}/{path}"
if query_string:
url += f"?{query_string}"
headers = {key: value for (key, value) in request.headers.items() if key.lower() != 'host'}
headers['Accept-Encoding'] = '' # Disable Accept-Encoding to avoid compressed responses
headers = {
key: value for (key, value) in request.headers.items() if key.lower() != "host"
}
headers["Accept-Encoding"] = (
"" # Disable Accept-Encoding to avoid compressed responses
)
response = requests.get(url, headers=headers)
replacement = request.host.encode('utf-8') # Convert the host to bytes for replacement
replacement = request.host.encode(
"utf-8"
) # Convert the host to bytes for replacement
if 'text/css' in response.headers.get('Content-Type', ''):
if "text/css" in response.headers.get("Content-Type", ""):
content = response.content
content = re.sub(PATTERN, replacement, content)
headers = {key: value for key, value in response.headers.items() if key.lower() not in [
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'transfer-encoding', 'upgrade']}
headers = {
key: value
for key, value in response.headers.items()
if key.lower()
not in [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
]
}
return Response(content, status=response.status_code, headers=headers)
else:
# For non-CSS content, return as is
headers = {key: value for key, value in response.headers.items() if key.lower() not in [
'connection', 'keep-alive', 'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'transfer-encoding', 'upgrade']}
headers = {
key: value
for key, value in response.headers.items()
if key.lower()
not in [
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade",
]
}
return Response(response.content, status=response.status_code, headers=headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5678)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5678)