From 69984e02fc45b3b47ae5912004108191cc417e2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 18 Feb 2017 00:39:30 +0100 Subject: [PATCH] Experimental option to embed the web interface in the firmware image --- code/build-data_h | 18 +++++++++++++++++ code/espurna/config/general.h | 2 ++ code/espurna/web.ino | 37 ++++++++++++++++++++++++++--------- 3 files changed, 48 insertions(+), 9 deletions(-) create mode 100644 code/build-data_h diff --git a/code/build-data_h b/code/build-data_h new file mode 100644 index 00000000..3df79488 --- /dev/null +++ b/code/build-data_h @@ -0,0 +1,18 @@ +#!/bin/python + +import binascii +import string + +source = 'espurna/data/index.html.gz' +destination = 'espurna/config/data.h' + +with open(source, 'rb') as f: + content = f.read() + +array = map(lambda x: '0x%02x' % ord(x), content) + +with open(destination, 'w') as f: + f.write("#define index_html_gz_len %d\n" % len(array)) + f.write("const uint8_t index_html_gz[] PROGMEM = {") + f.write(string.join(array, ',')) + f.write("};\n") diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 2406eb29..6254e016 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -101,6 +101,8 @@ #define AP_MODE_GW "192.168.4.1" #define AP_MODE_MASK "255.255.255.0" +#define EMBED_WEB_IN_FIRMWARE 0 + // ----------------------------------------------------------------------------- // OTA & NOFUSS // ----------------------------------------------------------------------------- diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 5ab383f6..9d68256b 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -16,6 +16,10 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include #include +#if EMBED_WEB_IN_FIRMWARE == 1 +#include "config/data.h" +#endif + AsyncWebServer * _server; AsyncWebSocket ws("/ws"); Ticker deferred; @@ -702,6 +706,14 @@ void _onAuth(AsyncWebServerRequest *request) { } +#if EMBED_WEB_IN_FIRMWARE == 1 +void _onHome(AsyncWebServerRequest *request) { + AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", index_html_gz, index_html_gz_len); + response->addHeader("Content-Encoding", "gzip"); + request->send(response); +} +#endif + void webSetup() { // Create server @@ -714,21 +726,28 @@ void webSetup() { // Setup webserver _server->addHandler(&ws); + // Rewrites + _server->rewrite("/", "/index.html"); + // Serve home (basic authentication protection) + #if EMBED_WEB_IN_FIRMWARE == 1 + _server->on("/index.html", HTTP_GET, _onHome); + #endif _server->on("/auth", HTTP_GET, _onAuth); _server->on("/apis", HTTP_GET, _onAPIs); _server->on("/rpc", HTTP_GET, _onRPC); // Serve static files - char lastModified[50]; - sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__); - _server->rewrite("/", "/index.html"); - _server->serveStatic("/", SPIFFS, "/") - .setLastModified(lastModified) - .setFilter([](AsyncWebServerRequest *request) -> bool { - webLogRequest(request); - return true; - }); + #if EMBED_WEB_IN_FIRMWARE == 0 + char lastModified[50]; + sprintf(lastModified, "%s %s GMT", __DATE__, __TIME__); + _server->serveStatic("/", SPIFFS, "/") + .setLastModified(lastModified) + .setFilter([](AsyncWebServerRequest *request) -> bool { + webLogRequest(request); + return true; + }); + #endif // 404 _server->onNotFound([](AsyncWebServerRequest *request){