[Enhance][Bug fix] Waitress & API token

- Fix a bug that using API token in config file will not success
- Add deployment mode `waitress`
This commit is contained in:
Lost-MSth
2022-11-03 13:42:39 +08:00
parent 3fde61c639
commit 78d28f8886
4 changed files with 20 additions and 10 deletions

View File

@@ -27,12 +27,12 @@ def role_required(request, powers=[]):
return error_return(PostError('No token', api_error_code=-1), 401)
user = APIUser()
if Config.API_TOKEN == request.headers['Token'] and Config.API_TOKEN != '':
user.set_role_system()
else:
with Connect() as c:
with Connect() as c:
user.c = c
if Config.API_TOKEN == request.headers['Token'] and Config.API_TOKEN != '':
user.set_role_system()
else:
try:
user.c = c
user.select_user_id_from_api_token(
request.headers['Token'])
user.select_role_and_powers()

View File

@@ -6,7 +6,7 @@ class Config:
HOST = '0.0.0.0'
PORT = 80
USE_GEVENT_WSGI = False
DEPLOY_MODE = 'flask_multithread'
USE_PROXY_FIX = False
USE_CORS = False

View File

@@ -1,5 +1,6 @@
import sqlite3
import traceback
from atexit import register
from flask import current_app
@@ -367,5 +368,7 @@ class MemoryDatabase:
'''create index if not exists download_token_1 on download_token (song_id, file_name);''')
self.conn.commit()
def atexit(self):
self.conn.close()
@register
def atexit():
MemoryDatabase.conn.close()

View File

@@ -9,7 +9,7 @@ if os.path.exists('config.py') or os.path.exists('config'):
# 导入用户自定义配置
ConfigManager.load(import_module('config').Config)
if Config.USE_GEVENT_WSGI:
if Config.DEPLOY_MODE == 'gevent':
# 异步
from gevent import monkey
monkey.patch_all()
@@ -99,12 +99,19 @@ def download(file_path):
def tcp_server_run():
if Config.USE_GEVENT_WSGI:
if Config.DEPLOY_MODE == 'gevent':
# 异步 gevent WSGI server
host_port = (Config.HOST, Config.PORT)
app.logger.info('Running gevent WSGI server... (%s:%s)' % host_port)
from gevent.pywsgi import WSGIServer
WSGIServer(host_port, app, log=app.logger).serve_forever()
elif Config.DEPLOY_MODE == 'waitress':
# waitress WSGI server
from waitress import serve
import logging
logger = logging.getLogger('waitress')
logger.setLevel(logging.INFO)
serve(app, host=Config.HOST, port=Config.PORT)
else:
if Config.SSL_CERT and Config.SSL_KEY:
app.run(Config.HOST, Config.PORT, ssl_context=(