Make startup cleaner and don't listen by default

This commit is contained in:
Aaron Kimbre 2025-01-02 16:35:27 -06:00
parent 2d08ec641c
commit 126701b5fe
3 changed files with 11 additions and 9 deletions

View File

@ -127,10 +127,7 @@ int main(int argc, char** argv) {
bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1"; bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1";
ChatWebAPI chatwebapi; ChatWebAPI chatwebapi;
if (web_server_enabled) chatwebapi.Listen();
if (web_server_enabled) {
chatwebapi = ChatWebAPI();
}
auto lastTime = std::chrono::high_resolution_clock::now(); auto lastTime = std::chrono::high_resolution_clock::now();

View File

@ -108,17 +108,21 @@ void ChatWebAPI::HandleRequests(struct mg_connection *c, int ev, void *ev_data)
ChatWebAPI::ChatWebAPI() { ChatWebAPI::ChatWebAPI() {
if (Game::logger->GetLogDebugStatements()) mg_log_set(MG_LL_DEBUG); if (Game::logger->GetLogDebugStatements()) mg_log_set(MG_LL_DEBUG);
mg_mgr_init(&mgr); // Initialize event manager
}
ChatWebAPI::~ChatWebAPI() {
mg_mgr_free(&mgr);
}
void ChatWebAPI::Listen() {
// make listen address // make listen address
std::string listen_ip = Game::config->GetValue("web_server_listen_ip"); std::string listen_ip = Game::config->GetValue("web_server_listen_ip");
std::string listen_port = Game::config->GetValue("wed_server_listen_port"); std::string listen_port = Game::config->GetValue("wed_server_listen_port");
std::string listen_address = "http://" + listen_ip + ":" + listen_port; std::string listen_address = "http://" + listen_ip + ":" + listen_port;
LOG("Starting web server on %s", listen_address.c_str()); LOG("Starting web server on %s", listen_address.c_str());
mg_mgr_init(&mgr); // Initialise event manager
mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL); // Create HTTP listener
}
ChatWebAPI::~ChatWebAPI() { mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL); // Create HTTP listener
mg_mgr_free(&mgr);
} }
void ChatWebAPI::ReceiveRequests() { void ChatWebAPI::ReceiveRequests() {

View File

@ -12,6 +12,7 @@ public:
ChatWebAPI(); ChatWebAPI();
~ChatWebAPI(); ~ChatWebAPI();
void ReceiveRequests(); void ReceiveRequests();
void Listen();
private: private:
struct mg_mgr mgr; struct mg_mgr mgr;
static void HandleRequests(struct mg_connection *c, int ev, void *ev_data); static void HandleRequests(struct mg_connection *c, int ev, void *ev_data);