-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathinit.py
111 lines (87 loc) · 2.72 KB
/
init.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import logging
import os
from flask import Flask, request, redirect, url_for, jsonify
from flask_babel import Babel, gettext
from flask_sqlalchemy import SQLAlchemy
from util import session_util, file_util
from util.schedule_util import start_schedule
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
app = Flask(__name__)
babel = Babel(app)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 6307200
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////etc/v2-ui/v2-ui.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
need_login_bps = []
common_context = {}
@babel.localeselector
def get_locale():
match = request.accept_languages.best_match(['zh-TW', 'zh-HK', 'zh-CN', 'zh', 'en'], 'en')
if 'TW' in match or 'HK' in match:
return 'zh_Hant'
if 'zh' in match:
return 'zh_Hans'
return 'en'
def init_db():
from v2ray.models import Inbound
from base.models import User, Setting
User.__name__.lower()
Inbound.__name__.lower()
Setting.__name__.lower()
file_util.mkdirs('/etc/v2-ui/')
db.create_all()
def init_app():
from util import config
app.secret_key = config.get_secret_key()
def init_common_context():
from util import config
global common_context
common_context = {
'cur_ver': config.get_current_version(),
'base_path': '' if app.debug else config.get_base_path(),
}
def init_bps():
from util import config
from base.router import base_bp
from server.router import server_bp
from v2ray.router import v2ray_bp
bps = [
base_bp,
v2ray_bp,
server_bp,
]
if not app.debug:
base_path = config.get_base_path()
for bp in bps:
bp.url_prefix = base_path + (bp.url_prefix if bp.url_prefix else '')
global need_login_bps
need_login_bps += [v2ray_bp, server_bp]
[app.register_blueprint(bp) for bp in bps]
def init_v2_jobs():
from util import v2_jobs
v2_jobs.init()
def is_ajax():
return request.headers.get('X-Requested-With') == 'XMLHttpRequest'
@app.before_request
def before():
from base.models import Msg
if not session_util.is_login():
for bp in need_login_bps:
if request.path.startswith(bp.url_prefix):
if is_ajax():
return jsonify(Msg(False, gettext('You has been logout, please refresh this page and login again')))
else:
return redirect(url_for('base.index'))
@app.errorhandler(500)
def error_handle(e):
from base.models import Msg
logging.warning(e)
response = jsonify(Msg(False, str(e)))
response.status_code = 200
return response
init_db()
init_app()
init_common_context()
init_bps()
init_v2_jobs()
start_schedule()