even more cleanup, and make the tryparse work properly

This commit is contained in:
Aaron Kimbre 2025-01-02 17:04:07 -06:00
parent 7d06d012b5
commit 3578076eca
3 changed files with 15 additions and 28 deletions

View File

@ -8,6 +8,7 @@
#include "dServer.h" #include "dServer.h"
#include "dConfig.h" #include "dConfig.h"
#include "PlayerContainer.h" #include "PlayerContainer.h"
#include "GeneralUtils.h"
void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) { void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) {
if (request == MG_EV_HTTP_MSG) { 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) { if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) {
// handle announcements // handle announcements
if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) { 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<json>(http_msg->body.buf);
if (!data) { if (!data) {
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}"); mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid JSON\"}");
return; return;
@ -37,7 +38,6 @@ void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, v
return; return;
} }
std::string message = data.value()["message"]; 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 // build and send the packet to all world servers
{ {
@ -133,15 +133,3 @@ void ChatWebAPI::Listen() {
void ChatWebAPI::ReceiveRequests() { void ChatWebAPI::ReceiveRequests() {
mg_mgr_poll(&mgr, 15); mg_mgr_poll(&mgr, 15);
} }
// TODO: Move to GeneralUtils
std::optional<json> ChatWebAPI::ParseJSON(char* data) {
try {
return std::make_optional<json>(json::parse(data));
} catch (const std::exception& e) {
LOG_DEBUG("Failed to parse JSON: %s", e.what());
return std::nullopt;
}
}

View File

@ -18,7 +18,6 @@ private:
static void HandleRequests(struct mg_connection *c, int ev, void *ev_data); static void HandleRequests(struct mg_connection *c, int ev, void *ev_data);
inline static const std::string root_path = "/api/v1/"; inline static const std::string root_path = "/api/v1/";
inline static const char * json_content_type = "Content-Type: application/json\r\n"; inline static const char * json_content_type = "Content-Type: application/json\r\n";
static std::optional<json> ParseJSON(char * data);
}; };
#endif #endif

View File

@ -263,19 +263,19 @@ namespace GeneralUtils {
return (str.size() == 3) ? TryParse<NiPoint3>(str[0], str[1], str[2]) : std::nullopt; return (str.size() == 3) ? TryParse<NiPoint3>(str[0], str[1], str[2]) : std::nullopt;
} }
// /** /**
// * The TryParse overload for handling json parsing * The TryParse overload for handling json parsing
// * @param str The string that is the json to parse * @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 * @returns An std::optional containing the desired json if it can be parsed from the string
// */ */
// template <typename T> template <typename T>
// [[nodiscard]] std::optional<json> TryParse(const std::span<char*> str) { [[nodiscard]] std::optional<json> TryParse(const std::string_view str) {
// try { try {
// return std::make_optional<json>(json::parse(str)); return std::make_optional<json>(json::parse(str));
// } catch (const std::exception& e) { } catch (const std::exception& e) {
// return std::nullopt; return std::nullopt;
// } }
// } }
template <typename T> template <typename T>
std::u16string to_u16string(const T value) { std::u16string to_u16string(const T value) {