-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
52 lines (43 loc) · 1.37 KB
/
Copy pathapp.py
File metadata and controls
52 lines (43 loc) · 1.37 KB
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
from flask import Flask, render_template, jsonify, request
from datetime import datetime, timedelta
import threading
app = Flask(__name__)
# Timer state
timer_data = {
'start_time': None,
'duration': timedelta(minutes=15),
'is_running': False
}
lock = threading.Lock()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/start', methods=['POST'])
def start_timer():
with lock:
timer_data['start_time'] = datetime.utcnow()
timer_data['is_running'] = True
return jsonify({'status': 'started'})
@app.route('/reset', methods=['POST'])
def reset_timer():
with lock:
timer_data['start_time'] = None
timer_data['is_running'] = False
return jsonify({'status': 'reset'})
@app.route('/time')
def get_time():
with lock:
if timer_data['is_running'] and timer_data['start_time']:
elapsed = datetime.utcnow() - timer_data['start_time']
remaining = timer_data['duration'] - elapsed
if remaining.total_seconds() <= 0:
remaining = timedelta(seconds=0)
timer_data['is_running'] = False
else:
remaining = timer_data['duration']
return jsonify({
'minutes': remaining.seconds // 60,
'seconds': remaining.seconds % 60
})
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)