Browse Source

Implemented API to list and download log files

master
Youen 7 months ago
parent
commit
9c08024a6c
  1. 43
      ESP32/src/WebServer.cpp
  2. 2
      ESP32/src/WebServer.h

43
ESP32/src/WebServer.cpp

@ -70,8 +70,12 @@ namespace detail
// API
ResourceNode* nodeGetStatus = new ResourceNode("/api/status", "GET", &WebServer::HandleGetStatus_);
ResourceNode* nodePostInfo = new ResourceNode("/api/info", "POST", &WebServer::HandlePostInfo_);
ResourceNode* nodeGetLogList = new ResourceNode("/api/log/list", "GET", &WebServer::HandleLogList_);
ResourceNode* nodeGetLog = new ResourceNode("/api/log/*", "GET", &WebServer::HandleLog_);
httpServer.registerNode(nodeGetStatus);
httpServer.registerNode(nodePostInfo);
httpServer.registerNode(nodeGetLogList);
httpServer.registerNode(nodeGetLog);
// Default node (either a static file, if found, or a 404 error)
ResourceNode* nodeDefault = new ResourceNode("", "GET", &WebServer::HandleDefault_);
@ -206,6 +210,45 @@ namespace detail
}
}
void WebServer::HandleLogList_(HTTPRequest * request, HTTPResponse * response)
{
String json;
json = "{\"files\":[";
auto logFolder = SPIFFS.open("/log");
auto file = logFolder.openNextFile();
bool first = true;
while(file)
{
if(!first) json += ",";
json += "{\"n\":\"/api/log/";
//::DebugLog.println(file.name());
json += file.name();
json += "\",\"s\":";
json += file.size();
json += "}";
first = false;
file = logFolder.openNextFile();
}
json += "]}";
response->setHeader("Content-Type", "text/json");
response->print(json.c_str());
}
void WebServer::HandleLog_(HTTPRequest * request, HTTPResponse * response)
{
auto* params = request->getParams();
std::string path = params->getPathParameter(0);
std::string fullPath = std::string("/log/") + path;
File file = SPIFFS.open(fullPath.c_str());
SendContent_(file, response);
file.close();
}
void WebServer::HandleDefault_(HTTPRequest * request, HTTPResponse * response)
{
// Discard request body, if we received any

2
ESP32/src/WebServer.h

@ -33,6 +33,8 @@ namespace detail
static void HandleIndex_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void HandleGetStatus_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void HandlePostInfo_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void HandleLogList_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void HandleLog_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void HandleDefault_(httpsserver::HTTPRequest * request, httpsserver::HTTPResponse * response);
static void SendContent_(Stream& stream, httpsserver::HTTPResponse * response);

Loading…
Cancel
Save