feat(api): add generic event endpoints in NodeService and ClusterService
NodeService: add POST /api/node/event to fire local events (params: event, payload).\nClusterService: add POST /api/cluster/event to broadcast events via cluster/broadcast (params: event, payload).\nApiServer: remove inline generic endpoints; services own their registrations.\nUnifies event dispatch through service layer and keeps core server lean.
This commit is contained in:
@@ -7,6 +7,29 @@ void ClusterService::registerEndpoints(ApiServer& api) {
|
|||||||
api.addEndpoint("/api/cluster/members", HTTP_GET,
|
api.addEndpoint("/api/cluster/members", HTTP_GET,
|
||||||
[this](AsyncWebServerRequest* request) { handleMembersRequest(request); },
|
[this](AsyncWebServerRequest* request) { handleMembersRequest(request); },
|
||||||
std::vector<ParamSpec>{});
|
std::vector<ParamSpec>{});
|
||||||
|
|
||||||
|
// 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>{
|
||||||
|
ParamSpec{String("event"), true, String("body"), String("string"), {}},
|
||||||
|
ParamSpec{String("payload"), true, String("body"), String("string"), {}}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClusterService::handleMembersRequest(AsyncWebServerRequest* request) {
|
void ClusterService::handleMembersRequest(AsyncWebServerRequest* request) {
|
||||||
|
|||||||
@@ -29,6 +29,24 @@ void NodeService::registerEndpoints(ApiServer& api) {
|
|||||||
api.addEndpoint("/api/node/endpoints", HTTP_GET,
|
api.addEndpoint("/api/node/endpoints", HTTP_GET,
|
||||||
[this](AsyncWebServerRequest* request) { handleEndpointsRequest(request); },
|
[this](AsyncWebServerRequest* request) { handleEndpointsRequest(request); },
|
||||||
std::vector<ParamSpec>{});
|
std::vector<ParamSpec>{});
|
||||||
|
|
||||||
|
// 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>{
|
||||||
|
ParamSpec{String("event"), true, String("body"), String("string"), {}},
|
||||||
|
ParamSpec{String("payload"), true, String("body"), String("string"), {}}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeService::handleStatusRequest(AsyncWebServerRequest* request) {
|
void NodeService::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user