diff --git a/lib/httpserver/httpserver.cpp b/lib/httpserver/httpserver.cpp index 36df250..1eeddd3 100644 --- a/lib/httpserver/httpserver.cpp +++ b/lib/httpserver/httpserver.cpp @@ -15,3 +15,28 @@ bool HTTPServer::addRootFileHandler() { return true; } + + +bool HTTPServer::addRootFileHandler_ls() { + logf("addRootFileHandler_ls: %s\n\r", rootDir); + Dir root = LittleFS.openDir(rootDir); + + while (root.next()) { + const String& fName = "/" + root.fileName(); + const String& fName_gz = "/" + root.fileName() + ".gz"; + Serial.printf("Register Callback for: %s @ %s\n\r", + fName.c_str(), fName_gz.c_str()); + + // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 + on(fName, [this, fName, fName_gz]() { + + logf("addRootFileHandler_ls: %s\n\r", rootDir); + File f = open(fName_gz.c_str()); + if (f) + streamFile(f, mime::getContentType(fName)); + else + send(404, "text/plain", "Failed to open file for reading"); + }); + } + return true; +} \ No newline at end of file diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 7078bff..443ba29 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -2,18 +2,30 @@ #include "filesys.h" #include +#include + +#define LOG_STR "[HTTPServer]" + class HTTPServer : public ESP8266WebServer { private: - const char* rootDir; + const char* rootDir = "/"; bool addRootFileHandler(); + bool addRootFileHandler_ls(); bool formatFS() { return format_fs(); } void listRoot() { ls(rootDir); } + void logf(const char *format, ...) { + va_list args; + va_start(args, format); + Serial.print(LOG_STR); + Serial.printf(format, args); + va_end(args); + } public: HTTPServer(const int _port, const char* _rootDir) : @@ -33,33 +45,34 @@ public: this->listRoot(); Serial.printf("\n\r"); - // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 - on("/compress.html", [this]() { - File f = open("/compress.html.gz"); - if (!f) { - Serial.println("Failed to open file for reading"); - send(404, "text/plain", "Failed to open file for reading"); - } - else - { - Serial.println("Streaming /compress.html.gz to client"); - const String cont = "text/html"; - streamFile(f, cont); - } - }); - on("/comp.html", [this]() { - File f = open("/comp.html.gz"); - if (!f) { - Serial.println("Failed to open file for reading"); - send(404, "text/plain", "Failed to open file for reading"); - } - else - { - Serial.println("Streaming /comp.html.gz to client"); - const String cont = "text/html"; - streamFile(f, cont); - } - }); + // on("/compress.html", [this]() { + // File f = open("/compress.html.gz"); + // if (!f) { + // Serial.println("Failed to open file for reading"); + // send(404, "text/plain", "Failed to open file for reading"); + // } + // else + // { + // Serial.println("Streaming /compress.html.gz to client"); + // const String cont = "text/html"; + // streamFile(f, cont); + // } + // f.close(); + // }); + // on("/comp.html", [this]() { + // File f = open("/comp.html.gz"); + // if (!f) { + // Serial.println("Failed to open file for reading"); + // send(404, "text/plain", "Failed to open file for reading"); + // } + // else + // { + // Serial.println("Streaming /comp.html.gz to client"); + // const String cont = "text/html"; + // streamFile(f, cont); + // } + // f.close(); + // }); on("/terminal", [this]() { String log_msg = "terminal: millis: "; @@ -67,6 +80,7 @@ public: send(200, "text/plain", log_msg); }); + // default handler onNotFound([this]() { String message = "File Not Found\n\n"; message += "URI: "; @@ -83,7 +97,7 @@ public: send(404, "text/plain", message); }); - if( this->addRootFileHandler() ){ + if( this->addRootFileHandler_ls() ){ this->begin(); Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); return true;