mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-10 10:17:05 +01:00
api: rework plain and JSON implementations (#2405)
- match paths through a custom AsyncWebHandler instead of using generic not-found fallback handler - allow MQTT-like patterns when registering paths (`simple/path`, `path/+/something`, `path/#`) Replaces `relay/0`, `relay/1` etc. with `relay/+`. Magnitudes are plain paths, but using `/+` in case there's more than 1 magnitude of the same type. - restore `std::function` as callback container (no more single-byte arg nonsense). Still, limit to 1 type per handler type - adds JSON handlers which will receive JsonObject root as both input and output. Same logic as plain - GET returns resource data, PUT updates it. - breaking change to `apiAuthenticate(request)`, it no longer will do `request->send(403)` and expect this to be handled externally. - allow `Api-Key` header containing the key, works for both GET & PUT plain requests. The only way to set apikey for JSON. - add `ApiRequest::param` to retrieve both GET and PUT params (aka args), remove ApiBuffer - remove `API_BUFFER_SIZE`. Allow custom form-data key=value pairs for requests, allow to send basic `String`. - add `API_JSON_BUFFER_SIZE` for the JSON buffer (both input and output) - `/apis` replaced with `/api/list`, no longer uses custom handler and is an `apiRegister` callback - `/api/rpc` custom handler replaced with an `apiRegister` callback WIP further down: - no more `webLog` for API requests, unless `webAccessLog` / `WEB_ACCESS_LOG` is set to `1`. This also needs to happen to the other handlers. - migrate to ArduinoJson v6, since it become apparent it is actually a good upgrade :) - actually make use of JSON endpoints more, right now it's just existing GET for sensors and relays - fork ESPAsyncWebServer to cleanup path parsing and temporary objects attached to the request (also, fix things a lot of things based on PRs there...)
This commit is contained in:
@@ -514,8 +514,8 @@ bool webAuthenticate(AsyncWebServerRequest *request) {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
AsyncWebServer * webServer() {
|
||||
return _server;
|
||||
AsyncWebServer& webServer() {
|
||||
return *_server;
|
||||
}
|
||||
|
||||
void webBodyRegister(web_body_callback_f callback) {
|
||||
@@ -536,22 +536,37 @@ uint16_t webPort() {
|
||||
}
|
||||
|
||||
void webLog(AsyncWebServerRequest *request) {
|
||||
DEBUG_MSG_P(PSTR("[WEBSERVER] Request: %s %s\n"), request->methodToString(), request->url().c_str());
|
||||
DEBUG_MSG_P(PSTR("[WEBSERVER] %s %s\n"), request->methodToString(), request->url().c_str());
|
||||
}
|
||||
|
||||
class WebAccessLogHandler : public AsyncWebHandler {
|
||||
bool canHandle(AsyncWebServerRequest* request) override {
|
||||
webLog(request);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
void webSetup() {
|
||||
|
||||
// Cache the Last-Modifier header value
|
||||
snprintf_P(_last_modified, sizeof(_last_modified), PSTR("%s %s GMT"), __DATE__, __TIME__);
|
||||
|
||||
// Create server
|
||||
// Create server and install global URL debug handler
|
||||
// (since we don't want to forcibly add it to each instance)
|
||||
unsigned int port = webPort();
|
||||
_server = new AsyncWebServer(port);
|
||||
|
||||
#if DEBUG_SUPPORT
|
||||
if (getSetting("webAccessLog", (1 == WEB_ACCESS_LOG))) {
|
||||
static WebAccessLogHandler log;
|
||||
_server->addHandler(&log);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Rewrites
|
||||
_server->rewrite("/", "/index.html");
|
||||
|
||||
// Serve home (basic authentication protection)
|
||||
// Serve home (basic authentication protection is done manually b/c the handler is installed through callback functions)
|
||||
#if WEB_EMBEDDED
|
||||
_server->on("/index.html", HTTP_GET, _onHome);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user