mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-18 06:10:10 +00:00
fix lifetime of http server, add /version
This commit is contained in:
@@ -17,6 +17,10 @@
|
|||||||
|
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
class TServer;
|
||||||
|
class TNetwork;
|
||||||
|
class TResourceManager;
|
||||||
|
|
||||||
namespace Http {
|
namespace Http {
|
||||||
std::string GET(const std::string& host, int port, const std::string& target, unsigned int* status = nullptr);
|
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 = {});
|
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 {
|
namespace Server {
|
||||||
class THttpServerInstance {
|
class THttpServerInstance {
|
||||||
public:
|
public:
|
||||||
THttpServerInstance();
|
THttpServerInstance(TServer&, TNetwork&, TResourceManager&);
|
||||||
|
~THttpServerInstance();
|
||||||
static fs::path KeyFilePath;
|
static fs::path KeyFilePath;
|
||||||
static fs::path CertFilePath;
|
static fs::path CertFilePath;
|
||||||
|
|
||||||
@@ -37,6 +42,9 @@ namespace Server {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::thread mThread;
|
std::thread mThread;
|
||||||
|
TServer& mServer;
|
||||||
|
TNetwork& mNetwork;
|
||||||
|
TResourceManager& mResourceManager;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
src/Http.cpp
32
src/Http.cpp
@@ -4,6 +4,9 @@
|
|||||||
#include "Common.h"
|
#include "Common.h"
|
||||||
#include "CustomAssert.h"
|
#include "CustomAssert.h"
|
||||||
#include "LuaAPI.h"
|
#include "LuaAPI.h"
|
||||||
|
#include "TNetwork.h"
|
||||||
|
#include "TResourceManager.h"
|
||||||
|
#include "TServer.h"
|
||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
|
|
||||||
#include <map>
|
#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);
|
Application::SetSubsystemStatus("HTTPServer", Application::Status::Starting);
|
||||||
mThread = std::thread(&Http::Server::THttpServerInstance::operator(), this);
|
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 {
|
void Http::Server::THttpServerInstance::operator()() try {
|
||||||
beammp_infof("HTTP Server starting on [{}]:{}", Application::Settings.HTTPServerIP, Application::Settings.HTTPServerPort);
|
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);
|
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>();
|
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;
|
res.status = 200;
|
||||||
json Players = json::array();
|
json Players = json::array();
|
||||||
LuaAPI::MP::Engine->Server().ForEachClient([&](std::weak_ptr<TClient> ClientPtr) {
|
mServer.ForEachClient([&](std::weak_ptr<TClient> ClientPtr) {
|
||||||
Players.push_back(ClientPtr.lock()->GetName());
|
if (!ClientPtr.expired()) {
|
||||||
|
Players.push_back(ClientPtr.lock()->GetName());
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
res.set_content(Players.dump(),
|
res.set_content(Players.dump(),
|
||||||
"application/json");
|
"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) {
|
HttpLibServerInstance->Get(API_V1 "/config", [](const httplib::Request&, httplib::Response& res) {
|
||||||
res.status = 200;
|
res.status = 200;
|
||||||
res.set_content(json {
|
res.set_content(json {
|
||||||
|
|||||||
@@ -163,8 +163,12 @@ int BeamMPServerMain(MainArguments Arguments) {
|
|||||||
PPSMonitor.SetNetwork(Network);
|
PPSMonitor.SetNetwork(Network);
|
||||||
Application::CheckForUpdates();
|
Application::CheckForUpdates();
|
||||||
|
|
||||||
|
std::unique_ptr<Http::Server::THttpServerInstance> HttpServer;
|
||||||
if (Application::Settings.HTTPServerEnabled) {
|
if (Application::Settings.HTTPServerEnabled) {
|
||||||
Http::Server::THttpServerInstance HttpServerInstance {};
|
HttpServer = std::make_unique<Http::Server::THttpServerInstance>(
|
||||||
|
Server,
|
||||||
|
Network,
|
||||||
|
ResourceManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
Application::SetSubsystemStatus("Main", Application::Status::Good);
|
Application::SetSubsystemStatus("Main", Application::Status::Good);
|
||||||
|
|||||||
Reference in New Issue
Block a user