31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
import os
|
|
import importlib.util
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def load_routes(app):
|
|
"""
|
|
Automatically discover and load route modules from api/routes/.
|
|
|
|
Each module should have a register(app) function.
|
|
"""
|
|
routes_dir = os.path.dirname(__file__)
|
|
for filename in os.listdir(routes_dir):
|
|
if filename.endswith('.py') and filename != '__init__.py':
|
|
module_name = filename[:-3] # remove .py
|
|
module_path = os.path.join(routes_dir, filename)
|
|
|
|
try:
|
|
spec = importlib.util.spec_from_file_location(module_name, module_path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
|
|
if hasattr(module, 'register'):
|
|
module.register(app)
|
|
logger.info(f"Loaded route module: {module_name}")
|
|
else:
|
|
logger.warning(f"Route module {module_name} has no register() function")
|
|
except Exception as e:
|
|
logger.error(f"Failed to load route module {module_name}: {e}")
|