Buckets:

download
raw
6.03 kB
"""
Modules Endpoints
Endpoints for module management and information.
"""
from fastapi import APIRouter, HTTPException, status
from ..models.modules import (
ModuleListResponse,
ModuleResponse,
SetEnabledRequest,
SetConfigRequest,
SuccessResponse,
)
from ..services.modules import module_service
from ..logging_config import get_logger
router = APIRouter()
logger = get_logger(__name__, component="modules")
@router.get(
"",
response_model=ModuleListResponse,
summary="Список модулей",
description="Получить список всех модулей системы с их статусами и метриками",
)
async def list_modules():
"""Получить список всех модулей"""
modules = module_service.list_modules()
return ModuleListResponse(modules=modules, total=len(modules))
@router.get(
"/{module_id}",
response_model=ModuleResponse,
summary="Информация о модуле",
description="Получить детальную информацию о конкретном модуле",
)
async def get_module(module_id: str):
"""Получить информацию о модуле"""
module = module_service.get_module(module_id)
if module is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Модуль '{module_id}' не найден",
)
return ModuleResponse(module=module)
@router.put(
"/{module_id}/enabled",
response_model=SuccessResponse,
summary="Включить/выключить модуль",
description="Включить или выключить функциональность модуля",
)
async def set_module_enabled(module_id: str, request: SetEnabledRequest):
"""Включить/выключить модуль"""
module = module_service.get_module(module_id)
if module is None:
logger.warning(f"Module not found: {module_id}")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Модуль '{module_id}' не найден",
)
if not request.enabled and not module.can_disable:
logger.warning(f"Attempt to disable core module: {module_id}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Модуль '{module_id}' нельзя отключить (core module)",
)
try:
module_service.set_enabled(module_id, request.enabled)
action = "включен" if request.enabled else "выключен"
logger.info(f"Module {action}: {module_id}", extra={"enabled": request.enabled})
return SuccessResponse(
success=True,
message=f"Модуль '{module.name}' {action}",
)
except Exception as e:
logger.error(f"Failed to set module enabled: {module_id}", extra={"error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
)
@router.get(
"/{module_id}/metrics",
summary="Метрики модуля",
description="Получить текущие метрики модуля",
)
async def get_module_metrics(module_id: str):
"""Получить метрики модуля"""
module = module_service.get_module(module_id)
if module is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Модуль '{module_id}' не найден",
)
return {"metrics": module.metrics}
@router.get(
"/{module_id}/config",
summary="Конфигурация модуля",
description="Получить текущую конфигурацию модуля",
)
async def get_module_config(module_id: str):
"""Получить конфигурацию модуля"""
module = module_service.get_module(module_id)
if module is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Модуль '{module_id}' не найден",
)
if not module.configurable:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Модуль '{module_id}' не поддерживает конфигурацию",
)
config = module_service.get_config(module_id)
return {"config": config or {}}
@router.put(
"/{module_id}/config",
response_model=SuccessResponse,
summary="Обновить конфигурацию",
description="Обновить конфигурацию модуля",
)
async def set_module_config(module_id: str, request: SetConfigRequest):
"""Обновить конфигурацию модуля"""
module = module_service.get_module(module_id)
if module is None:
logger.warning(f"Module not found: {module_id}")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Модуль '{module_id}' не найден",
)
if not module.configurable:
logger.warning(f"Attempt to configure non-configurable module: {module_id}")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail=f"Модуль '{module_id}' не поддерживает конфигурацию",
)
try:
module_service.set_config(module_id, request.config)
logger.info(f"Module config updated: {module_id}", extra={"config_keys": list(request.config.keys())})
return SuccessResponse(
success=True,
message=f"Конфигурация модуля '{module.name}' обновлена",
)
except Exception as e:
logger.error(f"Failed to update module config: {module_id}", extra={"error": str(e)})
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=str(e),
)

Xet Storage Details

Size:
6.03 kB
·
Xet hash:
ccfe41eb20f00b31e1938c6e1def801425e2c3dc67bc1a04a10e91de99527f65

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.