Files
KCGL/inventory-backend/app/extensions.py

34 lines
973 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
# 1. 创建扩展实例(此时未绑定具体的 App
db = SQLAlchemy()
migrate = Migrate()
cors = CORS()
jwt = JWTManager() # 必须实例化
def beijing_time():
"""获取北京时间 (UTC+8)"""
return datetime.now(timezone(timedelta(hours=8)))
# 2. 定义初始化函数 (供工厂函数 create_app 调用)
def init_extensions(app):
"""
统一初始化所有 Flask 扩展
"""
# 初始化数据库
db.init_app(app)
# 初始化迁移工具
migrate.init_app(app, db)
# 初始化跨域设置 (允许 /api/* 路径被所有来源访问)
cors.init_app(app, resources={r"/api/*": {"origins": "*"}})
# 初始化 JWT (这一步至关重要,缺少它会导致 500 错误)
jwt.init_app(app)