don't do things if the web server isn't enabled

This commit is contained in:
Aaron Kimbre 2025-01-31 23:02:14 -06:00
parent 5839a888bb
commit 394fcc050c
3 changed files with 12 additions and 11 deletions

View File

@ -93,19 +93,17 @@ int main(int argc, char** argv) {
} }
// setup the chat api web server // setup the chat api web server
bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1";
const uint32_t web_server_port = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("web_server_port")).value_or(2005); const uint32_t web_server_port = GeneralUtils::TryParse<uint32_t>(Game::config->GetValue("web_server_port")).value_or(2005);
if (web_server_enabled && !Game::web.Startup("localhost", web_server_port)) { if (Game::config->GetValue("web_server_enabled") == "1" && !Game::web.Startup("localhost", web_server_port)) {
// if we want the web api and it fails to start, exit // if we want the web server and it fails to start, exit
LOG("Failed to start web server, shutting down."); LOG("Failed to start web server, shutting down.");
Database::Destroy("ChatServer"); Database::Destroy("ChatServer");
delete Game::logger; delete Game::logger;
delete Game::config; delete Game::config;
return EXIT_FAILURE; return EXIT_FAILURE;
} }
if (web_server_enabled) {
ChatWeb::RegisterRoutes(); if (Game::web.IsEnabled()) ChatWeb::RegisterRoutes();
}
//Find out the master's IP: //Find out the master's IP:
std::string masterIP; std::string masterIP;
@ -169,10 +167,8 @@ int main(int argc, char** argv) {
packet = nullptr; packet = nullptr;
} }
//Check and handle web requests: // Check and handle web requests:
if (web_server_enabled) { if (Game::web.IsEnabled()) Game::web.ReceiveRequests();
Game::web.ReceiveRequests();
}
//Push our log every 30s: //Push our log every 30s:
if (framesSinceLastFlush >= logFlushTime) { if (framesSinceLastFlush >= logFlushTime) {

View File

@ -156,6 +156,7 @@ void HandleWSGetSubscriptions(mg_connection* connection, json data) {
} }
void HandleMessages(mg_connection* connection, int message, void* message_data) { void HandleMessages(mg_connection* connection, int message, void* message_data) {
if (!Game::web.IsEnabled()) return;
switch (message) { switch (message) {
case MG_EV_HTTP_MSG: case MG_EV_HTTP_MSG:
HandleHTTPMessage(connection, static_cast<mg_http_message*>(message_data)); HandleHTTPMessage(connection, static_cast<mg_http_message*>(message_data));
@ -232,7 +233,7 @@ bool Web::Startup(const std::string& listen_ip, const uint32_t listen_port) {
.name = "getSubscriptions", .name = "getSubscriptions",
.handle = HandleWSGetSubscriptions .handle = HandleWSGetSubscriptions
}); });
enabled = true;
return true; return true;
} }
@ -241,6 +242,8 @@ void Web::ReceiveRequests() {
} }
void Web::SendWSMessage(const std::string subscription, json& data) { void Web::SendWSMessage(const std::string subscription, json& data) {
if (!Game::web.enabled) return;
// find subscription // find subscription
auto subItr = std::find(g_WSSubscriptions.begin(), g_WSSubscriptions.end(), subscription); auto subItr = std::find(g_WSSubscriptions.begin(), g_WSSubscriptions.end(), subscription);
if (subItr == g_WSSubscriptions.end()) { if (subItr == g_WSSubscriptions.end()) {

View File

@ -49,8 +49,10 @@ public:
void RegisterHTTPRoute(HTTPRoute route); void RegisterHTTPRoute(HTTPRoute route);
void RegisterWSEvent(WSEvent event); void RegisterWSEvent(WSEvent event);
void RegisterWSSubscription(const std::string& subscription); void RegisterWSSubscription(const std::string& subscription);
bool IsEnabled() const { return enabled; };
private: private:
mg_mgr mgr; mg_mgr mgr;
bool enabled = false;
}; };
#endif // !__WEB_H__ #endif // !__WEB_H__