68 lines
1.7 KiB
C++

#include "httpserver.h"
bool HTTPServer::start() {
if(!mount_fs()) {
logf("cant mount filesystem, EXIT !\n\r");
return false;
}
logf("LittleFS mounted !\n\r");
logf("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);
});
// add static root file handler for http
this->serveStatic("/", LittleFS, "/");
this->begin();
Serial.printf("Server active on Port 80 !\n\r");
return true;
}
void HTTPServer::logf(const char *format, ...) {
va_list args;
va_start(args, format);
char temp[64];
size_t len = vsnprintf(temp, sizeof(temp), format, args);
Serial.print(log_prefix);
Serial.write(temp, len);
va_end(args);
}
void HTTPServer::logt(const char *format, ...) {
va_list args;
va_start(args, format);
// append logging string to local buffer
if(tbuf_head < TBUF_LEN) {
size_t len = vsnprintf(tbuf+tbuf_head, TBUF_LEN-tbuf_head, format, args);
tbuf_head += len;
}
va_end(args);
}
void HTTPServer::start_apps() {
// application handler
this->on("/app=terminal", HTTP_POST, [this]() {
if(tbuf_head) {
send(200, "text/plain", tbuf);
tbuf_head = 0;
}
});
}