-
Notifications
You must be signed in to change notification settings - Fork 20
/
manage.py
84 lines (67 loc) · 1.8 KB
/
manage.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
Created by liaoyangyang1 on 2018/8/21 下午3:40.
"""
import platform
from flask_script import Manager
from apps import create_app, db
from flask_migrate import Migrate
from utils.helper import start_service, register_consul, stop_service, deregister_consul
app = create_app()
migrate = Migrate(app, db) # 第二课新增
app.debug = app.config["DEBUG"]
# Init manager object via app object
manager = Manager(app)
@manager.command
def runserver(): # 第二课新增
"""
Recreates a local database. You probably should not use this on
production.
"""
sysstr = platform.system()
if (sysstr == "Windows"):
app.run(host=app.config['HOST'], port=app.config['PORT'])
register_consul()
else:
start_service('gunicorn')
register_consul()
@manager.command
def stopserver(): # 第二课新增
"""
Recreates a local database. You probably should not use this on
production.
"""
try:
deregister_consul()
stop_service('gunicorn')
except Exception as e:
raise e
@manager.shell
def make_shell_context():
"""Create a python CLI.
return: Default import object
type: `Dict`
"""
# 确保有导入 Flask app object,否则启动的 CLI 上下文中仍然没有 app 对象
return dict(app=app, db=db) # 第二课新增
# 创建数据库脚本
@manager.command
def create_db():
"""
Recreates a local database. You probably should not use this on
production.
"""
db.create_all()
db.session.commit()
@manager.command
def recreate_db():
"""
Recreates a local database. You probably should not use this on
production.
"""
db.drop_all()
db.create_all()
db.session.commit()
if __name__ == '__main__':
manager.run()