Files
DarkflameServer/dWeb/Web.h
Aaron Kimbrell 5453d163a3 WIP
2025-10-06 19:15:55 -05:00

109 lines
3.6 KiB
C++

#ifndef __WEB_H__
#define __WEB_H__
#include <functional>
#include <string>
#include <optional>
#include "mongoose.h"
#include "json_fwd.hpp"
#include "eHTTPStatusCode.h"
// Forward declarations for game namespace
// so that we can access the data anywhere
class Web;
namespace Game {
extern Web web;
}
enum class eHTTPMethod;
// Forward declaration for mongoose manager
typedef struct mg_mgr mg_mgr;
namespace ContentType {
const std::string JSON = "Content-Type: application/json\r\n";
const std::string HTML = "Content-Type: text/html\r\n";
const std::string PLAIN = "Content-Type: text/plain\r\n";
const std::string CSS = "Content-Type: text/css\r\n";
const std::string JAVASCRIPT = "Content-Type: application/javascript\r\n";
const std::string ICO = "Content-Type: image/x-icon\r\n";
const std::string PNG = "Content-Type: image/png\r\n";
const std::string SVG = "Content-Type: image/svg+xml\r\n";
const std::string JPG = "Content-Type: image/jpeg\r\n";
const std::string GIF = "Content-Type: image/gif\r\n";
const std::string WEBP = "Content-Type: image/webp\r\n";
const std::string MP4 = "Content-Type: video/mp4\r\n";
const std::string OGG = "Content-Type: audio/ogg\r\n";
const std::string MP3 = "Content-Type: audio/mpeg\r\n";
const std::string BINARY = "Content-Type: application/octet-stream\r\n";
const std::string FORM = "Content-Type: application/x-www-form-urlencoded\r\n";
const std::string MULTIPART = "Content-Type: multipart/form-data\r\n";
const std::string ZIP = "Content-Type: application/zip\r\n";
const std::string PDF = "Content-Type: application/pdf\r\n";
const std::string XML = "Content-Type: application/xml\r\n";
const std::string CSV = "Content-Type: text/csv\r\n";
const std::string YAML = "Content-Type: application/x-yaml\r\n";
}
// For passing HTTP messages between functions
struct HTTPReply {
eHTTPStatusCode status = eHTTPStatusCode::NOT_FOUND;
std::string message = "{\"error\":\"Not Found\"}";
std::string contentType = ContentType::JSON;
};
// HTTP route structure
// This structure is used to register HTTP routes
// with the server. Each route has a path, method, and a handler function
// that will be called when the route is matched.
struct HTTPRoute {
std::string path;
eHTTPMethod method;
std::function<void(HTTPReply&, const std::string&)> handle;
};
// WebSocket event structure
// This structure is used to register WebSocket events
// with the server. Each event has a name and a handler function
// that will be called when the event is triggered.
struct WSEvent {
std::string name;
std::function<void(mg_connection*, nlohmann::json)> handle;
};
// Subscription status for WebSocket clients
enum SubscriptionStatus {
UNSUBSCRIBED = 0,
SUBSCRIBED = 1
};
class Web {
public:
// Constructor
Web();
// Destructor
~Web();
// Handle incoming messages
void ReceiveRequests();
// Start the web server
// Returns true if the server started successfully
bool Startup(const std::string& listen_ip, const uint32_t listen_port);
// Register HTTP route to be handled by the server
void RegisterHTTPRoute(HTTPRoute route);
// Register WebSocket event to be handled by the server
void RegisterWSEvent(WSEvent event);
// Register WebSocket subscription to be handled by the server
void RegisterWSSubscription(const std::string& subscription);
// Returns if the web server is enabled
bool IsEnabled() const { return enabled; };
// Send a message to all connected WebSocket clients that are subscribed to the given topic
void static SendWSMessage(std::string sub, nlohmann::json& message);
private:
// mongoose manager
mg_mgr mgr;
// If the web server is enabled
bool enabled = false;
};
#endif // !__WEB_H__