import os import sys import time from flask import Flask, render_template_string import markdown app = Flask(__name__) # ⚡ Bolt: Pre-calculate absolute paths once to avoid redundant os.path.join calls on every request. 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') # ⚡ Bolt: MarkdownCache class to optimize rendering performance. # Caches the rendered HTML and only re-renders if the file's modification time (mtime) changes. # This eliminates redundant I/O and CPU-intensive markdown processing for static content. class MarkdownCache: def __init__(self, filepath, fallback_text): self.filepath = filepath self.fallback_text = fallback_text self.cached_html = None self.last_mtime = 0 def get_html(self): try: if not os.path.exists(self.filepath): return self.fallback_text current_mtime = os.path.getmtime(self.filepath) if self.cached_html is None or 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 # print(f"DEBUG: Cache refreshed for {self.filepath}") return self.cached_html except Exception as e: return f"

Error loading content: {str(e)}

" # Initialize caches readme_cache = MarkdownCache(README_PATH, "

README.md not found.

") verification_cache = MarkdownCache(VERIFICATION_PATH, "

VERIFICATION.md not found.

") @app.route('/') @app.route('/health') def health_check(): try: # ⚡ Bolt: Use cached HTML instead of reading and rendering on every request. html_readme = readme_cache.get_html() html_verification = verification_cache.get_html() return render_template_string(""" MQL5 Trading Automation Dashboard

System Status ONLINE

MQL5 Trading Automation is running.

{{ html_verification|safe }}

Project Documentation

{{ html_readme|safe }}
""", html_readme=html_readme, html_verification=html_verification, year=time.strftime("%Y")) 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)