more changes

This commit is contained in:
Karl 2025-07-17 16:58:24 +01:00
parent c9329a33ea
commit 051e5fb290

38
app.py
View File

@ -6,7 +6,6 @@ import requests.auth
import os import os
import base64 import base64
from typing import Dict, Any, Tuple, Union from typing import Dict, Any, Tuple, Union
from pywebpush import webpush, WebPushException
from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired
from lib.reqs import (get_urls, get_user_accounts, add_user_account, from lib.reqs import (get_urls, get_user_accounts, add_user_account,
@ -91,26 +90,35 @@ def index() -> Union[Response, str]:
return redirect(url_for("home")) return redirect(url_for("home"))
return render_template("index.html") return render_template("index.html")
@app.route('/vapid-public-key') @app.route('/vapid-public-key', methods=['GET'])
def vapid_public_key(): def proxy_vapid_public_key():
"""Provides the VAPID public key to the client.""" """Proxies the request for the VAPID public key to the backend."""
return jsonify({'public_key': app.config['VAPID_PUBLIC_KEY']}) backend_url = f"{app.config['BASE_URL']}/vapid-public-key"
try:
response = requests.get(backend_url)
return Response(response.content, status=response.status_code, mimetype=response.headers['Content-Type'])
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 502
@app.route('/save-subscription', methods=['POST']) @app.route('/save-subscription', methods=['POST'])
def save_subscription(): def proxy_save_subscription():
"""Saves a push notification subscription.""" """Proxies the request to save a push subscription to the backend."""
if not session.get("logged_in"): if not session.get("logged_in"):
return jsonify({'error': 'Unauthorized'}), 401 return jsonify({'error': 'Unauthorized'}), 401
subscription_data = request.get_json() backend_url = f"{app.config['BASE_URL']}/save-subscription"
if not subscription_data: credentials = base64.b64decode(session["auth_credentials"]).decode()
return jsonify({'error': 'No subscription data received'}), 400 username, password = credentials.split(":", 1)
# Here you would typically save the subscription to a database try:
# For this example, we'll just store it in the session response = requests.post(
session['push_subscription'] = subscription_data backend_url,
print(f"Subscription saved for user: {session['username']}") auth=requests.auth.HTTPBasicAuth(username, password),
return jsonify({'success': True}) json=request.get_json()
)
return Response(response.content, status=response.status_code, mimetype=response.headers['Content-Type'])
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 502
@app.route("/home") @app.route("/home")
@cache.cached(timeout=60) @cache.cached(timeout=60)