Implemented API to list and download log files

This commit is contained in:
Youen 2024-05-25 23:51:17 +02:00
parent 6c40ba4e1f
commit 9c08024a6c
2 changed files with 45 additions and 0 deletions

View File

@ -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

View File

@ -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);