HttpEndpoints: Add endpoint to list avail endpoints + data hints

pull/239/head
Peter Repukat 1 year ago
parent b359053771
commit 954d953f4b

@ -49,31 +49,38 @@ AppLauncher::AppLauncher(
} }
#endif #endif
HttpServer::AddEndpoint({"/launched-pids", HttpServer::AddEndpoint({
HttpServer::Method::GET, "/launched-pids",
[this](const httplib::Request& req, httplib::Response& res) { HttpServer::Method::GET,
const nlohmann::json j = launchedPids(); [this](const httplib::Request& req, httplib::Response& res) {
res.set_content(j.dump(), "text/json"); const nlohmann::json j = launchedPids();
}}); res.set_content(j.dump(), "text/json");
},
HttpServer::AddEndpoint({"/launched-pids", {1, 2, 3},
HttpServer::Method::POST, });
[this](const httplib::Request& req, httplib::Response& res) {
try { HttpServer::AddEndpoint({
const nlohmann::json postbody = nlohmann::json::parse(req.body); "/launched-pids",
addPids(postbody.get<std::vector<DWORD>>()); HttpServer::Method::POST,
} [this](const httplib::Request& req, httplib::Response& res) {
catch (std::exception& e) { try {
res.status = 401; const nlohmann::json postbody = nlohmann::json::parse(req.body);
res.set_content(nlohmann::json{ addPids(postbody.get<std::vector<DWORD>>());
{"code", 401}, }
{"name", "Bad Request"}, catch (std::exception& e) {
{"message", e.what()}, res.status = 401;
} res.set_content(nlohmann::json{
.dump(), {"code", 401},
"text/json"); {"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) void AppLauncher::launchApp(const std::wstring& path, const std::wstring& args)

@ -7,19 +7,23 @@ namespace CHTE {
inline void addEndpoints() inline void addEndpoints()
{ {
HttpServer::AddEndpoint( HttpServer::AddEndpoint(
{"/settings", {"/settings",
HttpServer::Method::GET, HttpServer::Method::GET,
[](const httplib::Request& req, httplib::Response& res) { [](const httplib::Request& req, httplib::Response& res) {
res.set_content(Settings::toJson().dump(), "text/json"); res.set_content(Settings::toJson().dump(), "text/json");
}}); },
"json"});
HttpServer::AddEndpoint( HttpServer::AddEndpoint(
{"/steam_settings", {"/steam_settings",
HttpServer::Method::GET, HttpServer::Method::GET,
[](const httplib::Request& req, httplib::Response& res) { [](const httplib::Request& req, httplib::Response& res) {
res.set_content(util::steam::getSteamConfig().dump(4), "text/json"); res.set_content(util::steam::getSteamConfig().dump(4), "text/json");
}}); },
"json"});
}; };
} // namespace CHTE } // namespace CHTE

@ -16,17 +16,28 @@ limitations under the License.
#include "HttpServer.h" #include "HttpServer.h"
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include <utility> #include <utility>
#include "AppLauncher.h" #include <algorithm>
#include "../common/Settings.h"
#include "../common/steam_util.h"
HttpServer::HttpServer(std::function<void()> close) : close_(std::move(close)) HttpServer::HttpServer(std::function<void()> 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) void HttpServer::AddEndpoint(const Endpoint&& e)
{ {
endpoints_.push_back(e); endpoints_.push_back(e);
@ -39,8 +50,29 @@ void HttpServer::run()
}; };
server_.Get("/", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) { server_.Get("/", [this, &setCorsHeader](const httplib::Request& req, httplib::Response& res) {
res.set_content("", "text/json");
setCorsHeader(res); 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_) { for (const auto& e : endpoints_) {
@ -86,7 +118,6 @@ void HttpServer::run()
} }
.dump(), .dump(),
"text/json"); "text/json");
return;
} }
}); });
} }

@ -17,6 +17,7 @@ limitations under the License.
#include <thread> #include <thread>
#include <httplib.h> #include <httplib.h>
#include <nlohmann/json.hpp>
class AppLauncher; class AppLauncher;
@ -25,17 +26,22 @@ class HttpServer {
public: public:
explicit HttpServer(std::function<void()> close); explicit HttpServer(std::function<void()> close);
// C++ enums suck.
enum Method { enum Method {
GET, GET,
POST, POST,
PUT, PUT,
PATCH, 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 { struct Endpoint {
std::string path; std::string path;
Method method; Method method;
std::function<void(const httplib::Request& req, httplib::Response& res)> handler; std::function<void(const httplib::Request& req, httplib::Response& res)> handler;
nlohmann::json response_hint = nullptr;
nlohmann::json payload_hint = nullptr;
}; };
static void AddEndpoint(const Endpoint&& e); static void AddEndpoint(const Endpoint&& e);

Loading…
Cancel
Save