mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-01-21 12:17:01 +00:00
works
This commit is contained in:
parent
7aaa69e42d
commit
6e66c5c362
@ -10,9 +10,17 @@
|
|||||||
#include "PlayerContainer.h"
|
#include "PlayerContainer.h"
|
||||||
#include "GeneralUtils.h"
|
#include "GeneralUtils.h"
|
||||||
|
|
||||||
void ChatWebAPI::HandleRequests(struct mg_connection* connection, int request, void* request_data) {
|
typedef struct mg_connection mg_connection;
|
||||||
|
typedef struct mg_http_message mg_http_message;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
const std::string root_path = "/api/v1/";
|
||||||
|
const char* json_content_type = "Content-Type: application/json\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void HandleRequests(mg_connection* connection, int request, void* request_data) {
|
||||||
if (request == MG_EV_HTTP_MSG) {
|
if (request == MG_EV_HTTP_MSG) {
|
||||||
struct mg_http_message* http_msg = static_cast<struct mg_http_message*>(request_data);
|
const mg_http_message* const http_msg = static_cast<mg_http_message*>(request_data);
|
||||||
if (!http_msg) {
|
if (!http_msg) {
|
||||||
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid Request\"}");
|
mg_http_reply(connection, 400, json_content_type, "{\"error\":\"Invalid Request\"}");
|
||||||
return;
|
return;
|
||||||
@ -22,7 +30,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;
|
||||||
@ -133,12 +141,3 @@ void ChatWebAPI::Listen() {
|
|||||||
void ChatWebAPI::ReceiveRequests() {
|
void ChatWebAPI::ReceiveRequests() {
|
||||||
mg_mgr_poll(&mgr, 15);
|
mg_mgr_poll(&mgr, 15);
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
typedef struct mg_mgr mg_mgr;
|
||||||
|
|
||||||
class ChatWebAPI {
|
class ChatWebAPI {
|
||||||
public:
|
public:
|
||||||
ChatWebAPI();
|
ChatWebAPI();
|
||||||
@ -14,12 +16,7 @@ public:
|
|||||||
void ReceiveRequests();
|
void ReceiveRequests();
|
||||||
void Listen();
|
void Listen();
|
||||||
private:
|
private:
|
||||||
static void HandleRequests(struct mg_connection *c, int ev, void *ev_data);
|
mg_mgr mgr;
|
||||||
static std::optional<json> ParseJSON(char * data);
|
|
||||||
|
|
||||||
struct mg_mgr mgr;
|
|
||||||
inline static const std::string root_path = "/api/v1/";
|
|
||||||
inline static const char * json_content_type = "Content-Type: application/json\r\n";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -327,6 +327,21 @@ std::vector<std::string> GeneralUtils::GetSqlFileNamesFromFolder(const std::stri
|
|||||||
return sortedFiles;
|
return sortedFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "json.hpp"
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
|
template<>
|
||||||
|
[[nodiscard]] std::optional<json> GeneralUtils::TryParse(std::string_view str) {
|
||||||
|
try {
|
||||||
|
return json::parse(str);
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
return std::nullopt;
|
||||||
|
} catch (...) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#if !(__GNUC__ >= 11 || _MSC_VER >= 1924)
|
#if !(__GNUC__ >= 11 || _MSC_VER >= 1924)
|
||||||
|
|
||||||
// MacOS floating-point parse function specializations
|
// MacOS floating-point parse function specializations
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
#include "dPlatforms.h"
|
#include "dPlatforms.h"
|
||||||
#include "Game.h"
|
#include "Game.h"
|
||||||
#include "Logger.h"
|
#include "Logger.h"
|
||||||
|
#include "json_fwd.hpp"
|
||||||
|
|
||||||
enum eInventoryType : uint32_t;
|
enum eInventoryType : uint32_t;
|
||||||
enum class eObjectBits : size_t;
|
enum class eObjectBits : size_t;
|
||||||
@ -201,6 +202,10 @@ namespace GeneralUtils {
|
|||||||
return isParsed ? static_cast<T>(result) : std::optional<T>{};
|
return isParsed ? static_cast<T>(result) : std::optional<T>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
requires(!Numeric<T>)
|
||||||
|
[[nodiscard]] std::optional<T> TryParse(std::string_view str);
|
||||||
|
|
||||||
#if !(__GNUC__ >= 11 || _MSC_VER >= 1924)
|
#if !(__GNUC__ >= 11 || _MSC_VER >= 1924)
|
||||||
|
|
||||||
// MacOS floating-point parse helper function specializations
|
// MacOS floating-point parse helper function specializations
|
||||||
|
Loading…
Reference in New Issue
Block a user