ESP-LiveControl  1.99.1
HTTP server, AJAX API backend and Vue.js web application implementing self-contained, zero-install WiFi remote control of hardware modules attached to the Espressif ESP32 SoC
api_server.hpp
Go to the documentation of this file.
1 
6 #ifndef API_SERVER_HPP__
7 #define API_SERVER_HPP__
8 
9 #include <map>
10 #include <functional>
11 
12 //#include <Arduino.h>
13 #include <Ticker.h>
14 #include <AsyncTCP.h>
15 #include <ESPAsyncWebServer.h>
16 
17 #include "app_config.hpp"
18 
19 // Callback function with string argument
20 using CbStringT = std::function<void(const String&)>;
21 // Callback function with float argument
22 using CbFloatT = std::function<void(const float)>;
23 // Callback function with integer argument
24 using CbIntT = std::function<void(const int)>;
25 // Callback function without arguments
26 using CbVoidT = std::function<void(void)>;
27 
28 // Mapping used for resolving command strings received via HTTP request
29 // on the "/cmd" endpoint to specialised request handlers
30 using CmdMapT = std::map<String, CbStringT>;
31 // String replacement mapping for template processor
32 using TemplateMapT = std::map<String, String>;
33 
34 
44 class APIServer
45 {
46 public:
47  // API server configuration
48  static constexpr APIServerConfig srv_conf{};
49  // Base ESPAsyncWebServer
50  AsyncWebServer* backend;
51  // Server-Sent Events (SSE) for "PUSH" updates of application data
52  AsyncEventSource* event_source;
53  // Callback registry, see above
54  CmdMapT cmd_map;
55  // String replacement mapping for template processor
56  TemplateMapT template_map;
57  // Can be polled externally as an alternative to conf_enable_reboot=true
58  bool reboot_requested;
59 
60 
61  APIServer(AsyncWebServer* http_backend);
62  ~APIServer();
63 
66  void set_template(const char* placeholder, const char* replacement);
67 
73  void register_api_cb(const char* cmd_name, CbStringT cmd_callback);
79  void register_api_cb(const char* cmd_name, CbFloatT cmd_callback);
85  void register_api_cb(const char* cmd_name, CbIntT cmd_callback);
91  void register_api_cb(const char* cmd_name, CbVoidT cmd_callback);
92 
97  void begin();
98 
99 
100 private:
102  // Add Request URL rewrites to the server instance
103  void _add_rewrites();
104  // Add URL redirects to the server instance
105  void _add_redirects();
106  // Add request handlers to the server instance
107  void _add_handlers();
108  // Activate the SSE envent source if conf_use_sse == true
109  void _add_event_source();
110  // Helper function for _add_event_source, only sends "Hello" message and info print
111  void _register_sse_on_connect_callback();
112 
114 
115  // on("/")
116  void _on_root_request(AsyncWebServerRequest *request);
117  // on("/cmd")
118  void _on_cmd_request(AsyncWebServerRequest *request);
119  // on("/update")
120  // When update is initiated via GET
121  void _on_update_request(AsyncWebServerRequest *request);
122  // When file is uploaded via POST request
123  static void _on_update_body_upload(
124  AsyncWebServerRequest *request, const String& filename,
125  size_t index, uint8_t *data, size_t len, bool final);
126  // Catch-All-Handlers
127  static void _on_request(AsyncWebServerRequest *request);
128  static void _on_body(AsyncWebServerRequest *request,
129  uint8_t *data, size_t len, size_t index, size_t total);
130  static void _on_upload(AsyncWebServerRequest *request, const String& filename,
131  size_t index, uint8_t *data, size_t len, bool final);
132 
134  // Template processor
135  String _template_processor(const String& placeholder);
136 };
137 
138 #endif /* API_SERVER_HPP__ */
Application, network and hardware settings, constants and preset configuration.
AJAX HTTP API server for ESP-AJAX-Lab.
Definition: api_server.hpp:45
void set_template(const char *placeholder, const char *replacement)
Definition: api_server.cpp:57
void register_api_cb(const char *cmd_name, CbStringT cmd_callback)
Definition: api_server.cpp:66
void begin()
Definition: api_server.cpp:35
Definition: app_config.hpp:79