diff --git a/dChatServer/CMakeLists.txt b/dChatServer/CMakeLists.txt index 853345dd..3dc64c8a 100644 --- a/dChatServer/CMakeLists.txt +++ b/dChatServer/CMakeLists.txt @@ -1,5 +1,5 @@ set(DCHATSERVER_SOURCES - "ChatHttpApi.cpp" + "ChatWebApi.cpp" "ChatIgnoreList.cpp" "ChatPacketHandler.cpp" "PlayerContainer.cpp" diff --git a/dChatServer/ChatServer.cpp b/dChatServer/ChatServer.cpp index 7b72e8ec..41c9b1b0 100644 --- a/dChatServer/ChatServer.cpp +++ b/dChatServer/ChatServer.cpp @@ -20,7 +20,7 @@ #include "eWorldMessageType.h" #include "ChatIgnoreList.h" #include "StringifiedEnum.h" -#include "ChatHttpApi.h" +#include "ChatWebApi.h" #include "Game.h" #include "Server.h" @@ -37,7 +37,7 @@ namespace Game { AssetManager* assetManager = nullptr; Game::signal_t lastSignal = 0; std::mt19937 randomEngine; - PlayerContainer playerContainer; + PlayerContainer playerContainer; } void HandlePacket(Packet* packet); @@ -123,7 +123,8 @@ int main(int argc, char** argv) { uint32_t framesSinceMasterDisconnect = 0; uint32_t framesSinceLastSQLPing = 0; - std::thread APIThread(ChatHttpApi::Listen, ourPort); + // start the web api thread + std::thread webAPIThread(ChatWebApi::Listen, ourPort); Game::logger->Flush(); // once immediately before main loop while (!Game::ShouldShutdown()) { @@ -177,8 +178,11 @@ int main(int argc, char** argv) { delete Game::server; delete Game::logger; delete Game::config; - ChatHttpApi::Stop(); - APIThread.join(); + + // rejoin the web api thread + ChatWebApi::Stop(); + webAPIThread.join(); + return EXIT_SUCCESS; } diff --git a/dChatServer/ChatHttpApi.cpp b/dChatServer/ChatWebApi.cpp similarity index 82% rename from dChatServer/ChatHttpApi.cpp rename to dChatServer/ChatWebApi.cpp index 8d652a32..c9cebc6a 100644 --- a/dChatServer/ChatHttpApi.cpp +++ b/dChatServer/ChatWebApi.cpp @@ -1,13 +1,14 @@ #include #include -#include "ChatHttpApi.h" +#include "ChatWebApi.h" #include "dCommonVars.h" #include "eConnectionType.h" #include "eChatMessageType.h" #include "httplib.h" #include "dServer.h" #include "PlayerContainer.h" +#include "dConfig.h" using json = nlohmann::json; @@ -15,7 +16,13 @@ namespace { httplib::Server m_APIServer; } -void ChatHttpApi::Listen(const uint32_t port) { +void ChatWebApi::Listen(const uint32_t port) { + if (Game::config->GetValue("enable_chat_web_api") != "1") { + LOG("Chat Web API is disabled"); + return; + } + LOG("Chat Web API is enabled, starting web server..."); + m_APIServer.Post("/announce", [](const httplib::Request& req, httplib::Response& res) { const json data = json::parse(req.body); if (!data.contains("title")) { @@ -75,9 +82,12 @@ void ChatHttpApi::Listen(const uint32_t port) { if (data.empty()) res.status = 204; }); - m_APIServer.listen("0.0.0.0", port); + m_APIServer.listen(Game::config->GetValue("enable_chat_web_api").c_str(), port); }; -void ChatHttpApi::Stop(){ - m_APIServer.stop(); +void ChatWebApi::Stop(){ + if (Game::config->GetValue("enable_chat_web_api") == "1") { + LOG("Stopping Chat Web API server..."); + m_APIServer.stop(); + } } diff --git a/dChatServer/ChatHttpApi.h b/dChatServer/ChatWebApi.h similarity index 75% rename from dChatServer/ChatHttpApi.h rename to dChatServer/ChatWebApi.h index aafb29de..420a4087 100644 --- a/dChatServer/ChatHttpApi.h +++ b/dChatServer/ChatWebApi.h @@ -1,6 +1,6 @@ #include "httplib.h" -namespace ChatHttpApi { +namespace ChatWebApi { void Listen(const uint32_t port); void Stop(); }; diff --git a/resources/chatconfig.ini b/resources/chatconfig.ini index 402534ed..ee990089 100644 --- a/resources/chatconfig.ini +++ b/resources/chatconfig.ini @@ -6,3 +6,10 @@ max_number_of_best_friends=5 # Change the value below to what you would like this to be (50 is live accurate) # going over 50 will be allowed in some secnarios, but proper handling will require client modding max_number_of_friends=50 + +# Enable or disable the chat web API, disabled by default +# It will run on the same port the chat server is running on, defined in shardconfig.ini +enable_chat_web_api=0 + +# If that chat web api is enabled, it will only listen for connections on this ip address +chat_web_api_listen_address=localhost