diff --git a/include/HttpHandler.cpp b/include/HttpHandler.cpp new file mode 100644 index 0000000..0203d79 --- /dev/null +++ b/include/HttpHandler.cpp @@ -0,0 +1,92 @@ +#include +#include "HttpHandler.hpp" +#include "logger.cpp" +#include "SPIFFS.h" + +PageProcessor::PageProcessor() +{ + this->Dataset = new Dictionary(); +} +PageProcessor::PageProcessor(String file) +{ + this->SourcePath = file; + this->Dataset = new Dictionary(); +} +PageProcessor::PageProcessor(String file, Dictionary *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("") >= 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; +} diff --git a/include/HttpHandler.hpp b/include/HttpHandler.hpp new file mode 100644 index 0000000..7873a72 --- /dev/null +++ b/include/HttpHandler.hpp @@ -0,0 +1,26 @@ +#include +#include "Dictionary.hpp" + +#ifndef HttpRequestHandler +#define HttpRequestHandler + +typedef Dictionary StringDictionary; +typedef Dictionary CompilerDictionary; + +class PageProcessor +{ +private: + String ReadFile(String Path); + +public: + PageProcessor(); + PageProcessor(String file); + PageProcessor(String file, Dictionary *dataset); + Dictionary* Dataset; + Dictionary* Compilers; + String Parse(String input); + String Execute(); + String SourcePath; +}; + +#endif \ No newline at end of file