From 328f872588f4eb22cc4f9781a4416ceab67c0f8e Mon Sep 17 00:00:00 2001 From: fretuks Date: Tue, 16 Dec 2025 22:36:44 +0100 Subject: [PATCH] Implement a number of small fixes --- app.py | 42 +++++++++++++++++++------------------- requirements.txt | Bin 446 -> 226 bytes static/style.css | 2 +- static/suggestion.js | 2 +- templates/about_us.html | 2 +- templates/suggestion.html | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/app.py b/app.py index e376573..bb9a7bb 100644 --- a/app.py +++ b/app.py @@ -1,18 +1,15 @@ -import json import os import time -from urllib import request as urlrequest from urllib.error import URLError, HTTPError -import requests -from flask import Flask, render_template, jsonify, request -from flask.cli import load_dotenv +import requests +from flask import Flask, render_template, jsonify, request, Blueprint +from werkzeug.middleware.proxy_fix import ProxyFix app = Flask(__name__) - +app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_proto=1, x_host=1) app.config["MAX_CONTENT_LENGTH"] = 16 * 1024 -load_dotenv() DISCORD_WEBHOOK_URL = os.getenv("DISCORD_WEBHOOK_URL") COOLDOWN_SECONDS = 60 * 60 @@ -30,6 +27,8 @@ PROHIBITED_WORDS = { 'git', 'shag', 'minge', 'nonce', 'plonker', 'pillock' } +api = Blueprint("api", __name__, url_prefix="/api") + def _get_client_ip() -> str: xff = request.headers.get("X-Forwarded-For", "").split(",")[0].strip() @@ -47,17 +46,19 @@ def _post_to_discord(payload: dict) -> None: url = DISCORD_WEBHOOK_URL.strip() if "?" not in url: url += "?wait=true" + headers = { "User-Agent": "TRLStatsFeedback/1.0 (contact: fretux@fretux.ch)", "Content-Type": "application/json", "Accept": "application/json", } + resp = requests.post(url, json=payload, headers=headers, timeout=10) if resp.status_code not in (200, 204): raise RuntimeError(f"Discord returned {resp.status_code}: {resp.text[:500]}") -@app.post("/api/feedback") +@api.post("/feedback") def api_feedback(): ip = _get_client_ip() now = time.time() @@ -76,7 +77,6 @@ def api_feedback(): rating = data.get("rating") comment = (data.get("comment") or "").strip() - # Normalize and validate if len(name) > 32: name = name[:32] @@ -97,10 +97,7 @@ def api_feedback(): content = f"**Rating:** {rating_int}\n**Comment:**\n{comment}" try: - _post_to_discord({ - "username": name, - "content": content - }) + _post_to_discord({"username": name, "content": content}) except (HTTPError, URLError, RuntimeError) as e: return jsonify({"ok": False, "error": f"Failed to forward feedback: {str(e)}"}), 502 @@ -108,24 +105,27 @@ def api_feedback(): return jsonify({"ok": True}), 200 -@app.route('/') +app.register_blueprint(api) + + +@app.route("/") def index(): - return render_template('index.html') + return render_template("index.html") -@app.route('/calculator') +@app.route("/calculator") def calculator(): - return render_template('calculator.html') + return render_template("calculator.html") -@app.route('/about_us') +@app.route("/about_us") def about_us(): - return render_template('about_us.html') + return render_template("about_us.html") -@app.route('/suggestion') +@app.route("/suggestion") def suggestion(): - return render_template('suggestion.html') + return render_template("suggestion.html") if __name__ == '__main__': diff --git a/requirements.txt b/requirements.txt index 87692d1f35425d8d6c7c9f7fcf6a6f59f77da6df..13ef7763056d0aaca79d990100ff7e5dd3adfed7 100644 GIT binary patch literal 226 zcmYjLJrBYl5Z&*;cqz1gEDSCVMhDZu(IB3PrBHz;)?Y8JnwY%h-o39tkDg+RLU1fI z;B1X+Cx?;v`woC9v(BR2#I{ze96x2%1$B`vQi)fqGHO7dzkD{9P*ca~oU=7SM z!vsUJ18kY(h>NLdLVk`-M05)I67oWv-i(iC%x-}-JrhLa