From 3578076eca85b4a6d1bb20147b9acf5b0713e5d3 Mon Sep 17 00:00:00 2001 From: Aaron Kimbre Date: Thu, 2 Jan 2025 17:04:07 -0600 Subject: [PATCH] even more cleanup, and make the tryparse work properly --- dChatServer/ChatWebAPI.cpp | 16 ++-------------- dChatServer/ChatWebAPI.h | 1 - dCommon/GeneralUtils.h | 26 +++++++++++++------------- 3 files changed, 15 insertions(+), 28 deletions(-) diff --git a/dChatServer/ChatWebAPI.cpp b/dChatServer/ChatWebAPI.cpp index e27f4bfc..fc069dbb 100644 --- a/dChatServer/ChatWebAPI.cpp +++ b/dChatServer/ChatWebAPI.cpp @@ -8,6 +8,7 @@ #include "dServer.h" #include "dConfig.h" #include "PlayerContainer.h" +#include "GeneralUtils.h" void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) { if (request == MG_EV_HTTP_MSG) { @@ -21,7 +22,7 @@ void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, v if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) { // handle announcements if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) { - auto data = ParseJSON(http_msg->body.buf); + auto data = GeneralUtils::TryParse(http_msg->body.buf); if (!data) { mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}"); return; @@ -37,7 +38,6 @@ void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, v return; } std::string message = data.value()["message"]; - LOG_DEBUG("Announcement: %s - %s", title.c_str(), message.c_str()); // build and send the packet to all world servers { @@ -133,15 +133,3 @@ void ChatWebAPI::Listen() { void ChatWebAPI::ReceiveRequests() { mg_mgr_poll(&mgr, 15); } - -// TODO: Move to GeneralUtils -std::optional ChatWebAPI::ParseJSON(char* data) { - try { - return std::make_optional(json::parse(data)); - } catch (const std::exception& e) { - LOG_DEBUG("Failed to parse JSON: %s", e.what()); - return std::nullopt; - } -} - - diff --git a/dChatServer/ChatWebAPI.h b/dChatServer/ChatWebAPI.h index 427bea31..2b376797 100644 --- a/dChatServer/ChatWebAPI.h +++ b/dChatServer/ChatWebAPI.h @@ -18,7 +18,6 @@ private: static void HandleRequests(struct mg_connection *c, int ev, void *ev_data); inline static const std::string root_path = "/api/v1/"; inline static const char * json_content_type = "Content-Type: application/json\r\n"; - static std::optional ParseJSON(char * data); }; #endif \ No newline at end of file diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index 9c609870..5f93f7f3 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -263,19 +263,19 @@ namespace GeneralUtils { return (str.size() == 3) ? TryParse(str[0], str[1], str[2]) : std::nullopt; } - // /** - // * The TryParse overload for handling json parsing - // * @param str The string that is the json to parse - // * @returns An std::optional containing the desired json if it can be parsed from the string - // */ - // template - // [[nodiscard]] std::optional TryParse(const std::span str) { - // try { - // return std::make_optional(json::parse(str)); - // } catch (const std::exception& e) { - // return std::nullopt; - // } - // } + /** + * The TryParse overload for handling json parsing + * @param str The string that is the json to parse + * @returns An std::optional containing the desired json if it can be parsed from the string + */ + template + [[nodiscard]] std::optional TryParse(const std::string_view str) { + try { + return std::make_optional(json::parse(str)); + } catch (const std::exception& e) { + return std::nullopt; + } + } template std::u16string to_u16string(const T value) {