diff --git a/include/Http.h b/include/Http.h index 35fb7b5..cdd3e34 100644 --- a/include/Http.h +++ b/include/Http.h @@ -17,6 +17,10 @@ namespace fs = std::filesystem; +class TServer; +class TNetwork; +class TResourceManager; + namespace Http { std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr); std::string POST(const std::string& host, int port, const std::string& target, const std::string& body, const std::string& ContentType, unsigned int* status = nullptr, const httplib::Headers& headers = {}); @@ -28,7 +32,8 @@ const std::string ErrorString = "-1"; namespace Server { class THttpServerInstance { public: - THttpServerInstance(); + THttpServerInstance(TServer&, TNetwork&, TResourceManager&); + ~THttpServerInstance(); static fs::path KeyFilePath; static fs::path CertFilePath; @@ -37,6 +42,9 @@ namespace Server { private: std::thread mThread; + TServer& mServer; + TNetwork& mNetwork; + TResourceManager& mResourceManager; }; } } diff --git a/src/Http.cpp b/src/Http.cpp index f89c675..12af53f 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -4,6 +4,9 @@ #include "Common.h" #include "CustomAssert.h" #include "LuaAPI.h" +#include "TNetwork.h" +#include "TResourceManager.h" +#include "TServer.h" #include "httplib.h" #include @@ -142,13 +145,19 @@ std::string Http::Status::ToString(int Code) { } } -Http::Server::THttpServerInstance::THttpServerInstance() { +#define API_V1 "/api/v1" + +Http::Server::THttpServerInstance::THttpServerInstance(TServer& Server, TNetwork& Network, TResourceManager& ResMan) + : mServer(Server) + , mNetwork(Network) + , mResourceManager(ResMan) { Application::SetSubsystemStatus("HTTPServer", Application::Status::Starting); mThread = std::thread(&Http::Server::THttpServerInstance::operator(), this); - mThread.detach(); } -#define API_V1 "/v1" +Http::Server::THttpServerInstance::~THttpServerInstance() { + mThread.detach(); +} void Http::Server::THttpServerInstance::operator()() try { beammp_infof("HTTP Server starting on [{}]:{}", Application::Settings.HTTPServerIP, Application::Settings.HTTPServerPort); @@ -159,22 +168,25 @@ void Http::Server::THttpServerInstance::operator()() try { beammp_warnf("HTTP Server is running on a possibly public IP ({}), but the server is private. The server's HTTP API supports viewing config, players, and other information, even though the server is private. (this is not an error)", Application::Settings.HTTPServerIP); } std::unique_ptr HttpLibServerInstance = std::make_unique(); - // todo: make this IP agnostic so people can set their own IP - HttpLibServerInstance->Get("/", [](const httplib::Request&, httplib::Response& res) { - res.set_content("

Hello World!

BeamMP Server can now serve HTTP requests!

", "text/html"); - }); - HttpLibServerInstance->Get(API_V1 "/players", [](const httplib::Request&, httplib::Response& res) { + HttpLibServerInstance->Get(API_V1 "/players", [this](const httplib::Request&, httplib::Response& res) { res.status = 200; json Players = json::array(); - LuaAPI::MP::Engine->Server().ForEachClient([&](std::weak_ptr ClientPtr) { - Players.push_back(ClientPtr.lock()->GetName()); + mServer.ForEachClient([&](std::weak_ptr ClientPtr) { + if (!ClientPtr.expired()) { + Players.push_back(ClientPtr.lock()->GetName()); + } return true; }); res.set_content(Players.dump(), "application/json"); }); + HttpLibServerInstance->Get(API_V1 "/version", [](const httplib::Request&, httplib::Response& res) { + res.status = 200; + res.set_content(Application::ServerVersionString(), "text/plain"); + }); + HttpLibServerInstance->Get(API_V1 "/config", [](const httplib::Request&, httplib::Response& res) { res.status = 200; res.set_content(json { diff --git a/src/main.cpp b/src/main.cpp index 76ef7ca..b96ddb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -163,8 +163,12 @@ int BeamMPServerMain(MainArguments Arguments) { PPSMonitor.SetNetwork(Network); Application::CheckForUpdates(); + std::unique_ptr HttpServer; if (Application::Settings.HTTPServerEnabled) { - Http::Server::THttpServerInstance HttpServerInstance {}; + HttpServer = std::make_unique( + Server, + Network, + ResourceManager); } Application::SetSubsystemStatus("Main", Application::Status::Good);