import os import sys from flask import Flask, render_template_string import markdown app = Flask(__name__) # ⚡ Bolt: Define file paths as constants to avoid redundant os.path.join calls. BASE_DIR = os.path.dirname(os.path.abspath(__file__)) README_PATH = os.path.join(BASE_DIR, '..', 'README.md') VERIFICATION_PATH = os.path.join(BASE_DIR, '..', 'VERIFICATION.md') class MarkdownCache: """ ⚡ Bolt: Performance optimization class for caching rendered Markdown. Only re-renders if the file's modification time (mtime) has changed. """ def __init__(self, filepath): self.filepath = filepath self.cached_html = "" self.last_mtime = 0 def get_html(self): if not os.path.exists(self.filepath): return f"
{os.path.basename(self.filepath)} not found.
" try: current_mtime = os.path.getmtime(self.filepath) # Only re-render if the file has been modified if current_mtime != self.last_mtime: with open(self.filepath, 'r', encoding='utf-8') as f: content = f.read() self.cached_html = markdown.markdown(content) self.last_mtime = current_mtime return self.cached_html except Exception as e: return f"Error rendering {os.path.basename(self.filepath)}: {str(e)}
" # Initialize caches readme_cache = MarkdownCache(README_PATH) verification_cache = MarkdownCache(VERIFICATION_PATH) @app.route('/') @app.route('/health') def health_check(): try: # ⚡ Bolt: Use pre-initialized caches to avoid redundant I/O and CPU-intensive parsing. html_readme = readme_cache.get_html() html_verification = verification_cache.get_html() return render_template_string("""MQL5 Trading Automation is running.
{{ html_verification|safe }}