From 4d68ed2a2413e0c8328a9bc68e1d522e39b8977f Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 1 Jul 2025 23:28:25 +0200 Subject: [PATCH] Solved caching bug --- app/app.py | 48 ++++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/app/app.py b/app/app.py index ac5ce64..b1e67fe 100644 --- a/app/app.py +++ b/app/app.py @@ -1,7 +1,5 @@ import os from flask import Flask, render_template -import requests -import hashlib import yaml from utils.configuration_resolver import ConfigurationResolver from utils.cache_manager import CacheManager @@ -14,39 +12,45 @@ cache_manager = CacheManager() cache_manager.clear_cache() def load_config(app): - """Load and resolve the configuration.""" + """Load and resolve the configuration from config.yaml.""" with open("config.yaml", "r") as f: config = yaml.safe_load(f) - # Resolve links in the configuration resolver = ConfigurationResolver(config) resolver.resolve_links() - # Update the app configuration app.config.update(resolver.get_config()) -app = Flask(__name__) -load_config(app) - -# Get the environment variable FLASK_ENV or set a default value -FLASK_ENV = os.getenv("FLASK_ENV", "production") - -@app.before_request -def reload_config_in_dev(): - if FLASK_ENV == "development": - load_config(app) - - # Cache the icons +def cache_icons_and_logos(app): + """Cache all icons and logos to local files.""" for card in app.config["cards"]: - # Just download the logo if an source url is passed - if card["icon"].get("source"): - card["icon"]["cache"] = cache_manager.cache_file(card["icon"]["source"]) - + icon = card.get("icon", {}) + if icon.get("source"): + icon["cache"] = cache_manager.cache_file(icon["source"]) + app.config["company"]["logo"]["cache"] = cache_manager.cache_file(app.config["company"]["logo"]["source"]) app.config["platform"]["favicon"]["cache"] = cache_manager.cache_file(app.config["platform"]["favicon"]["source"]) app.config["platform"]["logo"]["cache"] = cache_manager.cache_file(app.config["platform"]["logo"]["source"]) - + +# Get the environment variable FLASK_ENV or set a default value +FLASK_ENV = os.getenv("FLASK_ENV", "production") + +# Initialize Flask app +app = Flask(__name__) + +# Load configuration and cache assets on startup +load_config(app) +cache_icons_and_logos(app) + +@app.before_request +def reload_config_in_dev(): + """Reload config and recache icons before each request in development mode.""" + if FLASK_ENV == "development": + load_config(app) + cache_icons_and_logos(app) + @app.route('/') def index(): + """Render the main index page.""" cards = app.config["cards"] lg_classes, md_classes = compute_card_classes(cards) return render_template(