58 lines
2.2 KiB
C++
58 lines
2.2 KiB
C++
#pragma once
|
|
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <AsyncWebSocket.h>
|
|
#include <Updater.h>
|
|
#include <functional>
|
|
#include <vector>
|
|
#include <tuple>
|
|
|
|
#include "spore/core/NodeContext.h"
|
|
#include "spore/types/NodeInfo.h"
|
|
#include "spore/core/TaskManager.h"
|
|
#include "spore/types/ApiTypes.h"
|
|
|
|
class Service; // Forward declaration
|
|
|
|
class ApiServer {
|
|
public:
|
|
ApiServer(NodeContext& ctx, TaskManager& taskMgr, uint16_t port = 80);
|
|
void begin();
|
|
void registerService(Service& service);
|
|
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler);
|
|
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
std::function<void(AsyncWebServerRequest*, const String&, size_t, uint8_t*, size_t, bool)> uploadHandler);
|
|
|
|
void registerEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
const std::vector<ParamSpec>& params);
|
|
void registerEndpoint(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);
|
|
|
|
// Static file serving
|
|
void serveStatic(const String& uri, fs::FS& fs, const String& path, const String& cache_header = "");
|
|
|
|
static const char* methodToStr(int method);
|
|
|
|
// Access to endpoints for endpoints endpoint
|
|
const std::vector<EndpointInfo>& getEndpoints() const { return endpoints; }
|
|
|
|
private:
|
|
AsyncWebServer server;
|
|
AsyncWebSocket ws{ "/ws" };
|
|
NodeContext& ctx;
|
|
TaskManager& taskManager;
|
|
std::vector<std::reference_wrapper<Service>> services;
|
|
std::vector<EndpointInfo> endpoints; // Single source of truth for endpoints
|
|
std::vector<AsyncWebSocketClient*> wsClients;
|
|
|
|
// Internal helpers
|
|
void registerEndpoint(const String& uri, int method,
|
|
const std::vector<ParamSpec>& params,
|
|
const String& serviceName);
|
|
|
|
// WebSocket helpers
|
|
void setupWebSocket();
|
|
};
|