@@ -28,7 +28,7 @@ xhr.onreadystatechange = function(){ | |||
} | |||
function reloadTerminal() { | |||
xhr.open("GET", "/terminal", true); | |||
xhr.open("POST", "/app=terminal", true); | |||
xhr.send(); | |||
setTimeout(reloadTerminal, 1000); | |||
}; |
@@ -1,42 +1,42 @@ | |||
#include "httpserver.h" | |||
bool HTTPServer::start() { | |||
if(!mount_fs()) { | |||
logf("cant mount filesystem, EXIT !\n\r"); | |||
return false; | |||
} | |||
logf("[HTTPServer] LittleFS mounted !\n\r"); | |||
logf("[HTTPServer] root:\n\r"); | |||
this->listRoot(); | |||
logf("\n\r"); | |||
// default handler | |||
this->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); | |||
}); | |||
bool HTTPServer::addRootFileHandler() { | |||
// //experimental, see doku.md | |||
// server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip"); | |||
this->serveStatic("", LittleFS, "/index.html"); | |||
this->serveStatic("/", LittleFS, "/index.html"); | |||
this->serveStatic("/#", LittleFS, "/index.html"); | |||
this->serveStatic("/style.css", LittleFS, "/style.css"); | |||
this->serveStatic("/input.js", LittleFS, "/input.js"); | |||
this->serveStatic("/favicon.png", LittleFS, "/favicon.png"); | |||
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"); | |||
// add static root file handler for http | |||
this->serveStatic("/", LittleFS, "/"); | |||
// application handler | |||
this->on("/app=terminal", HTTP_POST, [this]() { | |||
String log_msg = "terminal: millis: "; | |||
log_msg += millis(); | |||
send(200, "text/plain", log_msg); | |||
}); | |||
} | |||
return true; | |||
this->begin(); | |||
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); | |||
} |
@@ -1,21 +1,20 @@ | |||
#pragma once | |||
#include "filesys.h" | |||
#include <ESP8266WebServer.h> | |||
#include <stdarg.h> | |||
#include "filesys.h" | |||
// debug log <ESP8266WebServer.h> | |||
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) | |||
#define LOG_STR "[HTTPServer]" | |||
class HTTPServer : public ESP8266WebServer { | |||
private: | |||
const char* rootDir = "/"; | |||
bool addRootFileHandler(); | |||
bool addRootFileHandler_ls(); | |||
bool formatFS() { | |||
return format_fs(); | |||
} | |||
void listRoot() { | |||
ls(rootDir); | |||
} | |||
@@ -36,73 +35,5 @@ public: | |||
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"); | |||
// 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: "; | |||
log_msg += millis(); | |||
send(200, "text/plain", log_msg); | |||
}); | |||
// default handler | |||
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_ls() ){ | |||
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; | |||
} | |||
bool start(); | |||
}; |