From 8fce2fc67f5aeb2a5aa3b460d3b89b2e54af37b4 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Mon, 23 May 2022 19:01:03 +0200 Subject: [PATCH] http: add /players --- openapi.yml | 15 +++++++++++++++ src/Http.cpp | 20 +++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/openapi.yml b/openapi.yml index 26c9407..f568f80 100644 --- a/openapi.yml +++ b/openapi.yml @@ -13,6 +13,21 @@ servers: port: default: "8000" paths: + /players: + get: + summary: list of players on this server + responses: + "200": + description: OK + content: + "application/json": + schema: + type: array + example: + - "LionKor" + - "Titch" + items: + type: string /config: get: summary: describes server configuration diff --git a/src/Http.cpp b/src/Http.cpp index 6bfdbad..f89c675 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -151,12 +151,30 @@ Http::Server::THttpServerInstance::THttpServerInstance() { #define API_V1 "/v1" void Http::Server::THttpServerInstance::operator()() try { - beammp_info("HTTP Server started on port " + std::to_string(Application::Settings.HTTPServerPort)); + beammp_infof("HTTP Server starting on [{}]:{}", Application::Settings.HTTPServerIP, Application::Settings.HTTPServerPort); + if (Application::Settings.Private + && Application::Settings.HTTPServerIP != "::" + && Application::Settings.HTTPServerIP != "localhost" + && Application::Settings.HTTPServerIP != "127.0.0.1") { + 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) { + res.status = 200; + json Players = json::array(); + LuaAPI::MP::Engine->Server().ForEachClient([&](std::weak_ptr ClientPtr) { + Players.push_back(ClientPtr.lock()->GetName()); + return true; + }); + res.set_content(Players.dump(), + "application/json"); + }); + HttpLibServerInstance->Get(API_V1 "/config", [](const httplib::Request&, httplib::Response& res) { res.status = 200; res.set_content(json {