mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-04-26 16:46:31 +00:00
even more cleanup, and make the tryparse work properly
This commit is contained in:
parent
7d06d012b5
commit
3578076eca
@ -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<json>(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<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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -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<json> ParseJSON(char * data);
|
||||
};
|
||||
|
||||
#endif
|
@ -263,19 +263,19 @@ namespace GeneralUtils {
|
||||
return (str.size() == 3) ? TryParse<NiPoint3>(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 <typename T>
|
||||
// [[nodiscard]] std::optional<json> TryParse(const std::span<char*> str) {
|
||||
// try {
|
||||
// return std::make_optional<json>(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 <typename T>
|
||||
[[nodiscard]] std::optional<json> TryParse(const std::string_view str) {
|
||||
try {
|
||||
return std::make_optional<json>(json::parse(str));
|
||||
} catch (const std::exception& e) {
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
std::u16string to_u16string(const T value) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user