支持从.env同步管理员密码;新增reset_admin.py
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,7 +17,7 @@ from werkzeug.security import check_password_hash, generate_password_hash
|
||||
from symbols import search_symbols, ths_to_codes
|
||||
from market import get_price as market_get_price, set_ths_refresh_token, get_quote_source_label
|
||||
|
||||
load_dotenv()
|
||||
load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env"))
|
||||
|
||||
app = Flask(__name__)
|
||||
app.secret_key = os.getenv("SECRET_KEY", "futures_monitor_default_secret")
|
||||
@@ -96,11 +96,7 @@ def init_db():
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
if not get_setting("admin_username"):
|
||||
username = os.getenv("ADMIN_USERNAME", "admin")
|
||||
password = os.getenv("ADMIN_PASSWORD", "admin123")
|
||||
set_setting("admin_username", username)
|
||||
set_setting("admin_password_hash", generate_password_hash(password))
|
||||
sync_admin_from_env()
|
||||
|
||||
if not get_setting("wechat_webhook") and os.getenv("WECHAT_WEBHOOK"):
|
||||
set_setting("wechat_webhook", os.getenv("WECHAT_WEBHOOK"))
|
||||
@@ -109,6 +105,33 @@ def init_db():
|
||||
set_setting("ths_refresh_token", os.getenv("THS_REFRESH_TOKEN"))
|
||||
|
||||
|
||||
def sync_admin_from_env():
|
||||
"""
|
||||
从 .env 同步管理员账号。
|
||||
- 首次建库:自动写入 ADMIN_USERNAME / ADMIN_PASSWORD
|
||||
- 已建库后改 .env:需设 ADMIN_SYNC_FROM_ENV=true 并重启服务
|
||||
"""
|
||||
sync = os.getenv("ADMIN_SYNC_FROM_ENV", "false").lower() in ("1", "true", "yes")
|
||||
env_username = os.getenv("ADMIN_USERNAME", "").strip()
|
||||
env_password = os.getenv("ADMIN_PASSWORD", "").strip()
|
||||
placeholder_passwords = {"", "change-me-on-first-login", "admin123"}
|
||||
|
||||
if not get_setting("admin_username"):
|
||||
username = env_username or "admin"
|
||||
password = env_password if env_password not in placeholder_passwords else "admin123"
|
||||
set_setting("admin_username", username)
|
||||
set_setting("admin_password_hash", generate_password_hash(password))
|
||||
return
|
||||
|
||||
if not sync:
|
||||
return
|
||||
|
||||
if env_username:
|
||||
set_setting("admin_username", env_username)
|
||||
if env_password and env_password not in placeholder_passwords:
|
||||
set_setting("admin_password_hash", generate_password_hash(env_password))
|
||||
|
||||
|
||||
init_db()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user