49 lines
1.5 KiB
Python
49 lines
1.5 KiB
Python
from flask_sqlalchemy import SQLAlchemy
|
||
from flask_migrate import Migrate
|
||
from flask_cors import CORS
|
||
from flask_jwt_extended import JWTManager # 确保引入了 JWTManager
|
||
from datetime import datetime, timezone, timedelta
|
||
import redis
|
||
|
||
# 1. 创建扩展实例(此时未绑定具体的 App)
|
||
db = SQLAlchemy()
|
||
migrate = Migrate()
|
||
cors = CORS()
|
||
jwt = JWTManager() # 必须实例化
|
||
|
||
# Redis 客户端 (单设备登录互踢用)
|
||
redis_client = None
|
||
|
||
|
||
def beijing_time():
|
||
"""获取北京时间 (UTC+8)"""
|
||
return datetime.now(timezone(timedelta(hours=8)))
|
||
|
||
|
||
# 2. 定义初始化函数 (供工厂函数 create_app 调用)
|
||
def init_extensions(app):
|
||
"""
|
||
统一初始化所有 Flask 扩展
|
||
"""
|
||
global redis_client
|
||
|
||
# 初始化数据库
|
||
db.init_app(app)
|
||
|
||
# 初始化迁移工具
|
||
migrate.init_app(app, db)
|
||
|
||
# 初始化跨域设置 (允许 /api/* 路径被所有来源访问)
|
||
cors.init_app(app, resources={r"/api/*": {"origins": "*"}})
|
||
|
||
# 初始化 JWT (这一步至关重要,缺少它会导致 500 错误)
|
||
jwt.init_app(app)
|
||
|
||
# 初始化 Redis (单设备登录互踢)
|
||
redis_url = app.config.get('REDIS_URL', 'redis://localhost:6379/0')
|
||
try:
|
||
redis_client = redis.from_url(redis_url, decode_responses=True)
|
||
redis_client.ping()
|
||
app.logger.info("✅ Redis connected successfully")
|
||
except Exception as e:
|
||
app.logger.warning(f"⚠️ Redis connection failed: {e}, single-device login will be disabled") |