from flask import Flask, render_template, request, jsonify from load_tester import tester import os app = Flask(__name__) app.config['SECRET_KEY'] = os.urandom(24) # Change Jinja2 delimiters to avoid conflict with Vue.js app.jinja_env.variable_start_string = '[[' app.jinja_env.variable_end_string = ']]' app.jinja_env.block_start_string = '[%' app.jinja_env.block_end_string = '%]' @app.route('/') def index(): try: return render_template('index.html') except Exception as e: return f"Error loading template: {str(e)}", 500 @app.errorhandler(404) def page_not_found(e): if request.path.startswith('/api/'): return jsonify({'error': 'Not found'}), 404 return "Page not found", 404 @app.errorhandler(500) def internal_server_error(e): if request.path.startswith('/api/'): return jsonify({'error': 'Internal server error'}), 500 return "Internal server error", 500 @app.route('/api/start', methods=['POST']) def start_test(): data = request.json try: # Basic validation if not data.get('url'): return jsonify({'error': 'URL is required'}), 400 # Start test success = tester.start(data) if success: return jsonify({'message': 'Test started successfully', 'status': 'running'}) else: return jsonify({'error': 'Test already running'}), 409 except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/api/stop', methods=['POST']) def stop_test(): tester.stop() return jsonify({'message': 'Test stopped', 'status': 'stopped'}) @app.route('/api/stats', methods=['GET']) def get_stats(): return jsonify(tester.get_stats()) if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=True)