- Restructure include/ and src/ directories with logical grouping - Move core components to spore/core/ (NodeContext, NetworkManager, TaskManager, ClusterManager, ApiServer) - Move services to spore/services/ (NodeService, NetworkService, ClusterService, TaskService) - Move types to spore/types/ (NodeInfo, ApiTypes, Config) - Move internal components to spore/internal/ (Globals) - Update all #include statements to use new namespace paths - Update platformio.ini build filters for all environments - Update all example files to use new include paths - Maintain backward compatibility for public API - Improve code organization, maintainability, and scalability This reorganization follows modern C++ project structure patterns and provides clear separation between public API, internal implementation, and utilities. All examples compile successfully with the new structure.
92 lines
3.6 KiB
C++
92 lines
3.6 KiB
C++
#include "spore/core/ApiServer.h"
|
|
#include "spore/Service.h"
|
|
#include <algorithm>
|
|
|
|
const char* ApiServer::methodToStr(int method) {
|
|
switch (method) {
|
|
case HTTP_GET: return "GET";
|
|
case HTTP_POST: return "POST";
|
|
case HTTP_PUT: return "PUT";
|
|
case HTTP_DELETE: return "DELETE";
|
|
case HTTP_PATCH: return "PATCH";
|
|
default: return "UNKNOWN";
|
|
}
|
|
}
|
|
|
|
ApiServer::ApiServer(NodeContext& ctx, TaskManager& taskMgr, uint16_t port) : server(port), ctx(ctx), taskManager(taskMgr) {}
|
|
|
|
void ApiServer::registerEndpoint(const String& uri, int method,
|
|
const std::vector<ParamSpec>& params,
|
|
const String& serviceName) {
|
|
// Add to local endpoints
|
|
endpoints.push_back(EndpointInfo{uri, method, params, serviceName, true});
|
|
|
|
// Update cluster if needed
|
|
if (ctx.memberList && !ctx.memberList->empty()) {
|
|
auto it = ctx.memberList->find(ctx.hostname);
|
|
if (it != ctx.memberList->end()) {
|
|
it->second.endpoints.push_back(EndpointInfo{uri, method, params, serviceName, true});
|
|
}
|
|
}
|
|
}
|
|
|
|
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler) {
|
|
// Get current service name if available
|
|
String serviceName = "unknown";
|
|
if (!services.empty()) {
|
|
serviceName = services.back().get().getName();
|
|
}
|
|
registerEndpoint(uri, method, {}, serviceName);
|
|
server.on(uri.c_str(), method, requestHandler);
|
|
}
|
|
|
|
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler) {
|
|
// Get current service name if available
|
|
String serviceName = "unknown";
|
|
if (!services.empty()) {
|
|
serviceName = services.back().get().getName();
|
|
}
|
|
registerEndpoint(uri, method, {}, serviceName);
|
|
server.on(uri.c_str(), method, requestHandler, uploadHandler);
|
|
}
|
|
|
|
// Overloads that also record minimal capability specs
|
|
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
const std::vector<ParamSpec>& params) {
|
|
// Get current service name if available
|
|
String serviceName = "unknown";
|
|
if (!services.empty()) {
|
|
serviceName = services.back().get().getName();
|
|
}
|
|
registerEndpoint(uri, method, params, serviceName);
|
|
server.on(uri.c_str(), method, requestHandler);
|
|
}
|
|
|
|
void ApiServer::addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler,
|
|
const std::vector<ParamSpec>& params) {
|
|
// Get current service name if available
|
|
String serviceName = "unknown";
|
|
if (!services.empty()) {
|
|
serviceName = services.back().get().getName();
|
|
}
|
|
registerEndpoint(uri, method, params, serviceName);
|
|
server.on(uri.c_str(), method, requestHandler, uploadHandler);
|
|
}
|
|
|
|
void ApiServer::addService(Service& service) {
|
|
services.push_back(service);
|
|
Serial.printf("[API] Added service: %s\n", service.getName());
|
|
}
|
|
|
|
void ApiServer::begin() {
|
|
// Register all service endpoints
|
|
for (auto& service : services) {
|
|
service.get().registerEndpoints(*this);
|
|
Serial.printf("[API] Registered endpoints for service: %s\n", service.get().getName());
|
|
}
|
|
|
|
server.begin();
|
|
}
|