PagerMaid-Modify/pagermaid/interface/__init__.py
Xtao_dada d022616c99
Remove unnecessary requirement (#117)
 移除不必要的依赖项
2021-08-18 16:41:22 +08:00

57 lines
1.7 KiB
Python

""" PagerMaid web interface utility. """
from threading import Thread
from distutils.util import strtobool
from importlib import import_module
from cheroot.wsgi import Server as WSGIServer, PathInfoDispatcher
from flask import Flask
from flask.logging import default_handler
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_bcrypt import Bcrypt
try:
from pagermaid import config, working_dir, logs, logging_handler
except ModuleNotFoundError:
print("出错了呜呜呜 ~ 此模块不应直接运行。")
exit(1)
app = Flask("pagermaid")
app.config['CSRF_ENABLED'] = True
app.config['SECRET_KEY'] = config['web_interface']['secret_key']
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{working_dir}/data/web_interface.db"
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login = LoginManager()
login.init_app(app)
@app.before_first_request
def init_db():
db.create_all()
import_module('pagermaid.interface.views')
import_module('pagermaid.interface.modals')
dispatcher = PathInfoDispatcher({'/': app})
server = WSGIServer((config['web_interface']['host'], int(config['web_interface']['port'])), dispatcher)
def start():
if strtobool(config['web_interface']['enable']):
logs.info(f"已经启动Web界面 {config['web_interface']['host']}:{config['web_interface']['port']}")
app.logger.removeHandler(default_handler)
app.logger.addHandler(logging_handler)
try:
server.start()
except OSError:
logs.fatal("出错了呜呜呜 ~ 另一个进程绑定到了 PagerMaid 需要的端口!")
return
else:
logs.info("Web 界面已禁用。")
Thread(target=start).start()