mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2026-02-26 04:39:48 +00:00
87 lines
2.8 KiB
C++
87 lines
2.8 KiB
C++
#include "APIRoutes.h"
|
|
#include "ServerState.h"
|
|
#include "Web.h"
|
|
#include "eHTTPMethod.h"
|
|
#include "json.hpp"
|
|
#include "Game.h"
|
|
#include "Database.h"
|
|
#include "Logger.h"
|
|
#include "HTTPContext.h"
|
|
#include "RequireAuthMiddleware.h"
|
|
#include <memory>
|
|
|
|
void RegisterAPIRoutes() {
|
|
// GET /api/status - Get overall server status
|
|
Game::web.RegisterHTTPRoute({
|
|
.path = "/api/status",
|
|
.method = eHTTPMethod::GET,
|
|
.middleware = { std::make_shared<RequireAuthMiddleware>(0) },
|
|
.handle = [](HTTPReply& reply, const HTTPContext& context) {
|
|
nlohmann::json response = ServerState::GetServerStateJson();
|
|
|
|
reply.status = eHTTPStatusCode::OK;
|
|
reply.message = response.dump();
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
}
|
|
});
|
|
|
|
// GET /api/players - Get list of online players
|
|
Game::web.RegisterHTTPRoute({
|
|
.path = "/api/players",
|
|
.method = eHTTPMethod::GET,
|
|
.middleware = { std::make_shared<RequireAuthMiddleware>(0) },
|
|
.handle = [](HTTPReply& reply, const HTTPContext& context) {
|
|
nlohmann::json response = {
|
|
{"players", nlohmann::json::array()},
|
|
{"count", 0}
|
|
};
|
|
|
|
reply.status = eHTTPStatusCode::OK;
|
|
reply.message = response.dump();
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
}
|
|
});
|
|
|
|
// GET /api/accounts/count - Get total account count
|
|
Game::web.RegisterHTTPRoute({
|
|
.path = "/api/accounts/count",
|
|
.method = eHTTPMethod::GET,
|
|
.middleware = { std::make_shared<RequireAuthMiddleware>(0) },
|
|
.handle = [](HTTPReply& reply, const HTTPContext& context) {
|
|
try {
|
|
const uint32_t count = Database::Get()->GetAccountCount();
|
|
nlohmann::json response = {{"count", count}};
|
|
reply.status = eHTTPStatusCode::OK;
|
|
reply.message = response.dump();
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
} catch (std::exception& ex) {
|
|
LOG("Error in /api/accounts/count: %s", ex.what());
|
|
reply.status = eHTTPStatusCode::INTERNAL_SERVER_ERROR;
|
|
reply.message = "{\"error\":\"Database error\"}";
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
}
|
|
}
|
|
});
|
|
|
|
// GET /api/characters/count - Get total character count
|
|
Game::web.RegisterHTTPRoute({
|
|
.path = "/api/characters/count",
|
|
.method = eHTTPMethod::GET,
|
|
.middleware = { std::make_shared<RequireAuthMiddleware>(0) },
|
|
.handle = [](HTTPReply& reply, const HTTPContext& context) {
|
|
try {
|
|
const uint32_t count = Database::Get()->GetCharacterCount();
|
|
nlohmann::json response = {{"count", count}};
|
|
reply.status = eHTTPStatusCode::OK;
|
|
reply.message = response.dump();
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
} catch (std::exception& ex) {
|
|
LOG("Error in /api/characters/count: %s", ex.what());
|
|
reply.status = eHTTPStatusCode::INTERNAL_SERVER_ERROR;
|
|
reply.message = "{\"error\":\"Database error\"}";
|
|
reply.contentType = eContentType::APPLICATION_JSON;
|
|
}
|
|
}
|
|
});
|
|
}
|