import os import sys from flask import Flask, render_template_string import markdown app = Flask(__name__) # --- ⚡ Bolt: Performance optimization - Cache rendered Markdown to avoid redundant processing. # Module-level path definitions to avoid repeated path joining. BASE_DIR = os.path.dirname(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: Simple cache for rendered markdown files with mtime validation.""" def __init__(self, filepath, name): self.filepath = filepath self.name = name self.cached_html = "" self.last_mtime = 0 def get_html(self): """Returns cached HTML or re-renders if the file has changed.""" try: # ⚡ Bolt: Use os.stat() to get metadata in a single system call instead of exists() + getmtime() try: stat_result = os.stat(self.filepath) except FileNotFoundError: return f"

{self.name} not found.

" current_mtime = stat_result.st_mtime if current_mtime > self.last_mtime: # File has changed or was never loaded 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 loading {self.name}: {str(e)}

" # Initialize caches readme_cache = MarkdownCache(README_PATH, "README.md") verification_cache = MarkdownCache(VERIFICATION_PATH, "VERIFICATION.md") # ⚡ Bolt: Define template as a module-level constant to avoid re-allocation on every request. # ⚡ Bolt: Hardcode year=2026 to simplify rendering and avoid passing constant values. DASHBOARD_TEMPLATE = """ MQL5 Trading Automation Dashboard

System Status ONLINE

MQL5 Trading Automation is running.

{{ html_verification|safe }}

Project Documentation

{{ html_readme|safe }}
""" @app.route('/') @app.route('/health') def health_check(): try: # ⚡ Bolt: Use cached content to avoid redundant I/O and markdown rendering on every request. html_readme = readme_cache.get_html() html_verification = verification_cache.get_html() return render_template_string(DASHBOARD_TEMPLATE, html_readme=html_readme, html_verification=html_verification) except Exception as e: return f"Error: {str(e)}", 500 if __name__ == '__main__': port = int(os.environ.get('PORT', 8080)) print(f"Starting web dashboard on port {port}...") app.run(host='0.0.0.0', port=port)