68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
import asyncio
|
|
import json
|
|
|
|
from modules.storages.storage import Storage
|
|
from modules.transports.server import Server
|
|
from services.control.context import *
|
|
from services.control.create_context import *
|
|
from services.control.response import *
|
|
|
|
def Handle(context, request):
|
|
"""
|
|
Обработка запроса
|
|
:param context: Контекст сервиса
|
|
:param request: Запрос
|
|
:return: Ответ
|
|
"""
|
|
|
|
# Парсим запрос
|
|
try:
|
|
request = json.loads(request)
|
|
except Exception as e:
|
|
return ErrorResponse("invalid request", "failed to parse request")
|
|
|
|
# Проверяем наличие нужных ключей
|
|
for key in ["handler", "parameters"]:
|
|
if key not in request:
|
|
return ErrorResponse("invalid request", f"key '{key}' is required")
|
|
|
|
# Проверяем наличие обработчика с указанным именем
|
|
handler_name = request["handler"]
|
|
if not context.handler_manager.contains(handler_name):
|
|
return ErrorResponse("not found", f"there is no handler with name {handler_name}")
|
|
|
|
# Достаём нужного обработчика
|
|
handler = context.handler_manager.get(handler_name)
|
|
|
|
# Запускаем обработку
|
|
try:
|
|
result = handler.do(context, request["parameters"])
|
|
return SuccessResponse(result)
|
|
except Exception as e:
|
|
return ErrorResponse("handling failed", str(e))
|
|
|
|
def main():
|
|
server = Server("localhost", 8081, Handle)
|
|
|
|
# Создаём контекст
|
|
context = CreateContext()
|
|
|
|
# Подключаемся к хранилищу
|
|
context.storage = Storage("localhost", "5432", "log_analysis", "postgres", "postgres")
|
|
|
|
# Заполняем пайплайны
|
|
query = "select name, pipes from pipelines;"
|
|
pipelines = context.storage.do(query, should_return=True)
|
|
|
|
for name, pipes in pipelines:
|
|
context.pipeline_manager.create(name, json.loads(pipes))
|
|
|
|
# Указываем контекст в сервере
|
|
server.context = context
|
|
|
|
# Запускаем сервер
|
|
asyncio.run(server.run())
|
|
|
|
if __name__ == "__main__":
|
|
main()
|