From 68c451aa823d8b8b68717b570bb7b737aec91934 Mon Sep 17 00:00:00 2001 From: Thomas Basler Date: Sun, 22 May 2022 22:42:06 +0200 Subject: [PATCH] Serve webapp from flash instead of LITTLEFS This allows a web based firmware update by just replacing one bin file --- platformio.ini | 1 + src/WebApi.cpp | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 82a5580..d0442c8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,6 +20,7 @@ platform = espressif32 build_flags = ${env.build_flags} -D=${PIOENV} + -DCOMPONENT_EMBED_FILES=data/index.html.gz:data/zones.json.gz:data/favicon.ico:data/js/app.js.gz lib_deps = https://github.com/me-no-dev/ESPAsyncWebServer.git diff --git a/src/WebApi.cpp b/src/WebApi.cpp index d076ee3..a4c9eea 100644 --- a/src/WebApi.cpp +++ b/src/WebApi.cpp @@ -17,6 +17,16 @@ WebApiClass::WebApiClass() { } +extern const uint8_t file_index_html_start[] asm("_binary_data_index_html_gz_start"); +extern const uint8_t file_favicon_ico_start[] asm("_binary_data_favicon_ico_start"); +extern const uint8_t file_zones_json_start[] asm("_binary_data_zones_json_gz_start"); +extern const uint8_t file_app_js_start[] asm("_binary_data_js_app_js_gz_start"); + +extern const uint8_t file_index_html_end[] asm("_binary_data_index_html_gz_end"); +extern const uint8_t file_favicon_ico_end[] asm("_binary_data_favicon_ico_end"); +extern const uint8_t file_zones_json_end[] asm("_binary_data_zones_json_gz_end"); +extern const uint8_t file_app_js_end[] asm("_binary_data_js_app_js_gz_end"); + void WebApiClass::init() { using namespace std::placeholders; @@ -45,7 +55,36 @@ void WebApiClass::init() _server.on("/api/inverter/edit", HTTP_POST, std::bind(&WebApiClass::onInverterEdit, this, _1)); _server.on("/api/inverter/del", HTTP_POST, std::bind(&WebApiClass::onInverterDelete, this, _1)); - _server.serveStatic("/", LittleFS, "/", "max-age=86400").setDefaultFile("index.html"); + _server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", file_index_html_start, file_index_html_end - file_index_html_start); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + _server.on("/index.html", HTTP_GET, [](AsyncWebServerRequest* request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", file_index_html_start, file_index_html_end - file_index_html_start); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + _server.on("/favicon.ico", HTTP_GET, [](AsyncWebServerRequest* request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "image/x-icon", file_favicon_ico_start, file_favicon_ico_end - file_favicon_ico_start); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + _server.on("/zones.json", HTTP_GET, [](AsyncWebServerRequest* request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "application/json", file_zones_json_start, file_zones_json_end - file_zones_json_start); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + + _server.on("/js/app.js", HTTP_GET, [](AsyncWebServerRequest* request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/javascript", file_app_js_start, file_app_js_end - file_app_js_start); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); + }); + _server.onNotFound(std::bind(&WebApiClass::onNotFound, this, _1)); _server.begin(); }