fix: activate plugins on startup + register routes + seed admin script

- lifespan: load active plugins from DB, call on_activate, register routes
- Fixes: Calendar/Mail/DMS/Tags/Permissions routes returning 404
- scripts/seed_admin.py: creates default tenant + admin user
This commit is contained in:
leocrm-bot
2026-07-02 13:02:38 +02:00
parent f477efc366
commit 5dc4d6d4c0
2 changed files with 117 additions and 0 deletions
+36
View File
@@ -11,8 +11,12 @@ from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from starlette.middleware.base import BaseHTTPMiddleware
import importlib
import logging
import os
logger = logging.getLogger(__name__)
from app.config import get_settings
from app.core.db import close_engine, get_engine
from app.core.middleware import CSRFMiddleware
@@ -91,6 +95,38 @@ async def lifespan(app: FastAPI):
registry.initialize(get_engine(), app)
registry.discover_builtins()
# Activate plugins that are marked active in the database
from sqlalchemy import select as sa_select
from sqlalchemy.ext.asyncio import async_sessionmaker
from app.models.plugin import Plugin as PluginModel
from app.core.event_bus import get_event_bus
event_bus = get_event_bus()
async_session = async_sessionmaker(get_engine(), expire_on_commit=False)
async with async_session() as db:
result = await db.execute(
sa_select(PluginModel).where(PluginModel.active == True) # noqa: E712
)
active_plugins = result.scalars().all()
for plugin_record in active_plugins:
plugin = registry.get_plugin(plugin_record.name)
if plugin is None:
continue
try:
await plugin.on_activate(db, container, event_bus)
# Register plugin routes into FastAPI app
for route_def in plugin.manifest.routes:
router_module = importlib.import_module(route_def.module)
router = getattr(router_module, route_def.router_attr)
app.include_router(router)
logger.info(f"Activated plugin: {plugin_record.name}")
except Exception as exc:
logger.warning(f"Failed to activate plugin {plugin_record.name}: {exc}")
await db.commit()
yield
await close_engine()