Fixed that shitty two-server construct

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-24 15:35:07 +02:00
parent 48ad3ba7fe
commit 99c429872b
14 changed files with 56 additions and 171 deletions

View File

@ -2,7 +2,6 @@
using namespace Logging;
using namespace Rest;
using namespace UI;
Framework::Framework()
{
@ -13,7 +12,6 @@ Framework::Framework()
log->Flush();
restInterface = new RestInterface();
uiServer = new UIServer();
PostInit();
@ -26,10 +24,8 @@ Framework::Framework()
Framework::~Framework()
{
delete restInterface;
delete uiServer;
delete log;
uiServer = nullptr;
restInterface = nullptr;
log = nullptr;
@ -41,7 +37,6 @@ void Framework::Run()
while (XGControl::keepServerRunning)
{
restInterface->Update();
uiServer->Update();
}
OnExit();
@ -64,7 +59,6 @@ void Framework::PreInit()
void Framework::PostInit()
{
restInterface->PostInit();
uiServer->PostInit();
return;
}
@ -72,7 +66,6 @@ void Framework::PostInit()
void Framework::OnExit()
{
restInterface->OnExit();
uiServer->OnExit();
return;
}

View File

@ -1,7 +1,6 @@
#pragma once
#include "Logger.h"
#include "LogHistory.h"
#include "UIServer.h"
#include "RestInterface.h"
#include "XGControl.h"
@ -20,7 +19,6 @@ private:
void PostExit();
Rest::RestInterface* restInterface;
UI::UIServer* uiServer;
Logging::Logger* log;

View File

@ -8,9 +8,7 @@ Logger::Logger(std::string identifier)
std::stringstream ss;
rawIdentifier = identifier;
ss << "[" << rawIdentifier << "]";
this->identifier = ss.str();
this->identifier = identifier;
return;
}
@ -47,11 +45,11 @@ std::string Logger::Flush()
strftime(timeBuf, 100, "%d.%m.%Y - %T", &currTm);
std::stringstream bufOut;
bufOut << "<" << timeBuf << "> " << identifier << TypeToPrefix(type) << ": " << cout.str();
bufOut << "<" << timeBuf << "> [" << identifier << "]" << TypeToPrefix(type) << ((additionalInfo.length() > 0) ? " " : "") << additionalInfo << ": " << cout.str();
LogEntry* newEntry = new LogEntry;
newEntry->message = bufOut.str();
newEntry->identifier = rawIdentifier;
newEntry->identifier = identifier;
newEntry->timestamp = currTime;
newEntry->type = type;
LogHistory::AddLogToHistory(newEntry);
@ -67,7 +65,6 @@ std::string Logger::Type(LOG_TYPE type)
{
if (!IsInitializedSanityCheck()) return "";
this->type = type;
return "";
}

View File

@ -21,6 +21,9 @@ namespace Logging
//Sets the buffered string
void Set(std::string str);
//Sets additional information to be appended after the identifier
void SetAdditionalInformation(std::string str) { additionalInfo = str; };
//Prints the buffered string to the console and clears it
std::string Flush();
@ -42,7 +45,7 @@ namespace Logging
bool IsInitializedSanityCheck();
std::string identifier;
std::string rawIdentifier;
std::string additionalInfo = "";
LOG_TYPE type = LOG_TYPE::LOG;
bool isInitialized = false;

View File

@ -48,6 +48,8 @@ bool RestInterface::InitWebServer()
}
mg_set_protocol_http_websocket(pNc);
frontend_serve_opts.document_root = "frontend";
frontend_serve_opts.enable_directory_listing = "no";
log->cout << "Started web server successfully!";
log->Flush();
@ -78,28 +80,15 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
case MG_EV_HTTP_REQUEST:
http_message* hpm = (http_message*)p;
std::string requestBodyRaw = FixUnterminatedString(hpm->body.p, hpm->body.len);
std::string requestedUri = FixUnterminatedString(hpm->uri.p, hpm->uri.len);
if (IsJsonValid(requestBodyRaw))
if ((requestedUri == "/api"))
{
Json requestBody;
requestBody.Parse(requestBodyRaw);
char addr[32];
mg_sock_addr_to_str(&pNc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
XGControl::lastIPThatRequested = std::string(addr);
JsonBlock responseBody;
HTTP_STATUS_CODE returnCode;
RestQueryHandler::ProcessQuery(requestBody, responseBody, returnCode);
Json response(responseBody);
ServeStringToConnection(pNc, response.Render(), returnCode);
ProcessAPIRequest(pNc, ev, p);
}
else
{
Json errorJson = RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Received json is fucked up");
ServeStringToConnection(pNc, errorJson.Render(), HTTP_STATUS_CODE::BAD_REQUEST);
mg_serve_http(pNc, (struct http_message*)p, frontend_serve_opts);
}
@ -109,6 +98,39 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
return;
}
void RestInterface::ProcessAPIRequest(mg_connection* pNc, int ev, void* p)
{
// Get struct with http message informations
http_message* hpm = (http_message*)p;
// Get the transmitted message body
std::string requestBodyRaw = FixUnterminatedString(hpm->body.p, hpm->body.len);
// Check for the body being valid json
if (IsJsonValid(requestBodyRaw))
{
Json requestBody;
requestBody.Parse(requestBodyRaw);
char addr[32];
mg_sock_addr_to_str(&pNc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP);
JsonBlock responseBody;
HTTP_STATUS_CODE returnCode;
RestQueryHandler::ProcessQuery(std::string(addr), requestBody, responseBody, returnCode);
Json response(responseBody);
ServeStringToConnection(pNc, response.Render(), returnCode);
}
else // return error message for invalid json
{
Json errorJson = RestResponseTemplates::GetByCode(HTTP_STATUS_CODE::BAD_REQUEST, "Received json is fucked up");
ServeStringToConnection(pNc, errorJson.Render(), HTTP_STATUS_CODE::BAD_REQUEST);
}
return;
}
void RestInterface::OnExit()
{
log->cout << "Shutting down rest api server...";
@ -130,3 +152,5 @@ std::string RestInterface::FixUnterminatedString(const char* cstr, const std::si
return ss.str();
}
mg_serve_http_opts RestInterface::frontend_serve_opts;

View File

@ -24,6 +24,7 @@ namespace Rest
private:
bool InitWebServer();
static void ProcessAPIRequest(struct mg_connection* pNc, int ev, void* p);
static void EventHandler(struct mg_connection* pNc, int ev, void* p);
static void ServeStringToConnection(struct mg_connection* c, std::string str, int httpStatusCode = 200);
@ -32,6 +33,7 @@ namespace Rest
struct mg_mgr* pMgr;
struct mg_connection* pNc;
static mg_serve_http_opts frontend_serve_opts;
Logging::Logger* log;

View File

@ -11,8 +11,10 @@ void RestQueryHandler::PreInit()
return;
}
bool RestQueryHandler::ProcessQuery(const Json& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
bool RestQueryHandler::ProcessQuery(const std::string clientAdress, const Json& request, JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode)
{
log->SetAdditionalInformation(std::string("@") + clientAdress);
if (!ValidateField("request", JSON_DATA_TYPE::STRING, request, responseBody))
{
responseCode = BAD_REQUEST;
@ -42,7 +44,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
{
XGControl::keepServerRunning = false;
log->cout << "Shutting down server upon rest request by " << XGControl::lastIPThatRequested << "...";
log->cout << "Shutting down server upon rest request...";
log->Flush();
responseCode = OK;

View File

@ -11,7 +11,7 @@ namespace Rest
public:
static void PreInit();
static bool ProcessQuery(const JasonPP::Json& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static bool ProcessQuery(const std::string clientAdress, const JasonPP::Json& request, JasonPP::JsonBlock& responseBody, HTTP_STATUS_CODE& responseCode);
static void PostExit();

View File

@ -148,7 +148,6 @@
<ClCompile Include="RestQueryHandler.cpp" />
<ClCompile Include="RestResponseTemplates.cpp" />
<ClCompile Include="RestInterface.cpp" />
<ClCompile Include="UIServer.cpp" />
<ClCompile Include="XGControl.cpp" />
</ItemGroup>
<ItemGroup>
@ -161,7 +160,6 @@
<ClInclude Include="RestQueryHandler.h" />
<ClInclude Include="RestResponseTemplates.h" />
<ClInclude Include="RestInterface.h" />
<ClInclude Include="UIServer.h" />
<ClInclude Include="XGControl.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -48,9 +48,6 @@
<ClCompile Include="XGControl.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
<ClCompile Include="UIServer.cpp">
<Filter>Quelldateien</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="JasonPP.hpp">
@ -83,8 +80,5 @@
<ClInclude Include="XGControl.h">
<Filter>Headerdateien</Filter>
</ClInclude>
<ClInclude Include="UIServer.h">
<Filter>Headerdateien</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -1,91 +0,0 @@
#include "UIServer.h"
using namespace UI;
using namespace Logging;
UIServer::UIServer()
{
pMgr = new mg_mgr();
pNc = nullptr;
log = new Logger("UIServer");
return;
}
UIServer::~UIServer()
{
delete pMgr;
delete log;
log = nullptr;
pMgr = nullptr;
return;
}
void UIServer::PostInit()
{
isBootedSuccessfully = InitWebServer();
return;
}
bool UIServer::InitWebServer()
{
mg_mgr_init(pMgr, NULL);
log->cout << "Starting ui web server on port " << WEBUI_SERVER_PORT << "...";
log->Flush();
pNc = mg_bind(pMgr, WEBUI_SERVER_PORT, this->EventHandler);
if (pNc == NULL)
{
log->cout << log->Err() << "Failed to boot ui web server! - Unable to bind listener! (port: " << WEBUI_SERVER_PORT << ")";
log->Flush();
return false;
}
mg_set_protocol_http_websocket(pNc);
serve_opts.document_root = "frontend"; // Serve current directory
serve_opts.enable_directory_listing = "no";
log->cout << "Started web server successfully!";
log->Flush();
isBootedSuccessfully = true;
return true;
}
void UIServer::Update()
{
mg_mgr_poll(pMgr, WEBUI_SERVER_POLLRATE);
return;
}
void UIServer::EventHandler(mg_connection* pNc, int ev, void* p)
{
switch (ev)
{
case MG_EV_HTTP_REQUEST:
mg_serve_http(pNc, (struct http_message*)p, serve_opts);
break;
}
return;
}
void UIServer::OnExit()
{
log->cout << "Shutting down ui web server...";
log->Flush();
mg_mgr_free(pMgr);
return;
}
mg_serve_http_opts UIServer::serve_opts;

View File

@ -1,33 +0,0 @@
#pragma once
#include "mongoose.h"
#include "Logger.h"
#define WEBUI_SERVER_PORT "6970"
#define WEBUI_SERVER_POLLRATE 0
namespace UI
{
class UIServer
{
public:
UIServer();
~UIServer();
void PostInit();
void Update();
void OnExit();
private:
bool InitWebServer();
static void EventHandler(struct mg_connection* pNc, int ev, void* p);
struct mg_mgr* pMgr;
struct mg_connection* pNc;
static mg_serve_http_opts serve_opts;
Logging::Logger* log;
bool isBootedSuccessfully = false;
};
}

View File

@ -1,4 +1,3 @@
#include "XGControl.h"
bool XGControl::keepServerRunning = false;
std::string XGControl::lastIPThatRequested = "";

View File

@ -8,6 +8,5 @@ class XGControl
{
public:
static bool keepServerRunning;
static std::string lastIPThatRequested;
};