49 lines
1.9 KiB
C++
49 lines
1.9 KiB
C++
#pragma once
|
|
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <ESPAsyncWebServer.h>
|
|
#include <Updater.h>
|
|
#include <functional>
|
|
#include <vector>
|
|
#include <tuple>
|
|
|
|
#include "NodeContext.h"
|
|
#include "NodeInfo.h"
|
|
#include "TaskManager.h"
|
|
#include "ApiTypes.h"
|
|
|
|
class Service; // Forward declaration
|
|
|
|
class ApiServer {
|
|
public:
|
|
ApiServer(NodeContext& ctx, TaskManager& taskMgr, uint16_t port = 80);
|
|
void begin();
|
|
void addService(Service& service);
|
|
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler);
|
|
void 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);
|
|
|
|
void addEndpoint(const String& uri, int method, std::function<void(AsyncWebServerRequest*)> requestHandler,
|
|
const std::vector<ParamSpec>& params);
|
|
void 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);
|
|
|
|
static const char* methodToStr(int method);
|
|
|
|
// Access to endpoints for endpoints endpoint
|
|
const std::vector<EndpointInfo>& getEndpoints() const { return endpoints; }
|
|
|
|
private:
|
|
AsyncWebServer server;
|
|
NodeContext& ctx;
|
|
TaskManager& taskManager;
|
|
std::vector<std::reference_wrapper<Service>> services;
|
|
std::vector<EndpointInfo> endpoints; // Single source of truth for endpoints
|
|
|
|
// Internal helpers
|
|
void registerEndpoint(const String& uri, int method,
|
|
const std::vector<ParamSpec>& params,
|
|
const String& serviceName);
|
|
};
|