Added frontend system

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-24 15:15:18 +02:00
parent 595e04bc32
commit 48ad3ba7fe
13 changed files with 174 additions and 9 deletions

View File

@ -2,6 +2,7 @@
using namespace Logging; using namespace Logging;
using namespace Rest; using namespace Rest;
using namespace UI;
Framework::Framework() Framework::Framework()
{ {
@ -10,7 +11,9 @@ Framework::Framework()
log = new Logger("Framework"); log = new Logger("Framework");
log->cout << "Starting Tubio server..."; log->cout << "Starting Tubio server...";
log->Flush(); log->Flush();
restInterface = new RestInterface(); restInterface = new RestInterface();
uiServer = new UIServer();
PostInit(); PostInit();
@ -23,8 +26,10 @@ Framework::Framework()
Framework::~Framework() Framework::~Framework()
{ {
delete restInterface; delete restInterface;
delete uiServer;
delete log; delete log;
uiServer = nullptr;
restInterface = nullptr; restInterface = nullptr;
log = nullptr; log = nullptr;
@ -36,6 +41,7 @@ void Framework::Run()
while (XGControl::keepServerRunning) while (XGControl::keepServerRunning)
{ {
restInterface->Update(); restInterface->Update();
uiServer->Update();
} }
OnExit(); OnExit();
@ -58,6 +64,7 @@ void Framework::PreInit()
void Framework::PostInit() void Framework::PostInit()
{ {
restInterface->PostInit(); restInterface->PostInit();
uiServer->PostInit();
return; return;
} }
@ -65,6 +72,7 @@ void Framework::PostInit()
void Framework::OnExit() void Framework::OnExit()
{ {
restInterface->OnExit(); restInterface->OnExit();
uiServer->OnExit();
return; return;
} }

View File

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

View File

@ -10,8 +10,6 @@ RestInterface::RestInterface()
pNc = nullptr; pNc = nullptr;
log = new Logger("WebServer"); log = new Logger("WebServer");
isBootedSuccessfully = false;
return; return;
} }
@ -37,14 +35,14 @@ bool RestInterface::InitWebServer()
{ {
mg_mgr_init(pMgr, NULL); mg_mgr_init(pMgr, NULL);
log->cout << "Starting web server on port " << WEBAPI_SERVER_PORT << "..."; log->cout << "Starting rest api server on port " << WEBAPI_SERVER_PORT << "...";
log->Flush(); log->Flush();
pNc = mg_bind(pMgr, WEBAPI_SERVER_PORT, this->EventHandler); pNc = mg_bind(pMgr, WEBAPI_SERVER_PORT, this->EventHandler);
if (pNc == NULL) if (pNc == NULL)
{ {
log->cout << log->Err() << "Failed to boot the web server! - Unable to bind listener!"; log->cout << log->Err() << "Failed to boot rest api web server! - Unable to bind listener! (port: " << WEBAPI_SERVER_PORT << ")";
log->Flush(); log->Flush();
return false; return false;
} }
@ -86,7 +84,11 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
{ {
Json requestBody; Json requestBody;
requestBody.Parse(requestBodyRaw); 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; JsonBlock responseBody;
HTTP_STATUS_CODE returnCode; HTTP_STATUS_CODE returnCode;
RestQueryHandler::ProcessQuery(requestBody, responseBody, returnCode); RestQueryHandler::ProcessQuery(requestBody, responseBody, returnCode);
@ -109,6 +111,9 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
void RestInterface::OnExit() void RestInterface::OnExit()
{ {
log->cout << "Shutting down rest api server...";
log->Flush();
mg_mgr_free(pMgr); mg_mgr_free(pMgr);
return; return;

View File

@ -35,6 +35,6 @@ namespace Rest
Logging::Logger* log; Logging::Logger* log;
bool isBootedSuccessfully; bool isBootedSuccessfully = false;
}; };
} }

View File

@ -42,7 +42,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
{ {
XGControl::keepServerRunning = false; XGControl::keepServerRunning = false;
log->cout << "Shutting down server upon rest request..."; log->cout << "Shutting down server upon rest request by " << XGControl::lastIPThatRequested << "...";
log->Flush(); log->Flush();
responseCode = OK; responseCode = OK;

View File

@ -9,7 +9,6 @@ namespace Rest
UNAUTHORIZED = 401, UNAUTHORIZED = 401,
FORBIDDEN = 403, FORBIDDEN = 403,
NOT_FOUND = 404, NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
INTERNAL_SERVER_ERROR = 500, INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501 NOT_IMPLEMENTED = 501
}; };
@ -25,7 +24,6 @@ namespace Rest
static JasonPP::JsonBlock Unauthorized(std::string message); static JasonPP::JsonBlock Unauthorized(std::string message);
static JasonPP::JsonBlock Forbidden(std::string message); static JasonPP::JsonBlock Forbidden(std::string message);
static JasonPP::JsonBlock NotFound(std::string message); static JasonPP::JsonBlock NotFound(std::string message);
static JasonPP::JsonBlock MethodNotAllowed(std::string message);
static JasonPP::JsonBlock InternalServerError(std::string message); static JasonPP::JsonBlock InternalServerError(std::string message);
static JasonPP::JsonBlock NotImplemented(std::string message); static JasonPP::JsonBlock NotImplemented(std::string message);

View File

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

View File

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

91
Tubio/UIServer.cpp Normal file
View File

@ -0,0 +1,91 @@
#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;

33
Tubio/UIServer.h Normal file
View File

@ -0,0 +1,33 @@
#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,3 +1,4 @@
#include "XGControl.h" #include "XGControl.h"
bool XGControl::keepServerRunning = false; bool XGControl::keepServerRunning = false;
std::string XGControl::lastIPThatRequested = "";

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <string>
/// <summary> /// <summary>
/// Class to house control variables /// Class to house control variables
@ -7,5 +8,6 @@ class XGControl
{ {
public: public:
static bool keepServerRunning; static bool keepServerRunning;
static std::string lastIPThatRequested;
}; };

17
Tubio/frontend/index.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello, World!</title>
</head>
<body style="position: relative;">
<div style="width: 100%; height: 100vh; display: flex; justify-content: center; align-items: center; text-align: center; font-size: 38pt; font-family: sans-serif;">
<p>
HELLO, WORLD :3
</p>
</div>
</body>
</html>