diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 2323867b..0a5cc8b7 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -127,11 +127,8 @@ int main(int argc, char** argv) { bool web_server_enabled = Game::config->GetValue("web_server_enabled") == "1"; ChatWebAPI chatwebapi; + if (web_server_enabled) chatwebapi.Listen(); - if (web_server_enabled) { - chatwebapi = ChatWebAPI(); - } - auto lastTime = std::chrono::high_resolution_clock::now(); Game::logger->Flush(); // once immediately before main loop diff --git a/dChatServer/ChatWebAPI.cpp b/dChatServer/ChatWebAPI.cpp index c8172872..d43c51eb 100644 --- a/dChatServer/ChatWebAPI.cpp +++ b/dChatServer/ChatWebAPI.cpp @@ -108,17 +108,21 @@ void ChatWebAPI::HandleRequests(struct mg_connection *c, int ev, void *ev_data) ChatWebAPI::ChatWebAPI() { 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 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_address = "http://" + listen_ip + ":" + listen_port; 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_mgr_free(&mgr); + mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL); // Create HTTP listener } void ChatWebAPI::ReceiveRequests() { diff --git a/dChatServer/ChatWebAPI.h b/dChatServer/ChatWebAPI.h index 4225d331..427bea31 100644 --- a/dChatServer/ChatWebAPI.h +++ b/dChatServer/ChatWebAPI.h @@ -12,6 +12,7 @@ public: ChatWebAPI(); ~ChatWebAPI(); void ReceiveRequests(); + void Listen(); private: struct mg_mgr mgr; static void HandleRequests(struct mg_connection *c, int ev, void *ev_data);