typenot/app.py
Kumi 8a8a1ca7cd
feat: Add README and make port configurable via env
Introduces a README file with setup and usage instructions for
TypeNot, highlighting its privacy benefits and configuration
options.

Updates the application to allow port configuration with an
environment variable, enhancing deployment flexibility.
2024-11-27 12:43:40 +01:00

76 lines
2.1 KiB
Python

from flask import Flask, request, Response, render_template
import requests
import re
import os
app = Flask(__name__)
FONT_URL = "https://use.typekit.net"
PATTERN = r"use\.typekit\.net"
# P_PATTERN: Any *line* that contains p.typekit.net will be removed
P_PATTERN = r".*p\.typekit\.net.*\n"
@app.route("/")
def home():
domain = request.url_root
return render_template("index.html", domain=domain)
@app.route("/<path:path>", methods=["GET"])
def proxy_fonts(path):
query_string = request.query_string.decode("utf-8")
url = f"{FONT_URL}/{path}"
if query_string:
url += f"?{query_string}"
headers = {
"Accept-Encoding": "",
}
response = requests.get(url, headers=headers)
if "text/css" in response.headers.get("Content-Type", ""):
content = response.content.decode("utf-8")
content = re.sub(PATTERN, request.host, content)
content = re.sub(P_PATTERN, "", 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",
]
}
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",
]
}
return Response(response.content, status=response.status_code, headers=headers)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=os.environ.get("PORT", 5690))