From 668576b8666a6ebba8b87855efaa16442ebc6f9d Mon Sep 17 00:00:00 2001 From: mac Date: Tue, 2 Jun 2026 23:40:14 +0800 Subject: [PATCH] =?UTF-8?q?v1.0.3=20=E2=80=94=20=E4=BF=AE=E5=A4=8D=20gunic?= =?UTF-8?q?orn=20=E5=A4=9A=20worker=20session=20=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - secret_key 从 os.urandom() 改为固定值/环境变量 - deploy.sh 首次部署自动生成 .env 并持久化 - 解决多 worker 下 session 解密失败导致保存 302 --- app.py | 3 ++- deploy.sh | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 62bf799..1473a6c 100644 --- a/app.py +++ b/app.py @@ -9,7 +9,8 @@ from werkzeug.security import generate_password_hash, check_password_hash from database import init_db, get_checkin, save_checkin, delete_checkin, get_all_checkins app = Flask(__name__) -app.secret_key = os.urandom(24) +# 固定密钥确保 gunicorn 多 worker 下 session 可互通 +app.secret_key = os.environ.get('SECRET_KEY', 'ziwei-power-secret-2026') # 会话持久化:30 天 app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=30) diff --git a/deploy.sh b/deploy.sh index 4794173..8c658ae 100644 --- a/deploy.sh +++ b/deploy.sh @@ -1,6 +1,9 @@ #!/bin/bash # ziwei-power 一键部署脚本 # 用法: bash deploy.sh [port] +# +# 首次运行: 自动生成 .env (SECRET_KEY),之后不变 +# 多 worker 安全: 所有 worker 共享同一密钥 set -e @@ -30,7 +33,15 @@ fi echo "安装依赖..." venv/bin/pip install -q -r requirements.txt -# 3. 自动建表(首次运行) +# 3. 生成/读取固定 SECRET_KEY(多 worker 共享) +if [ ! -f ".env" ]; then + echo "生成 SECRET_KEY..." + python3 -c "import os; print('SECRET_KEY=' + os.urandom(24).hex())" > .env +fi +set -a; source .env; set +a +export SECRET_KEY + +# 4. 自动建表(首次运行) echo "初始化数据库..." venv/bin/python -c " from database import init_db