From 954d953f4bf48ae32774e35f41d2e65d964d07b4 Mon Sep 17 00:00:00 2001 From: Peter Repukat Date: Tue, 28 Feb 2023 16:35:23 +0100 Subject: [PATCH] HttpEndpoints: Add endpoint to list avail endpoints + data hints --- GlosSITarget/AppLauncher.cpp | 57 +++++++++++++++++------------- GlosSITarget/CommonHttpEndpoints.h | 8 +++-- GlosSITarget/HttpServer.cpp | 43 ++++++++++++++++++---- GlosSITarget/HttpServer.h | 6 ++++ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/GlosSITarget/AppLauncher.cpp b/GlosSITarget/AppLauncher.cpp index 971ff0c..9e5ac29 100644 --- a/GlosSITarget/AppLauncher.cpp +++ b/GlosSITarget/AppLauncher.cpp @@ -49,31 +49,38 @@ AppLauncher::AppLauncher( } #endif - HttpServer::AddEndpoint({"/launched-pids", - HttpServer::Method::GET, - [this](const httplib::Request& req, httplib::Response& res) { - const nlohmann::json j = launchedPids(); - res.set_content(j.dump(), "text/json"); - }}); - - HttpServer::AddEndpoint({"/launched-pids", - HttpServer::Method::POST, - [this](const httplib::Request& req, httplib::Response& res) { - try { - const nlohmann::json postbody = nlohmann::json::parse(req.body); - addPids(postbody.get>()); - } - catch (std::exception& e) { - res.status = 401; - res.set_content(nlohmann::json{ - {"code", 401}, - {"name", "Bad Request"}, - {"message", e.what()}, - } - .dump(), - "text/json"); - } - }}); + HttpServer::AddEndpoint({ + "/launched-pids", + HttpServer::Method::GET, + [this](const httplib::Request& req, httplib::Response& res) { + const nlohmann::json j = launchedPids(); + res.set_content(j.dump(), "text/json"); + }, + {1, 2, 3}, + }); + + HttpServer::AddEndpoint({ + "/launched-pids", + HttpServer::Method::POST, + [this](const httplib::Request& req, httplib::Response& res) { + try { + const nlohmann::json postbody = nlohmann::json::parse(req.body); + addPids(postbody.get>()); + } + catch (std::exception& e) { + res.status = 401; + res.set_content(nlohmann::json{ + {"code", 401}, + {"name", "Bad Request"}, + {"message", e.what()}, + } + .dump(), + "text/json"); + } + }, + {1, 2, 3, 4}, + {2, 3, 4}, + }); }; void AppLauncher::launchApp(const std::wstring& path, const std::wstring& args) diff --git a/GlosSITarget/CommonHttpEndpoints.h b/GlosSITarget/CommonHttpEndpoints.h index bd1c2a4..98c7098 100644 --- a/GlosSITarget/CommonHttpEndpoints.h +++ b/GlosSITarget/CommonHttpEndpoints.h @@ -7,19 +7,23 @@ namespace CHTE { inline void addEndpoints() { + HttpServer::AddEndpoint( {"/settings", HttpServer::Method::GET, [](const httplib::Request& req, httplib::Response& res) { res.set_content(Settings::toJson().dump(), "text/json"); - }}); + }, + "json"}); HttpServer::AddEndpoint( {"/steam_settings", HttpServer::Method::GET, [](const httplib::Request& req, httplib::Response& res) { res.set_content(util::steam::getSteamConfig().dump(4), "text/json"); - }}); + }, + "json"}); + }; } // namespace CHTE diff --git a/GlosSITarget/HttpServer.cpp b/GlosSITarget/HttpServer.cpp index 768b36f..d2b4fe1 100644 --- a/GlosSITarget/HttpServer.cpp +++ b/GlosSITarget/HttpServer.cpp @@ -16,17 +16,28 @@ limitations under the License. #include "HttpServer.h" #include -#include #include -#include "AppLauncher.h" -#include "../common/Settings.h" -#include "../common/steam_util.h" +#include HttpServer::HttpServer(std::function close) : close_(std::move(close)) { } +std::string HttpServer::ToString(Method m) +{ + switch (m) { + case POST: + return "POST"; + case PATCH: + return "PATCH"; + case PUT: + return "PUT"; + default: + return "GET"; + } +} + void HttpServer::AddEndpoint(const Endpoint&& e) { endpoints_.push_back(e); @@ -39,8 +50,29 @@ void HttpServer::run() }; server_.Get("/", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) { - res.set_content("", "text/json"); setCorsHeader(res); + + auto content_json = nlohmann::json{ + {"endpoints", nlohmann::json::array()}}; + + for (const auto& e : endpoints_) { + content_json["endpoints"].push_back( + nlohmann::json{ + {"path", e.path}, + {"method", ToString(e.method)}, + {"response", e.response_hint}, + {"payload", e.payload_hint}, + }); + } + + content_json["endpoints"].push_back( + nlohmann::json{ + {"path", "/quit"}, + {"method", "POST"} + }); + + res.set_content(content_json.dump(4), + "text/json"); }); for (const auto& e : endpoints_) { @@ -86,7 +118,6 @@ void HttpServer::run() } .dump(), "text/json"); - return; } }); } diff --git a/GlosSITarget/HttpServer.h b/GlosSITarget/HttpServer.h index 52fdd77..1fe27e3 100644 --- a/GlosSITarget/HttpServer.h +++ b/GlosSITarget/HttpServer.h @@ -17,6 +17,7 @@ limitations under the License. #include #include +#include class AppLauncher; @@ -25,17 +26,22 @@ class HttpServer { public: explicit HttpServer(std::function close); + // C++ enums suck. enum Method { GET, POST, PUT, PATCH, }; + // but im not in the mood of adding yet another dependency for just that shit here. + static std::string ToString(Method m); struct Endpoint { std::string path; Method method; std::function handler; + nlohmann::json response_hint = nullptr; + nlohmann::json payload_hint = nullptr; }; static void AddEndpoint(const Endpoint&& e);