File size: 1,797 Bytes
a26cbc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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)