-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
66 lines (53 loc) · 1.67 KB
/
app.py
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
62
63
64
65
66
import logging
from bot_main import run_bot
from flask import Flask, render_template, request
import threading
import atexit
# Setting up logging
logging.basicConfig(filename='app.log', filemode='a', format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('my_application')
# Setting up Flask application
app = Flask(__name__)
# This will hold our thread object
thread = None
def stop_thread():
if thread is not None:
thread.stop()
atexit.register(stop_thread)
class MyThread(threading.Thread):
def __init__(self):
self._stop_event = threading.Event()
super().__init__()
def stop(self):
self._stop_event.set()
def run(self):
while not self._stop_event.is_set():
try:
# Call your main function here
run_bot()
except Exception as e:
logger.error("Error in main function", exc_info=True)
@app.route('/start', methods=['POST'])
def start_script():
global thread
if thread is not None and thread.is_alive():
return "Script already running"
thread = MyThread()
thread.start()
return "Script started"
@app.route('/stop', methods=['POST'])
def stop_script():
global thread
if thread is not None and thread.is_alive():
thread.stop()
thread = None
return "Script stopped"
return "Script not running"
@app.route('/logs', methods=['GET', 'POST']) # Updated this line
def view_logs():
with open('app.log', 'r') as log_file:
content = log_file.read()
return content
# add more routes for other features...
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000, debug=True)