12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- #pragma once
-
- #include "filesys.h"
- #include <ESP8266WebServer.h>
-
- class HTTPServer : public ESP8266WebServer {
- private:
- const char* rootDir;
-
- bool addRootFileHandler();
- bool formatFS() {
- return format_fs();
- }
- void listRoot() {
- ls(rootDir);
- }
-
- public:
- HTTPServer(const int _port, const char* _rootDir) :
- ESP8266WebServer(_port), rootDir(_rootDir)
- { }
- ~HTTPServer()
- {
- Serial.printf("[HTTPServer] shut down ...\n\r");
- }
-
- bool start() {
- if(!mount_fs())
- return false;
- Serial.printf("[HTTPServer] LittleFS mounted !\n\r");
-
- Serial.printf("[HTTPServer] root:\n\r");
- 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("/terminal", [this]() {
- String log_msg = "terminal: millis: ";
- log_msg += millis();
- send(200, "text/plain", log_msg);
- });
-
- onNotFound([this]() {
- String message = "File Not Found\n\n";
- message += "URI: ";
- message += uri();
- message += "\nMethod: ";
- message += (method() == HTTP_GET) ? "GET" : "POST";
- message += "\nArguments: ";
- message += args();
- message += "\n";
-
- for (uint8_t i = 0; i < args(); i++) {
- message += " " + argName(i) + ": " + arg(i) + "\n";
- }
- send(404, "text/plain", message);
- });
-
- if( this->addRootFileHandler() ){
- this->begin();
- Serial.printf("[HTTPServer] Server active on Port 80 !\n\r");
- return true;
- }
- Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r");
- return false;
- }
- };
|