TT-002: modular router registration and module infrastructure
This commit is contained in:
@@ -2,9 +2,10 @@
|
||||
|
||||
import importlib.util
|
||||
import inspect
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
from app.core.sdk.module import BaseModule
|
||||
from app.core.module_registry import get_module_registry, ModuleAlreadyRegisteredError
|
||||
@@ -41,18 +42,43 @@ def _find_module_classes(module_path: Path) -> List[type]:
|
||||
return classes
|
||||
|
||||
|
||||
def _read_manifest(module_dir: Path) -> Optional[dict]:
|
||||
"""Read manifest.json from a module directory.
|
||||
|
||||
Args:
|
||||
module_dir: Path to the module directory.
|
||||
|
||||
Returns:
|
||||
Parsed manifest dict, or None if not found/invalid.
|
||||
"""
|
||||
manifest_path = module_dir / "manifest.json"
|
||||
if not manifest_path.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
with open(manifest_path, "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def discover_modules(modules_dir: Path) -> List[BaseModule]:
|
||||
"""Discover and instantiate modules from a directory.
|
||||
|
||||
Scans the given directory for subdirectories containing module.py files.
|
||||
Each module.py should contain a class that extends BaseModule.
|
||||
|
||||
If a manifest.json exists in the module directory, its metadata will
|
||||
be used to override the module's class attributes.
|
||||
|
||||
Directory structure:
|
||||
modules/
|
||||
├── request-module/
|
||||
│ └── module.py
|
||||
├── crm-module/
|
||||
│ └── module.py
|
||||
│ ├── module.py
|
||||
│ ├── manifest.json
|
||||
│ ├── static/
|
||||
│ ├── templates/
|
||||
│ └── migrations/
|
||||
└── ...
|
||||
|
||||
Args:
|
||||
@@ -74,10 +100,28 @@ def discover_modules(modules_dir: Path) -> List[BaseModule]:
|
||||
if not module_file.exists():
|
||||
continue
|
||||
|
||||
# Read manifest if available
|
||||
manifest = _read_manifest(subdir)
|
||||
|
||||
classes = _find_module_classes(module_file)
|
||||
for cls in classes:
|
||||
try:
|
||||
instance = cls()
|
||||
|
||||
# Apply manifest metadata
|
||||
if manifest:
|
||||
if "id" in manifest and manifest["id"]:
|
||||
instance.id = manifest["id"]
|
||||
if "name" in manifest and manifest["name"]:
|
||||
instance.name = manifest["name"]
|
||||
if "version" in manifest and manifest["version"]:
|
||||
instance.version = manifest["version"]
|
||||
if "description" in manifest and manifest["description"]:
|
||||
instance.description = manifest["description"]
|
||||
|
||||
# Set module directory for static/templates
|
||||
instance._module_dir = subdir
|
||||
|
||||
discovered.append(instance)
|
||||
except Exception:
|
||||
pass # Skip modules that fail to instantiate
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Base module class for platform modules."""
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from pathlib import Path
|
||||
from typing import List, Optional
|
||||
from fastapi import APIRouter
|
||||
|
||||
@@ -16,17 +17,22 @@ class BaseModule(ABC):
|
||||
name: Human-readable display name
|
||||
version: Semantic version string
|
||||
description: Brief description of the module
|
||||
enabled: Whether the module is enabled
|
||||
"""
|
||||
|
||||
id: str
|
||||
name: str
|
||||
version: str
|
||||
description: str = ""
|
||||
enabled: bool = True
|
||||
|
||||
def routes(self) -> List[APIRouter]:
|
||||
"""Return FastAPI routers to be mounted by the platform.
|
||||
def routers(self) -> List[APIRouter]:
|
||||
"""Return FastAPI routers to be auto-mounted by the platform.
|
||||
|
||||
Override this method to expose API endpoints through the platform.
|
||||
All routers returned here will be automatically included in the
|
||||
FastAPI application without manual registration in main.py.
|
||||
|
||||
Default implementation returns an empty list.
|
||||
|
||||
Returns:
|
||||
@@ -47,6 +53,32 @@ class BaseModule(ABC):
|
||||
"""
|
||||
return []
|
||||
|
||||
def static_path(self) -> Optional[Path]:
|
||||
"""Return path to the module's static files directory.
|
||||
|
||||
Override this method to serve static files (CSS, JS, images)
|
||||
through the platform. The platform will mount this directory
|
||||
at /modules/{module_id}/static
|
||||
|
||||
Default implementation returns None.
|
||||
|
||||
Returns:
|
||||
Path to static directory, or None if not applicable.
|
||||
"""
|
||||
return None
|
||||
|
||||
def templates_path(self) -> Optional[Path]:
|
||||
"""Return path to the module's templates directory.
|
||||
|
||||
Override this method to serve HTML templates through the platform.
|
||||
|
||||
Default implementation returns None.
|
||||
|
||||
Returns:
|
||||
Path to templates directory, or None if not applicable.
|
||||
"""
|
||||
return None
|
||||
|
||||
async def startup(self) -> None:
|
||||
"""Lifecycle hook called when the platform starts.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user