-
Notifications
You must be signed in to change notification settings - Fork 29
/
web.py
97 lines (73 loc) · 2.54 KB
/
web.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import logging
from logging import StreamHandler
from logging.handlers import TimedRotatingFileHandler
import multiprocessing
import random
import string
import sys
from threading import Thread
from time import sleep
from urllib import request
import webbrowser
from flask import Flask
import socketio
from utils.config import Config, static_path, data_path
from utils.storage import Storage
from webapp.backend import Backend
from webapp.index import Index
def url_ok(url):
try:
request.urlopen(url=url)
return True
except Exception as e:
return False
def run(browser=True):
port = 5000
if len(sys.argv) > 1:
port = int(sys.argv[1])
app = Flask(__name__, static_folder=static_path)
app.register_blueprint(Index().register())
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
console = StreamHandler()
console.setLevel(logging.DEBUG)
console.setFormatter(formatter)
logger.addHandler(console)
if not app.debug:
file = TimedRotatingFileHandler(data_path + "/error.log", when="w0", backupCount=14)
file.setLevel(logging.ERROR)
file.setFormatter(formatter)
logger.addHandler(file)
try:
config = Config()
secret_key = config.read("secret_key")
if not secret_key:
secret_key = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in range(16))
config.write("secret_key", secret_key)
app.secret_key = secret_key
Storage().init()
sockets = socketio.Server()
app.wsgi_app = socketio.Middleware(sockets, app.wsgi_app)
sockets.register_namespace(Backend())
def open_in_browser():
logging.info("Application is starting...")
url = "http://127.0.0.1:%s" % port
while not url_ok(url):
sleep(0.5)
logging.info("Application is available at " + url)
if not app.debug and browser:
webbrowser.open(url)
Thread(target=open_in_browser, daemon=True).start()
app.run(host="0.0.0.0", port=port, threaded=True, use_reloader=False)
except (KeyboardInterrupt, SystemExit):
raise
except:
logging.exception(sys.exc_info()[0])
if __name__ == "__main__":
if len(sys.argv) > 1 and "fork" in sys.argv[1]:
multiprocessing.freeze_support()
exit(0)
if sys.platform.startswith("win"):
multiprocessing.freeze_support()
run()