50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
import os
|
|
import importlib.util
|
|
from flask import Flask
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
from flask_migrate import Migrate
|
|
from flask_login import LoginManager
|
|
|
|
db = SQLAlchemy()
|
|
migrate = Migrate()
|
|
login_manager = LoginManager()
|
|
|
|
|
|
def create_app():
|
|
app = Flask(__name__)
|
|
app.config.from_object('app.config.Config')
|
|
|
|
# Initialize core extensions
|
|
db.init_app(app)
|
|
migrate.init_app(app, db)
|
|
login_manager.init_app(app)
|
|
login_manager.login_view = 'auth.login' # Redirect for @login_required
|
|
|
|
# Register error handlers
|
|
from .errors import bp as errors_bp
|
|
app.register_blueprint(errors_bp)
|
|
|
|
# Plugin auto-loader
|
|
plugin_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'plugins'))
|
|
for plugin in os.listdir(plugin_path):
|
|
full_path = os.path.join(plugin_path, plugin, 'routes.py')
|
|
if os.path.isfile(full_path):
|
|
spec = importlib.util.spec_from_file_location(f"plugins.{plugin}.routes", full_path)
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
if hasattr(mod, 'bp'):
|
|
app.register_blueprint(mod.bp)
|
|
|
|
# Register CLI commands if the plugin has any
|
|
if hasattr(mod, 'cli_commands'):
|
|
for command in mod.cli_commands:
|
|
app.cli.add_command(command)
|
|
|
|
return app
|
|
|
|
|
|
@login_manager.user_loader
|
|
def load_user(user_id):
|
|
from plugins.auth.models import User
|
|
return User.query.get(int(user_id))
|