Hotfix for aarch64
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
d0135da4fd
commit
cd769fa8bc
|
@ -164,6 +164,8 @@ The configuration file must contain the `run-actions` field, which is an object
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Note: if you don't want to use the `args` field, just leave an empty array such as `"args": []`.
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](LICENSE) file for details.
|
This project is licensed under the GNU General Public License v3.0 - see the [LICENSE](LICENSE) file for details.
|
||||||
|
|
|
@ -5,6 +5,6 @@
|
||||||
#include <crow/http_response.h>
|
#include <crow/http_response.h>
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
crow::response run_actions(const nlohmann::json &, const nlohmann::json &,const crow::request &);
|
crow::response run_actions(const nlohmann::json &, const crow::request &);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -9,7 +9,7 @@ void Config::create_config(std::string config_file_path) {
|
||||||
std::cout << "Creating config file" << std::endl;
|
std::cout << "Creating config file" << std::endl;
|
||||||
nlohmann::json config = {
|
nlohmann::json config = {
|
||||||
{"port", 65001},
|
{"port", 65001},
|
||||||
{"tokens", nlohmann::json::array()},
|
{"tokens", {}},
|
||||||
};
|
};
|
||||||
std::string path_to_config = config_file_path.substr(0, config_file_path.find_last_of('/'));
|
std::string path_to_config = config_file_path.substr(0, config_file_path.find_last_of('/'));
|
||||||
if (!std::filesystem::exists(path_to_config)) {
|
if (!std::filesystem::exists(path_to_config)) {
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
|
|
||||||
crow::response run_actions(const nlohmann::json &run_actions, const nlohmann::json &tokens, const crow::request &req) {
|
crow::response run_actions(const nlohmann::json &run_actions, const crow::request &req) {
|
||||||
nlohmann::json payload;
|
nlohmann::json payload;
|
||||||
try {
|
try {
|
||||||
payload = nlohmann::json::parse(req.body);
|
payload = nlohmann::json::parse(req.body);
|
||||||
|
|
|
@ -117,10 +117,14 @@ void Logger::log(std::string message, std::string level) {
|
||||||
formatted_message += "(" + std::string(time_buffer) + ") ";
|
formatted_message += "(" + std::string(time_buffer) + ") ";
|
||||||
|
|
||||||
if (level == "CODE") {
|
if (level == "CODE") {
|
||||||
|
#if defined(__aarch64__)
|
||||||
|
formatted_message += "\n" + message + "\n";
|
||||||
|
#else
|
||||||
struct winsize w;
|
struct winsize w;
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
int term_width = w.ws_col > 250 ? 250 : w.ws_col - 1;
|
int term_width = w.ws_col > 250 ? 250 : w.ws_col - 1;
|
||||||
formatted_message += "\n" + std::string(term_width, '=') + "\n" + message + "\n" + std::string(term_width, '=');
|
formatted_message += "\n" + std::string(term_width, '=') + "\n" + message + "\n" + std::string(term_width, '=');
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
formatted_message += "[" + level + "] " + message;
|
formatted_message += "[" + level + "] " + message;
|
||||||
}
|
}
|
||||||
|
@ -129,10 +133,14 @@ void Logger::log(std::string message, std::string level) {
|
||||||
}
|
}
|
||||||
std::cout << formatted_message << std::endl;
|
std::cout << formatted_message << std::endl;
|
||||||
if (level == "CODE") {
|
if (level == "CODE") {
|
||||||
|
#if defined(__aarch64__)
|
||||||
|
Logger::log_file << std::endl << message << std::endl;
|
||||||
|
#else
|
||||||
struct winsize w;
|
struct winsize w;
|
||||||
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
int term_width = w.ws_col > 250 ? 250 : w.ws_col - 1;
|
int term_width = w.ws_col > 250 ? 250 : w.ws_col - 1;
|
||||||
Logger::log_file << std::string(term_width, '=') << std::endl << message << std::endl << std::string(term_width, '=') << std::endl;
|
Logger::log_file << std::string(term_width, '=') << std::endl << message << std::endl << std::string(term_width, '=') << std::endl;
|
||||||
|
#endif
|
||||||
} else {
|
} else {
|
||||||
Logger::log_file << "[" << level << "] " << message << std::endl;
|
Logger::log_file << "[" << level << "] " << message << std::endl;
|
||||||
}
|
}
|
||||||
|
|
107
src/routes.cpp
107
src/routes.cpp
|
@ -6,14 +6,14 @@
|
||||||
#include "logger.hpp"
|
#include "logger.hpp"
|
||||||
|
|
||||||
Routes::Routes(nlohmann::json config) {
|
Routes::Routes(nlohmann::json config) {
|
||||||
Logger::info("Partitioning configuration");
|
Logger::info("[Routes] Partitioning configuration");
|
||||||
const nlohmann::json config_update_files = config["update-files"];
|
const nlohmann::json config_update_files = config["update-files"];
|
||||||
const nlohmann::json config_run_actions = config["run-actions"];
|
const nlohmann::json config_run_actions = config["run-actions"];
|
||||||
const nlohmann::json config_tokens = config["tokens"];
|
const nlohmann::json config_tokens = config["tokens"];
|
||||||
|
|
||||||
Logger::info("Registering route \"/\"");
|
Logger::info("[Routes] Registering route \"/\"");
|
||||||
CROW_ROUTE(this->app, "/")
|
CROW_ROUTE(this->app, "/")
|
||||||
.methods("POST"_method)
|
.methods("POST"_method, "GET"_method)
|
||||||
.name("Ping")
|
.name("Ping")
|
||||||
([]() {
|
([]() {
|
||||||
nlohmann::json response = {
|
nlohmann::json response = {
|
||||||
|
@ -23,49 +23,72 @@ Routes::Routes(nlohmann::json config) {
|
||||||
return crow::response(200, response.dump());
|
return crow::response(200, response.dump());
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!config_update_files.is_null()) {
|
Logger::info("[Routes] Registering route \"/update-files\"");
|
||||||
Logger::info("Registering route \"/update-files\"");
|
CROW_ROUTE(this->app, "/update-files")
|
||||||
CROW_ROUTE(this->app, "/update-files")
|
.methods("POST"_method)
|
||||||
.methods("POST"_method)
|
.name("Update Files")
|
||||||
.name("Update Files")
|
([&config_update_files, &config_tokens](const crow::request &req) {
|
||||||
([&config_update_files, &config_tokens](const crow::request &req) {
|
if (config_update_files.is_null()) {
|
||||||
try {
|
Logger::warn("[Routes] No update-files configuration found");
|
||||||
return update_files(config_update_files, config_tokens, req);
|
nlohmann::json response = {
|
||||||
} catch (const std::exception &e) {
|
{"status", 404},
|
||||||
Logger::error("Unknown error in update_files: " + std::string(e.what()));
|
{"error", "No update-files configuration found"}
|
||||||
nlohmann::json response = {
|
};
|
||||||
{"status", 500},
|
return crow::response(404, response.dump());
|
||||||
{"error", "Internal server error"}
|
}
|
||||||
};
|
try {
|
||||||
return crow::response(500, response.dump());
|
return update_files(config_update_files, config_tokens, req);
|
||||||
}
|
} catch (const std::exception &e) {
|
||||||
});
|
Logger::error("[Routes] Unknown error in update_files: " + std::string(e.what()));
|
||||||
}
|
nlohmann::json response = {
|
||||||
|
{"status", 500},
|
||||||
|
{"error", "Internal server error"}
|
||||||
|
};
|
||||||
|
return crow::response(500, response.dump());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (!config_run_actions.is_null()) {
|
Logger::info("[Routes] Registering route \"/run-actions\"");
|
||||||
Logger::info("Registering route \"/run-actions\"");
|
CROW_ROUTE(this->app, "/run-actions")
|
||||||
CROW_ROUTE(this->app, "/run-actions")
|
.methods("POST"_method)
|
||||||
.methods("POST"_method)
|
.name("Run Actions")
|
||||||
.name("Run Actions")
|
([&config_run_actions](const crow::request &req) {
|
||||||
([&config_run_actions, &config_tokens](const crow::request &req) {
|
if (config_run_actions.is_null()) {
|
||||||
try {
|
Logger::warn("[Routes] No run-actions configuration found");
|
||||||
return run_actions(config_run_actions, config_tokens, req);
|
nlohmann::json response = {
|
||||||
} catch (const std::exception &e) {
|
{"status", 404},
|
||||||
Logger::error("Unknown error in run_actions: " + std::string(e.what()));
|
{"error", "No run-actions configuration found"}
|
||||||
nlohmann::json response = {
|
};
|
||||||
{"status", 500},
|
return crow::response(404, response.dump());
|
||||||
{"error", "Internal server error"}
|
}
|
||||||
};
|
try {
|
||||||
return crow::response(500, response.dump());
|
return run_actions(config_run_actions, req);
|
||||||
}
|
} catch (const std::exception &e) {
|
||||||
});
|
Logger::error("[Routes] Unknown error in run_actions: " + std::string(e.what()));
|
||||||
}
|
nlohmann::json response = {
|
||||||
|
{"status", 500},
|
||||||
|
{"error", "Internal server error"}
|
||||||
|
};
|
||||||
|
return crow::response(500, response.dump());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
Logger::info("Starting server");
|
Logger::info("[Routes] Registering catch-all route (404)");
|
||||||
|
CROW_CATCHALL_ROUTE(this->app)
|
||||||
|
([](const crow::request &req) {
|
||||||
|
nlohmann::json response = {
|
||||||
|
{"status", 404},
|
||||||
|
{"error", "Not found"}
|
||||||
|
};
|
||||||
|
return crow::response(404, response.dump());
|
||||||
|
});
|
||||||
|
|
||||||
|
Logger::info("[Routes] Routes registered");
|
||||||
|
Logger::info("[Routes] Starting server");
|
||||||
try {
|
try {
|
||||||
this->app.port(config["port"].get<int>()).multithreaded().run();
|
this->app.port(config["port"].get<int>()).multithreaded().run();
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
Logger::fatal("Error starting server: " + std::string(e.what()));
|
Logger::fatal("[Routes] Error starting server: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
Logger::info("Server stopped");
|
Logger::info("[Routes] Server stopped");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue