"""
Flask 扩展初始化
"""
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
from flask_login import LoginManager
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
import redis

db = SQLAlchemy()
migrate = Migrate()
jwt = JWTManager()
login_manager = LoginManager()
cors = CORS()
limiter = Limiter(
    key_func=get_remote_address,
    default_limits=["100 per minute"]
)

# Redis 客户端
redis_client = None


def init_redis(app):
    """初始化 Redis 客户端"""
    global redis_client
    redis_url = app.config.get('REDIS_URL', 'redis://localhost:6379/0')
    redis_client = redis.from_url(redis_url)
    return redis_client
