You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

65 lines
2.5 KiB

from quart import Quart, render_template, request, jsonify,send_from_directory,session,redirect, url_for
from quart_session import Session
from pymemcache.client import base
from .main import main
from .API import api
from functools import wraps
from myutils.ConfigManager import myCongif
from quart_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
#app.config['SECRET_KEY'] = 'mysecret' #密钥 --需要放配置文件
#socketio = SocketIO(app)
# Create the custom backend for quart-session
class MemcachedSessionInterface: #只是能用,不明所以
def __init__(self, client):
self.client = client
async def open_session(self, app, request):
sid = request.cookies.get(app.session_cookie_name)
if not sid:
sid = self._generate_sid()
val = await self.client.get(self.key_prefix + sid)
if val is not None:
return self._deserialize(val)
return self._get_default_session()
async def save_session(self, app, session, response):
val = self._serialize(session)
await self.client.set(self.key_prefix + session.sid, val, self.expire)
def create_app():
app = Quart(__name__)
#相关配置--设置各种配置选项,这些选项会在整个应用程序中被访问和使用。
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db'
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'zfxxkj_2024_!@#'
if myCongif.get_data("model_platform") == "acl":
app.config['SESSION_TYPE'] = 'memcached' # session类型
elif myCongif.get_data("model_platform") =="cpu":
app.config['SESSION_TYPE'] = 'redis' # session类型
#app.config['SESSION_FILE_DIR'] = './sessions' # session保存路径
#app.config['SESSION_MEMCACHED'] = base.Client(('localhost', 11211))
app.config['SESSION_PERMANENT'] = True # 如果设置为True,则关闭浏览器session就失效。
app.config['SESSION_USE_SIGNER'] = False # 是否对发送到浏览器上session的cookie值进行加密
memcached_client = base.Client(('localhost', 11211))
app.session_interface = MemcachedSessionInterface(memcached_client)
Session(app)
# 注册main
app.register_blueprint(main)
#注册API模块
app.register_blueprint(api,url_prefix = '/api')
return app
def login_required(f):
@wraps(f)
async def decorated_function(*args, **kwargs):
if 'user' not in session:
return redirect(url_for('main.login'))
return await f(*args, **kwargs)
return decorated_function