diff --git a/src/spore/services/ClusterService.cpp b/src/spore/services/ClusterService.cpp index 12c7f18..d9dd43d 100644 --- a/src/spore/services/ClusterService.cpp +++ b/src/spore/services/ClusterService.cpp @@ -7,6 +7,29 @@ void ClusterService::registerEndpoints(ApiServer& api) { api.addEndpoint("/api/cluster/members", HTTP_GET, [this](AsyncWebServerRequest* request) { handleMembersRequest(request); }, std::vector{}); + + // Generic cluster broadcast endpoint + api.addEndpoint("/api/cluster/event", HTTP_POST, + [this](AsyncWebServerRequest* request) { + if (!request->hasParam("event", true) || !request->hasParam("payload", true)) { + request->send(400, "application/json", "{\"error\":\"Missing 'event' or 'payload'\"}"); + return; + } + String eventName = request->getParam("event", true)->value(); + String payloadStr = request->getParam("payload", true)->value(); + JsonDocument envelope; + envelope["event"] = eventName; + envelope["data"] = payloadStr; // pass payload as JSON string + String eventJson; + serializeJson(envelope, eventJson); + std::string ev = "cluster/broadcast"; + ctx.fire(ev, &eventJson); + request->send(200, "application/json", "{\"ok\":true}"); + }, + std::vector{ + ParamSpec{String("event"), true, String("body"), String("string"), {}}, + ParamSpec{String("payload"), true, String("body"), String("string"), {}} + }); } void ClusterService::handleMembersRequest(AsyncWebServerRequest* request) { diff --git a/src/spore/services/NodeService.cpp b/src/spore/services/NodeService.cpp index 9da9015..ffbebc4 100644 --- a/src/spore/services/NodeService.cpp +++ b/src/spore/services/NodeService.cpp @@ -29,6 +29,24 @@ void NodeService::registerEndpoints(ApiServer& api) { api.addEndpoint("/api/node/endpoints", HTTP_GET, [this](AsyncWebServerRequest* request) { handleEndpointsRequest(request); }, std::vector{}); + + // Generic local event endpoint + api.addEndpoint("/api/node/event", HTTP_POST, + [this](AsyncWebServerRequest* request) { + if (!request->hasParam("event", true) || !request->hasParam("payload", true)) { + request->send(400, "application/json", "{\"error\":\"Missing 'event' or 'payload'\"}"); + return; + } + String eventName = request->getParam("event", true)->value(); + String payloadStr = request->getParam("payload", true)->value(); + std::string ev = eventName.c_str(); + ctx.fire(ev, &payloadStr); + request->send(200, "application/json", "{\"ok\":true}"); + }, + std::vector{ + ParamSpec{String("event"), true, String("body"), String("string"), {}}, + ParamSpec{String("payload"), true, String("body"), String("string"), {}} + }); } void NodeService::handleStatusRequest(AsyncWebServerRequest* request) {