-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrun.py
91 lines (80 loc) · 2.51 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- 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 init_admin(app):
'''初始化管理员用户'''
with app.test_request_context():
from backend.admin.models import User
db = app.db
default_email = '[email protected]'
if User.query.filter_by(email=default_email).count() < 1:
user_obj = User()
user_obj.name = '玖亖伍'
user_obj.email = default_email
user_obj.password = User.encrypt_string('administrator')
user_obj.is_admin = True
db.session.add(user_obj)
db.session.commit()
print('添加初始数据成功')
else:
user_obj = User.query.first().to_dict()
if isinstance(user_obj, User):
print(user_obj)
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)
init_admin(app)
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
app.run(**run_cfg)
if __name__ == '__main__':
run_cfg = {
'host': '0.0.0.0',
'port': 5556,
'debug': True,
'threaded': True
}
start_server(run_cfg)