Add GitHub ping handling

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-10-14 22:20:32 +01:00
parent d5a2353ded
commit 549444fd47
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 29 additions and 0 deletions

View File

@ -10,6 +10,8 @@ class Routes {
private:
crow::SimpleApp app;
static bool check_ping(const crow::request &req);
};
#endif

View File

@ -11,6 +11,8 @@ Routes::Routes(nlohmann::json config) {
const nlohmann::json config_run_actions = config["run-actions"];
const nlohmann::json config_tokens = config["tokens"];
Logger::info("[Routes] Creating server");
Logger::info("[Routes] Registering route \"/\"");
CROW_ROUTE(this->app, "/")
.methods("POST"_method, "GET"_method)
@ -28,6 +30,13 @@ Routes::Routes(nlohmann::json config) {
.methods("POST"_method)
.name("Run Actions")
([&config_run_actions](const crow::request &req) {
if (check_ping(req)) {
nlohmann::json response = {
{"status", 200},
{"message", "Pong"}
};
return crow::response(200, response.dump());
}
if (config_run_actions.is_null()) {
Logger::warn("[Routes] No run-actions configuration found");
nlohmann::json response = {
@ -53,6 +62,13 @@ Routes::Routes(nlohmann::json config) {
.methods("POST"_method)
.name("Update Files")
([&config_update_files, &config_tokens](const crow::request &req) {
if (check_ping(req)) {
nlohmann::json response = {
{"status", 200},
{"message", "Pong"}
};
return crow::response(200, response.dump());
}
if (config_update_files.is_null()) {
Logger::warn("[Routes] No update-files configuration found");
nlohmann::json response = {
@ -92,3 +108,14 @@ Routes::Routes(nlohmann::json config) {
}
Logger::info("[Routes] Server stopped");
}
bool Routes::check_ping(const crow::request &req) {
if (req.headers.find("X-GitHub-Event") == req.headers.end()) {
Logger::warn("[Routes] No X-GitHub-Event header found");
return false;
}
if (req.headers.find("X-GitHub-Event")->second == "ping") {
return true;
}
return false;
}