fix lifetime of http server, add /version

This commit is contained in:
Lion Kortlepel
2022-05-23 23:16:53 +02:00
parent 48bb1373e1
commit 5598d75fd5
3 changed files with 36 additions and 12 deletions

View File

@@ -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;
};
}
}

View File

@@ -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 <map>
@@ -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<httplib::Server> HttpLibServerInstance = std::make_unique<httplib::Server>();
// todo: make this IP agnostic so people can set their own IP
HttpLibServerInstance->Get("/", [](const httplib::Request&, httplib::Response& res) {
res.set_content("<!DOCTYPE html><article><h1>Hello World!</h1><section><p>BeamMP Server can now serve HTTP requests!</p></section></article></html>", "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<TClient> ClientPtr) {
Players.push_back(ClientPtr.lock()->GetName());
mServer.ForEachClient([&](std::weak_ptr<TClient> 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 {

View File

@@ -163,8 +163,12 @@ int BeamMPServerMain(MainArguments Arguments) {
PPSMonitor.SetNetwork(Network);
Application::CheckForUpdates();
std::unique_ptr<Http::Server::THttpServerInstance> HttpServer;
if (Application::Settings.HTTPServerEnabled) {
Http::Server::THttpServerInstance HttpServerInstance {};
HttpServer = std::make_unique<Http::Server::THttpServerInstance>(
Server,
Network,
ResourceManager);
}
Application::SetSubsystemStatus("Main", Application::Status::Good);