File size: 2,288 Bytes
960cd20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os.path

from flask import Flask
from flask_apscheduler import APScheduler
from flask_login import LoginManager
from flask_wtf import CSRFProtect

from utils.data_utils import clean_folder
from utils.phrases_dict import phrases_dict_init
from tts_app.frontend.views import frontend
from tts_app.voice_api.views import voice_api
from tts_app.auth.views import auth
from tts_app.admin.views import admin

from contants import config

app = Flask(__name__, template_folder=os.path.join(os.path.dirname(__file__), 'tts_app', 'templates'),
            static_folder=os.path.join(os.path.dirname(__file__), 'tts_app', 'static'))

app.config.from_pyfile("config.py")
# app.config.update(config)

phrases_dict_init()

csrf = CSRFProtect(app)
# 禁用tts api请求的CSRF防护
csrf.exempt(voice_api)

if config.system.is_admin_enabled:
    login_manager = LoginManager()
    login_manager.init_app(app)
    login_manager.login_view = 'auth.login'


    @login_manager.user_loader
    def load_user(user_id):
        admin = config.admin
        if admin.get_id() == user_id:
            return admin
        return None

# Initialize scheduler
scheduler = APScheduler()
scheduler.init_app(app)
if config.system.clean_interval_seconds > 0:
    scheduler.start()

app.register_blueprint(frontend, url_prefix='/')
app.register_blueprint(voice_api, url_prefix='/voice')
if config.system.is_admin_enabled:
    app.register_blueprint(auth, url_prefix=config.system.admin_route)
    app.register_blueprint(admin, url_prefix=config.system.admin_route)


def create_folders(paths):
    for path in paths:
        if not os.path.exists(path):
            os.makedirs(path, exist_ok=True)


create_folders([os.path.join(config.abs_path, config.system.upload_folder),
                os.path.join(config.abs_path, config.system.cache_path), ])


# regular cleaning
@scheduler.task('interval', id='clean_task', seconds=config.system.clean_interval_seconds,
                misfire_grace_time=900)
def clean_task():
    clean_folder(os.path.join(config.abs_path, config.system.upload_folder))
    clean_folder(os.path.join(config.abs_path, config.system.cache_path))


if __name__ == '__main__':
    app.run(host=config.http_service.host, port=config.http_service.port, debug=config.http_service.debug)