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 base64
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.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 render_template("index.html")
@app.route('/vapid-public-key')
def vapid_public_key():
"""Provides the VAPID public key to the client."""
return jsonify({'public_key': app.config['VAPID_PUBLIC_KEY']})
@app.route('/vapid-public-key', methods=['GET'])
def proxy_vapid_public_key():
"""Proxies the request for the VAPID public key to the backend."""
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'])
def save_subscription():
"""Saves a push notification subscription."""
def proxy_save_subscription():
"""Proxies the request to save a push subscription to the backend."""
if not session.get("logged_in"):
return jsonify({'error': 'Unauthorized'}), 401
subscription_data = request.get_json()
if not subscription_data:
return jsonify({'error': 'No subscription data received'}), 400
backend_url = f"{app.config['BASE_URL']}/save-subscription"
credentials = base64.b64decode(session["auth_credentials"]).decode()
username, password = credentials.split(":", 1)
# Here you would typically save the subscription to a database
# For this example, we'll just store it in the session
session['push_subscription'] = subscription_data
print(f"Subscription saved for user: {session['username']}")
return jsonify({'success': True})
try:
response = requests.post(
backend_url,
auth=requests.auth.HTTPBasicAuth(username, password),
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")
@cache.cached(timeout=60)