Created HTTP page processor
This commit is contained in:
92
include/HttpHandler.cpp
Normal file
92
include/HttpHandler.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
#include <WString.h>
|
||||
#include "HttpHandler.hpp"
|
||||
#include "logger.cpp"
|
||||
#include "SPIFFS.h"
|
||||
|
||||
PageProcessor::PageProcessor()
|
||||
{
|
||||
this->Dataset = new Dictionary<String, String>();
|
||||
}
|
||||
PageProcessor::PageProcessor(String file)
|
||||
{
|
||||
this->SourcePath = file;
|
||||
this->Dataset = new Dictionary<String, String>();
|
||||
}
|
||||
PageProcessor::PageProcessor(String file, Dictionary<String, String> *dataset)
|
||||
{
|
||||
this->SourcePath = file;
|
||||
this->Dataset = dataset;
|
||||
}
|
||||
|
||||
String PageProcessor::Parse(String input)
|
||||
{
|
||||
auto Result = input;
|
||||
Logger::Log("Count: " + Dataset->Count());
|
||||
for (size_t i = 0; i < Dataset->Count(); i++)
|
||||
{
|
||||
Logger::Log("Processing entry " + i);
|
||||
auto entry = Dataset->Get(i);
|
||||
auto key = entry.GetKey();
|
||||
auto val = entry.GetValue();
|
||||
Logger::Log("Processing [" + key + "] -> " + val);
|
||||
Result.replace(key, val);
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
String PageProcessor::Execute()
|
||||
{
|
||||
String Master, Content, Result = ReadFile(SourcePath);
|
||||
for (int i = 0; i < Compilers->Count(); i++)
|
||||
{
|
||||
auto cmp = Compilers->Get(i);
|
||||
if (Result.indexOf(cmp.GetKey()) >= 0)
|
||||
{
|
||||
auto v = cmp.GetValue()(Result);
|
||||
Result.replace(cmp.GetKey(), (String)v);
|
||||
}
|
||||
}
|
||||
|
||||
if (Result.indexOf("<!--INCLUDE MASTER-->") >= 0)
|
||||
{
|
||||
Logger::Log(Logger::TRACE, "Compiling page using master " + SourcePath);
|
||||
Master = ReadFile("/master.html");
|
||||
Content = Result;
|
||||
Result = Master;
|
||||
Result.replace("<@ContentPlaceHolder/>", Content);
|
||||
}
|
||||
|
||||
// Logger::Log(Logger::TRACE, "Parsing " + SourcePath);
|
||||
Result = PageProcessor::Parse(Result);
|
||||
// Logger::Log(Logger::TRACE, "Parsed result ----------------------");
|
||||
return Result;
|
||||
}
|
||||
|
||||
String PageProcessor::ReadFile(String Path)
|
||||
{
|
||||
String result = "";
|
||||
#ifdef LogFileRead
|
||||
//Logger::Log(Logger::TRACE, "Opening file" + Path);
|
||||
#endif
|
||||
if (!SPIFFS.exists(Path))
|
||||
{
|
||||
}
|
||||
File file = SPIFFS.open(Path, "r");
|
||||
if (file)
|
||||
{
|
||||
#ifdef LogFileRead
|
||||
//Logger::Log(Logger::TRACE, "Found");
|
||||
#endif
|
||||
file.setTimeout(100);
|
||||
result = file.readString();
|
||||
file.close();
|
||||
#ifdef LogFileRead
|
||||
//Logger::Log(Logger::TRACE, result);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger::Log(Logger::ERROR, "Could not open " + Path);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
26
include/HttpHandler.hpp
Normal file
26
include/HttpHandler.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "Dictionary.hpp"
|
||||
|
||||
#ifndef HttpRequestHandler
|
||||
#define HttpRequestHandler
|
||||
|
||||
typedef Dictionary<String, String> StringDictionary;
|
||||
typedef Dictionary<String, String (*)(String)> CompilerDictionary;
|
||||
|
||||
class PageProcessor
|
||||
{
|
||||
private:
|
||||
String ReadFile(String Path);
|
||||
|
||||
public:
|
||||
PageProcessor();
|
||||
PageProcessor(String file);
|
||||
PageProcessor(String file, Dictionary<String, String> *dataset);
|
||||
Dictionary<String, String>* Dataset;
|
||||
Dictionary<String, String (*)(String)>* Compilers;
|
||||
String Parse(String input);
|
||||
String Execute();
|
||||
String SourcePath;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user