import os import sys from flask import Flask, render_template_string, jsonify import markdown import time app = Flask(__name__) # Cache storage: filepath -> (mtime, html_content) _content_cache = {} # Constants for paths to avoid re-calculating on every request 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') def get_cached_markdown(filepath): """ Returns the markdown content of a file converted to HTML, using a cache that invalidates based on file modification time. Optimization: Uses os.stat() to get mtime and check existence in one syscall. """ try: # Optimization: os.stat gets existence and mtime in one call # removing the need for separate os.path.exists() check stat_result = os.stat(filepath) except OSError: return None try: mtime = stat_result.st_mtime if filepath in _content_cache: cached_mtime, cached_html = _content_cache[filepath] if cached_mtime == mtime: return cached_html # Cache miss or file changed with open(filepath, 'r', encoding='utf-8') as f: content = f.read() html_content = markdown.markdown(content) _content_cache[filepath] = (mtime, html_content) return html_content except Exception as e: print(f"Error reading/converting {filepath}: {e}") return None @app.after_request def add_security_headers(response): """Add security headers to the response.""" # Content Security Policy # - default-src 'self': Only allow resources from same origin by default # - script-src 'self': Only allow scripts from same origin (blocks inline scripts & external) # - style-src 'self' 'unsafe-inline': Allow inline styles (template uses

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=2026) 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)