Added frontend system
This commit is contained in:
parent
595e04bc32
commit
48ad3ba7fe
@ -2,6 +2,7 @@
|
||||
|
||||
using namespace Logging;
|
||||
using namespace Rest;
|
||||
using namespace UI;
|
||||
|
||||
Framework::Framework()
|
||||
{
|
||||
@ -10,7 +11,9 @@ Framework::Framework()
|
||||
log = new Logger("Framework");
|
||||
log->cout << "Starting Tubio server...";
|
||||
log->Flush();
|
||||
|
||||
restInterface = new RestInterface();
|
||||
uiServer = new UIServer();
|
||||
|
||||
PostInit();
|
||||
|
||||
@ -23,8 +26,10 @@ Framework::Framework()
|
||||
Framework::~Framework()
|
||||
{
|
||||
delete restInterface;
|
||||
delete uiServer;
|
||||
delete log;
|
||||
|
||||
uiServer = nullptr;
|
||||
restInterface = nullptr;
|
||||
log = nullptr;
|
||||
|
||||
@ -36,6 +41,7 @@ void Framework::Run()
|
||||
while (XGControl::keepServerRunning)
|
||||
{
|
||||
restInterface->Update();
|
||||
uiServer->Update();
|
||||
}
|
||||
|
||||
OnExit();
|
||||
@ -58,6 +64,7 @@ void Framework::PreInit()
|
||||
void Framework::PostInit()
|
||||
{
|
||||
restInterface->PostInit();
|
||||
uiServer->PostInit();
|
||||
|
||||
return;
|
||||
}
|
||||
@ -65,6 +72,7 @@ void Framework::PostInit()
|
||||
void Framework::OnExit()
|
||||
{
|
||||
restInterface->OnExit();
|
||||
uiServer->OnExit();
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
#include "Logger.h"
|
||||
#include "LogHistory.h"
|
||||
#include "UIServer.h"
|
||||
#include "RestInterface.h"
|
||||
#include "XGControl.h"
|
||||
|
||||
@ -19,6 +20,7 @@ private:
|
||||
void PostExit();
|
||||
|
||||
Rest::RestInterface* restInterface;
|
||||
UI::UIServer* uiServer;
|
||||
|
||||
Logging::Logger* log;
|
||||
|
||||
|
@ -10,8 +10,6 @@ RestInterface::RestInterface()
|
||||
pNc = nullptr;
|
||||
log = new Logger("WebServer");
|
||||
|
||||
isBootedSuccessfully = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@ -37,14 +35,14 @@ bool RestInterface::InitWebServer()
|
||||
{
|
||||
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();
|
||||
|
||||
pNc = mg_bind(pMgr, WEBAPI_SERVER_PORT, this->EventHandler);
|
||||
|
||||
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();
|
||||
return false;
|
||||
}
|
||||
@ -86,7 +84,11 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
|
||||
{
|
||||
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);
|
||||
@ -109,6 +111,9 @@ void RestInterface::EventHandler(mg_connection* pNc, int ev, void* p)
|
||||
|
||||
void RestInterface::OnExit()
|
||||
{
|
||||
log->cout << "Shutting down rest api server...";
|
||||
log->Flush();
|
||||
|
||||
mg_mgr_free(pMgr);
|
||||
|
||||
return;
|
||||
|
@ -35,6 +35,6 @@ namespace Rest
|
||||
|
||||
Logging::Logger* log;
|
||||
|
||||
bool isBootedSuccessfully;
|
||||
bool isBootedSuccessfully = false;
|
||||
};
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ bool RestQueryHandler::KillYourself(const JsonBlock& request, JsonBlock& respons
|
||||
{
|
||||
XGControl::keepServerRunning = false;
|
||||
|
||||
log->cout << "Shutting down server upon rest request...";
|
||||
log->cout << "Shutting down server upon rest request by " << XGControl::lastIPThatRequested << "...";
|
||||
log->Flush();
|
||||
|
||||
responseCode = OK;
|
||||
|
@ -9,7 +9,6 @@ namespace Rest
|
||||
UNAUTHORIZED = 401,
|
||||
FORBIDDEN = 403,
|
||||
NOT_FOUND = 404,
|
||||
METHOD_NOT_ALLOWED = 405,
|
||||
INTERNAL_SERVER_ERROR = 500,
|
||||
NOT_IMPLEMENTED = 501
|
||||
};
|
||||
@ -25,7 +24,6 @@ namespace Rest
|
||||
static JasonPP::JsonBlock Unauthorized(std::string message);
|
||||
static JasonPP::JsonBlock Forbidden(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 NotImplemented(std::string message);
|
||||
|
@ -148,6 +148,7 @@
|
||||
<ClCompile Include="RestQueryHandler.cpp" />
|
||||
<ClCompile Include="RestResponseTemplates.cpp" />
|
||||
<ClCompile Include="RestInterface.cpp" />
|
||||
<ClCompile Include="UIServer.cpp" />
|
||||
<ClCompile Include="XGControl.cpp" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
@ -160,6 +161,7 @@
|
||||
<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" />
|
||||
|
@ -48,6 +48,9 @@
|
||||
<ClCompile Include="XGControl.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="UIServer.cpp">
|
||||
<Filter>Quelldateien</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="JasonPP.hpp">
|
||||
@ -80,5 +83,8 @@
|
||||
<ClInclude Include="XGControl.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="UIServer.h">
|
||||
<Filter>Headerdateien</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
91
Tubio/UIServer.cpp
Normal file
91
Tubio/UIServer.cpp
Normal 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
33
Tubio/UIServer.h
Normal 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;
|
||||
};
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
#include "XGControl.h"
|
||||
|
||||
bool XGControl::keepServerRunning = false;
|
||||
std::string XGControl::lastIPThatRequested = "";
|
||||
|
@ -1,4 +1,5 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
|
||||
/// <summary>
|
||||
/// Class to house control variables
|
||||
@ -7,5 +8,6 @@ class XGControl
|
||||
{
|
||||
public:
|
||||
static bool keepServerRunning;
|
||||
static std::string lastIPThatRequested;
|
||||
};
|
||||
|
||||
|
17
Tubio/frontend/index.html
Normal file
17
Tubio/frontend/index.html
Normal 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>
|
Loading…
x
Reference in New Issue
Block a user