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
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<std::vector<DWORD>>());
}
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<std::vector<DWORD>>());
}
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)

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

@ -16,17 +16,28 @@ limitations under the License.
#include "HttpServer.h"
#include <spdlog/spdlog.h>
#include <nlohmann/json.hpp>
#include <utility>
#include "AppLauncher.h"
#include "../common/Settings.h"
#include "../common/steam_util.h"
#include <algorithm>
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)
{
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;
}
});
}

@ -17,6 +17,7 @@ limitations under the License.
#include <thread>
#include <httplib.h>
#include <nlohmann/json.hpp>
class AppLauncher;
@ -25,17 +26,22 @@ class HttpServer {
public:
explicit HttpServer(std::function<void()> 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<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);

Loading…
Cancel
Save