2025-01-02 06:45:53 +00:00
|
|
|
#include "ChatWebAPI.h"
|
2025-01-02 22:11:45 +00:00
|
|
|
|
2025-01-02 06:45:53 +00:00
|
|
|
#include "Logger.h"
|
|
|
|
#include "Game.h"
|
|
|
|
#include "json.hpp"
|
2025-01-02 22:11:45 +00:00
|
|
|
#include "dCommonVars.h"
|
|
|
|
#include "MessageType/Chat.h"
|
|
|
|
#include "dServer.h"
|
|
|
|
#include "dConfig.h"
|
|
|
|
#include "PlayerContainer.h"
|
2025-01-02 23:04:07 +00:00
|
|
|
#include "GeneralUtils.h"
|
2025-01-02 22:11:45 +00:00
|
|
|
|
2025-01-03 04:28:03 +00:00
|
|
|
#include "json.hpp"
|
|
|
|
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2025-01-03 02:42:50 +00:00
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2025-01-03 04:14:07 +00:00
|
|
|
struct HttpReply {
|
|
|
|
uint32_t status = 404;
|
|
|
|
std::string message = "{\"error\":\"Not Found\"}";
|
|
|
|
};
|
|
|
|
|
2025-01-03 02:42:50 +00:00
|
|
|
void HandleRequests(mg_connection* connection, int request, void* request_data) {
|
2025-01-02 22:48:31 +00:00
|
|
|
if (request == MG_EV_HTTP_MSG) {
|
2025-01-03 04:14:07 +00:00
|
|
|
HttpReply reply;
|
2025-01-03 02:42:50 +00:00
|
|
|
const mg_http_message* const http_msg = static_cast<mg_http_message*>(request_data);
|
2025-01-02 22:48:31 +00:00
|
|
|
if (!http_msg) {
|
2025-01-03 04:14:07 +00:00
|
|
|
reply.status = 400;
|
|
|
|
reply.message = "{\"error\":\"Invalid Request\"}";
|
|
|
|
} else {
|
|
|
|
// Handle Post requests
|
|
|
|
if (mg_strcmp(http_msg->method, mg_str("POST")) == 0) {
|
2025-01-03 02:42:50 +00:00
|
|
|
auto data = GeneralUtils::TryParse<json>(http_msg->body.buf);
|
2025-01-02 22:11:45 +00:00
|
|
|
if (!data) {
|
2025-01-03 04:14:07 +00:00
|
|
|
reply.status = 400;
|
|
|
|
reply.message = "{\"error\":\"Invalid JSON\"}";
|
|
|
|
} else if (mg_match(http_msg->uri, mg_str((root_path + "announce").c_str()), NULL)) {
|
|
|
|
auto& jsonBuffer = data.value();
|
|
|
|
// handle announcements
|
|
|
|
|
|
|
|
if (!jsonBuffer.contains("title")) {
|
|
|
|
reply.status = 400;
|
|
|
|
reply.message = "{\"error\":\"Missing paramater: title\"}";
|
|
|
|
} else if (!jsonBuffer.contains("message")) {
|
|
|
|
reply.status = 400;
|
|
|
|
reply.message = "{\"error\":\"Missing paramater: message\"}";
|
|
|
|
} else {
|
|
|
|
std::string title = jsonBuffer["title"];
|
|
|
|
std::string message = jsonBuffer["message"];
|
|
|
|
|
|
|
|
// build and send the packet to all world servers
|
|
|
|
CBITSTREAM;
|
|
|
|
BitStreamUtils::WriteHeader(bitStream, eConnectionType::CHAT, MessageType::Chat::GM_ANNOUNCE);
|
|
|
|
bitStream.Write<uint32_t>(title.size());
|
|
|
|
bitStream.Write(title);
|
|
|
|
bitStream.Write<uint32_t>(message.size());
|
|
|
|
bitStream.Write(message);
|
|
|
|
SEND_PACKET_BROADCAST;
|
|
|
|
|
|
|
|
reply.status = 200;
|
|
|
|
reply.message = "{\"status\":\"Announcement Sent\"}";
|
2025-01-02 22:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-03 04:14:07 +00:00
|
|
|
// Handle GET Requests
|
|
|
|
} else if (mg_strcmp(http_msg->method, mg_str("GET")) == 0) {
|
|
|
|
// Get All Online players
|
|
|
|
if (mg_match(http_msg->uri, mg_str((root_path + "players").c_str()), NULL)) {
|
|
|
|
const json data = Game::playerContainer;
|
|
|
|
|
2025-01-03 05:11:06 +00:00
|
|
|
reply.status = data.empty() ? 204 : 200;
|
2025-01-03 04:14:07 +00:00
|
|
|
reply.message = data.empty() ? "{\"error\":\"No Players Online\"}" : data.dump();
|
|
|
|
} else if (mg_match(http_msg->uri, mg_str((root_path + "teams").c_str()), NULL)) {
|
|
|
|
// Get Teams
|
2025-01-06 00:31:53 +00:00
|
|
|
const json data = Game::playerContainer.GetTeamContainer();
|
2025-01-03 04:14:07 +00:00
|
|
|
|
2025-01-03 05:11:06 +00:00
|
|
|
reply.status = data.empty() ? 204 : 200;
|
2025-01-03 04:14:07 +00:00
|
|
|
reply.message = data.empty() ? "{\"error\":\"No Teams Online\"}" : data.dump();
|
2025-01-02 22:11:45 +00:00
|
|
|
}
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-02 22:48:31 +00:00
|
|
|
|
2025-01-03 04:14:07 +00:00
|
|
|
LOG_DEBUG("Replying with status %d: %s", reply.status, reply.message.c_str());
|
|
|
|
mg_http_reply(connection, reply.status, json_content_type, reply.message.c_str());
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
ChatWebAPI::ChatWebAPI() {
|
2025-01-03 04:14:07 +00:00
|
|
|
mg_log_set(MG_LL_NONE);
|
2025-01-02 22:35:27 +00:00
|
|
|
mg_mgr_init(&mgr); // Initialize event manager
|
|
|
|
}
|
|
|
|
|
|
|
|
ChatWebAPI::~ChatWebAPI() {
|
|
|
|
mg_mgr_free(&mgr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatWebAPI::Listen() {
|
2025-01-02 22:11:45 +00:00
|
|
|
// make listen address
|
2025-01-03 04:14:07 +00:00
|
|
|
std::string listen_ip = Game::config->GetValue("web_server_listen_ip");
|
|
|
|
if (listen_ip == "localhost") listen_ip = "127.0.0.1";
|
|
|
|
|
|
|
|
const std::string& listen_port = Game::config->GetValue("web_server_listen_port");
|
2025-01-03 02:03:44 +00:00
|
|
|
const std::string& listen_address = "http://" + listen_ip + ":" + listen_port;
|
2025-01-02 22:28:34 +00:00
|
|
|
LOG("Starting web server on %s", listen_address.c_str());
|
2025-01-02 06:45:53 +00:00
|
|
|
|
2025-01-03 04:14:07 +00:00
|
|
|
// Create HTTP listener
|
|
|
|
if (!mg_http_listen(&mgr, listen_address.c_str(), HandleRequests, NULL)) {
|
|
|
|
LOG("Failed to create web server listener");
|
|
|
|
}
|
2025-01-02 06:45:53 +00:00
|
|
|
}
|
|
|
|
|
2025-01-02 22:11:45 +00:00
|
|
|
void ChatWebAPI::ReceiveRequests() {
|
|
|
|
mg_mgr_poll(&mgr, 15);
|
|
|
|
}
|