first commit
This commit is contained in:
13
api/main.py
13
api/main.py
@@ -131,7 +131,14 @@ def health_check():
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for module in ROUTE_MODULES:
|
||||
if hasattr(module, "register"):
|
||||
module.register(app)
|
||||
from api.routes import load_routes
|
||||
load_routes(app)
|
||||
|
||||
# Optional: load general plugins
|
||||
try:
|
||||
from plugins import load_plugins
|
||||
load_plugins(app)
|
||||
except ImportError:
|
||||
pass # plugins folder not present
|
||||
|
||||
app.run(host="0.0.0.0", port=5000)
|
||||
|
||||
30
api/routes/__init__.py
Normal file
30
api/routes/__init__.py
Normal file
@@ -0,0 +1,30 @@
|
||||
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}")
|
||||
Reference in New Issue
Block a user