mdserver-web/web/admin/__init__.py

193 lines
5.1 KiB
Python
Raw Normal View History

2024-10-08 11:35:42 -04:00
# coding:utf-8
# ---------------------------------------------------------------------------------
# MW-Linux面板
# ---------------------------------------------------------------------------------
# copyright (c) 2018-∞(https://github.com/midoks/mdserver-web) All rights reserved.
# ---------------------------------------------------------------------------------
# Author: midoks <midoks@163.com>
# ---------------------------------------------------------------------------------
2024-10-24 14:24:38 -04:00
import os
2024-10-08 11:35:42 -04:00
import sys
2024-10-25 16:04:00 -04:00
import time
2024-10-24 14:24:38 -04:00
import uuid
import logging
from datetime import timedelta
2024-10-08 11:35:42 -04:00
from flask import Flask
from flask_socketio import SocketIO, emit, send
from flask import Flask, abort, request, current_app, session, url_for
2024-10-13 10:15:58 -04:00
from flask import Blueprint, render_template
2024-10-19 10:17:16 -04:00
from flask import render_template_string
2024-10-21 13:36:09 -04:00
from flask_migrate import Migrate
from werkzeug.local import LocalProxy
from admin.model import db as sys_db
2024-10-24 14:24:38 -04:00
from admin import setup
2024-10-20 15:19:08 -04:00
import core.mw as mw
2024-10-27 15:13:10 -04:00
import config
2024-10-26 14:23:12 -04:00
import utils.config as utils_config
2024-10-08 11:35:42 -04:00
2024-10-21 13:36:09 -04:00
root_dir = mw.getRunDir()
2024-10-08 11:35:42 -04:00
socketio = SocketIO(manage_session=False, async_mode='threading',
logger=False, engineio_logger=False, debug=False,
ping_interval=25, ping_timeout=120)
2024-10-13 10:15:58 -04:00
app = Flask(__name__, template_folder='templates/default')
2024-10-08 11:35:42 -04:00
2024-10-24 14:24:38 -04:00
# app.debug = True
2024-10-21 13:36:09 -04:00
# 静态文件配置
2024-10-19 09:27:24 -04:00
from whitenoise import WhiteNoise
app.wsgi_app = WhiteNoise(app.wsgi_app, root="../web/static/", prefix="static/", max_age=604800)
2024-10-08 11:35:42 -04:00
2024-10-24 14:24:38 -04:00
# session配置
app.secret_key = uuid.UUID(int=uuid.getnode()).hex[-12:]
# app.config['sessions'] = dict()
app.config['SESSION_PERMANENT'] = True
app.config['SESSION_USE_SIGNER'] = True
app.config['SESSION_KEY_PREFIX'] = 'MW_:'
app.config['SESSION_COOKIE_NAME'] = "MW_VER_1"
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=31)
2024-10-21 13:36:09 -04:00
# db的配置
2024-10-27 15:13:10 -04:00
app.config['SQLALCHEMY_DATABASE_URI'] = mw.getSqitePrefix()+config.SQLITE_PATH # 使用 SQLite 数据库
2024-10-21 13:36:09 -04:00
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
2024-10-20 15:19:08 -04:00
2024-10-24 14:24:38 -04:00
2024-10-21 13:36:09 -04:00
# 初始化db
sys_db.init_app(app)
Migrate(app, sys_db)
2024-10-20 15:19:08 -04:00
2024-10-24 14:24:38 -04:00
# 检查数据库是否存在。如果没有就创建它。
setup_db_required = False
2024-10-27 15:13:10 -04:00
if not os.path.isfile(config.SQLITE_PATH):
2024-10-24 14:24:38 -04:00
setup_db_required = True
# with app.app_context():
# sys_db.create_all()
2024-10-21 13:36:09 -04:00
with app.app_context():
2024-10-24 14:24:38 -04:00
if setup_db_required:
sys_db.create_all()
2024-10-20 15:19:08 -04:00
2024-10-24 14:24:38 -04:00
# 初始化用户信息
with app.app_context():
2024-10-25 07:04:14 -04:00
if setup_db_required:
setup.init_admin_user()
setup.init_option()
2024-10-20 15:19:08 -04:00
2024-10-13 10:15:58 -04:00
# 加载模块
from .submodules import get_submodules
for module in get_submodules():
app.logger.info('Registering blueprint module: %s' % module)
if app.blueprints.get(module.name) is None:
app.register_blueprint(module)
2024-10-08 11:35:42 -04:00
2024-10-24 14:24:38 -04:00
@app.before_request
def requestCheck():
2024-10-25 07:04:14 -04:00
# print("hh")
pass
2024-10-08 11:35:42 -04:00
2024-10-19 10:17:16 -04:00
@app.after_request
def requestAfter(response):
2024-10-27 15:13:10 -04:00
response.headers['soft'] = config.APP_NAME
response.headers['mw-version'] = config.APP_VERSION
2024-10-19 10:17:16 -04:00
return response
@app.errorhandler(404)
def page_unauthorized(error):
return render_template_string('404 not found', error_info=error), 404
2024-10-20 15:19:08 -04:00
# 设置模板全局变量
@app.context_processor
def inject_global_variables():
2024-10-27 15:13:10 -04:00
ver = config.APP_VERSION;
2024-10-25 16:04:00 -04:00
if mw.isDebugMode():
ver = ver + str(time.time())
2024-10-26 14:23:12 -04:00
data = utils_config.getGlobalVar()
2024-10-27 15:13:10 -04:00
g_config = {
2024-10-25 16:04:00 -04:00
'version': ver,
2024-10-25 07:04:14 -04:00
'title' : '面板',
'ip' : '127.0.0.1'
2024-10-20 15:19:08 -04:00
}
2024-10-27 15:13:10 -04:00
return dict(config=g_config, data=data)
2024-10-20 15:19:08 -04:00
# from flasgger import Swagger
2024-10-21 13:36:09 -04:00
# api = Api(app, version='1.0', title='API', description='API 文档')
2024-10-20 15:19:08 -04:00
# Swagger(app)
# @app.route('/colors/<palette>/')
# def colors(palette):
# """
# 根据调色板名称返回颜色列表
# ---
# parameters:
# - name: palette
# in: path
# type: string
# enum: ['all', 'rgb', 'cmyk']
# required: true
# default: all
# definitions:
# Palette:
# type: object
# properties:
# palette_name:
# type: array
# items:
# $ref: '#/definitions/Color'
# Color:
# type: string
# responses:
# 200:
# description: 返回的颜色列表,可按调色板过滤
# schema:
# $ref: '#/definitions/Palette'
# examples:
# rgb: ['red', 'green', 'blue']
# """
# all_colors = {
# 'cmyk': ['cyan', 'magenta', 'yellow', 'black'],
# 'rgb': ['red', 'green', 'blue']
# }
# if palette == 'all':
# result = all_colorselse
# result = {palette: all_colors.get(palette)}
# return jsonify(result)
2024-10-24 14:24:38 -04:00
2024-10-20 15:19:08 -04:00
# Log the startup
app.logger.info('########################################################')
2024-10-27 15:13:10 -04:00
app.logger.info('Starting %s v%s...', config.APP_NAME, config.APP_VERSION)
2024-10-20 15:19:08 -04:00
app.logger.info('########################################################')
app.logger.debug("Python syspath: %s", sys.path)
2024-10-21 13:36:09 -04:00
# OK
socketio.init_app(app, cors_allowed_origins="*")
2024-10-13 10:15:58 -04:00
# def create_app(app_name = None):
#
# if not app_name:
# app_name = config.APP_NAME
2024-10-08 11:35:42 -04:00
2024-10-13 10:15:58 -04:00
# # Check if app is created for CLI operations or Web
# cli_mode = False
# if app_name.endswith('-cli'):
# cli_mode = True
# return app