-
Notifications
You must be signed in to change notification settings - Fork 18
/
run.py
75 lines (66 loc) · 2.03 KB
/
run.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
# -*- coding: utf-8 -*-
'''测试阶段-启动文件'''
import os
import sys
from werkzeug.routing import EndpointPrefix
from backend.startup import create_app
from backend.utils import touch, md5
def get_site_version(root):
'''获取网站版本'''
vf = os.path.join(root, 'site.version')
if not os.path.exists(vf):
touch(vf)
v = None
with open(vf, 'r+') as f:
v = f.read().strip()
if not bool(v):
v = '0.0.0'
f.write(v)
return v
def start_server(run_cfg=None, is_deploy=False):
'''启动web服务器'''
if not bool(run_cfg):
run_cfg = {}
proj_root = os.path.abspath(os.path.dirname(__file__))
os.environ['PROJ_ROOT'] = proj_root
site_version = get_site_version(proj_root)
os.environ['SITE_VERSION'] = site_version
config = {
'use_cdn': False,
'debug': run_cfg.get('debug', False),
'secret': md5('!secret!'),
'url_prefix': None,
'debugtoolbar': True
}
app = create_app(config)
app.proj_root = proj_root
app.site_version = site_version
@app.before_first_request
def init_user(*args, **kwargs):
print(args)
print(kwargs)
if 'host' in run_cfg and 'port' in run_cfg:
print_host = run_cfg['host']
if print_host == '0.0.0.0':
if sys.platform == 'win32' or os.name == 'nt':
print_host = '127.0.0.1'
print('=' * 28, 'visit by', '=' * 28)
print(' http://{0}:{1}/'.format(print_host, run_cfg['port']))
print('=' * 66)
if is_deploy:
return app
# ##########[ Debug Print-URL_MAP ----------------
if config['debug']:
for rule in app.url_map.iter_rules():
print(rule.endpoint, '->\n ', rule.rule)
print('.' * 60)
# ---------- Debug Print-URL_MAP ]###############
app.run(**run_cfg)
if __name__ == '__main__':
run_cfg = {
'host': '0.0.0.0',
'port': 5556,
'debug': True,
'threaded': True
}
start_server(run_cfg)