Update Config
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
3234dc6e39
commit
e22fa683e0
66
README.md
66
README.md
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
## Simple C++ WebAPI to work with GitHub Webhooks
|
## Simple C++ WebAPI to work with GitHub Webhooks
|
||||||
|
|
||||||
Currently creating a local copy of remote files on every push
|
This application is a simple C++ WebAPI that listens for GitHub Webhooks and performs actions based on the received data and configuration.
|
||||||
|
|
||||||
## Usage
|
## Installation
|
||||||
|
|
||||||
### Use installation script (recommended)
|
### Use installation script (recommended)
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ Head over to the [Releases Page](https://github.com/TiagoRG/gh-wh-handler/releas
|
||||||
|
|
||||||
Run the application using your configuration file:
|
Run the application using your configuration file:
|
||||||
```console
|
```console
|
||||||
/path/to/gh-wh-handler.<arch> /path/to/config.json
|
/path/to/gh-wh-handler.<arch> /path/to/config.json /path/to/logs_dir
|
||||||
```
|
```
|
||||||
|
|
||||||
You can see the config file format below.
|
You can see the config file format below.
|
||||||
|
@ -32,7 +32,7 @@ You can see the config file format below.
|
||||||
- [CrowCpp](https://crowcpp.org/master/)
|
- [CrowCpp](https://crowcpp.org/master/)
|
||||||
- [nlohmann::json](https://github.com/nlohmann/json)
|
- [nlohmann::json](https://github.com/nlohmann/json)
|
||||||
|
|
||||||
#### Build the application:
|
#### Build and install the application:
|
||||||
|
|
||||||
1. Clone the repository:
|
1. Clone the repository:
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ cmake ..
|
||||||
sudo make install
|
sudo make install
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Run the application:
|
## Usage
|
||||||
|
|
||||||
The application is running on a systemd service, which is both enabled and started after installation.
|
The application is running on a systemd service, which is both enabled and started after installation.
|
||||||
|
|
||||||
|
@ -80,45 +80,51 @@ As of now, the configuration menu is not yet implemented so you have to create t
|
||||||
|
|
||||||
### Config File
|
### Config File
|
||||||
|
|
||||||
The configuration file can be found in `/services/gh-wh-handler/config.json` and has the following format:
|
The configuration file can be found in `/services/gh-wh-handler/config.json` and has the following base format:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"port": 65001,
|
"port": 65001,
|
||||||
"update-files": {
|
|
||||||
"owner/repo-name": {
|
|
||||||
"branch": "main",
|
|
||||||
"files": {
|
|
||||||
"path/to/remote/file": "/path/to/local/file",
|
|
||||||
"...": "..."
|
|
||||||
},
|
|
||||||
"post-update": [
|
|
||||||
"post-update-command",
|
|
||||||
"post-update-script",
|
|
||||||
"..."
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"run-scripts": {
|
|
||||||
"owner/repo-name": {
|
|
||||||
"branch": "main",
|
|
||||||
"actions": [
|
|
||||||
"command",
|
|
||||||
"script",
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tokens": {
|
"tokens": {
|
||||||
"owner/repo-name": "token"
|
"owner/repo-name": "token"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
This configuration will then have more fields for each endpoint that you want to configure.
|
||||||
|
|
||||||
|
Note: Tokens are only required for private repositories.
|
||||||
|
|
||||||
## Endpoints
|
## Endpoints
|
||||||
|
|
||||||
Currently, the only endpoint for the application is `/update-files`, which is used to update the local files on every push as well as run post-update scripts.
|
Currently, the only endpoint for the application is `/update-files`, which is used to update the local files on every push as well as run post-update scripts.
|
||||||
|
|
||||||
Since only the `/update-files` endpoint is implemented, the configuration file may not contain the `run-scripts` field.
|
### `/update-files`
|
||||||
|
|
||||||
|
#### Webhook event: `push`
|
||||||
|
|
||||||
|
This endpoint allows the application to update specific files on the server when a push to a specific branch is made. This way, there's no need to manually update the files on the server or to pull the entire repository.
|
||||||
|
|
||||||
|
It also allows the application to run post-update scripts after the files are updated.
|
||||||
|
|
||||||
|
The configuration file must contain the `update-files` field, which is an object with the following format:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"update-files": {
|
||||||
|
"owner/repo-name": {
|
||||||
|
"branch": "main",
|
||||||
|
"files": {
|
||||||
|
"path/to/remote/file": "/path/to/local/file",
|
||||||
|
"...": "..."
|
||||||
|
},
|
||||||
|
"post-update": [
|
||||||
|
"post-update-command",
|
||||||
|
"post-update-script",
|
||||||
|
"..."
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|
|
@ -9,8 +9,6 @@ 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},
|
||||||
{"update-files", nlohmann::json::array()},
|
|
||||||
{"run-scripts", nlohmann::json::array()},
|
|
||||||
{"tokens", nlohmann::json::array()},
|
{"tokens", nlohmann::json::array()},
|
||||||
};
|
};
|
||||||
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('/'));
|
||||||
|
@ -43,6 +41,20 @@ nlohmann::json Config::get_config(std::string config_file_path) {
|
||||||
Logger::fatal("Error loading config file: " + std::string(e.what()));
|
Logger::fatal("Error loading config file: " + std::string(e.what()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (config.is_null()) {
|
||||||
|
Logger::fatal("[Config] Config file is empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.find("port") == config.end()) {
|
||||||
|
Logger::warn("[Config] Port not found in config file, using default port 65001");
|
||||||
|
config["port"] = 65001;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.find("tokens") == config.end()) {
|
||||||
|
Logger::warn("[Config] Tokens not found in config file, using empty array. Private repositories will not be accessible.");
|
||||||
|
config["tokens"] = nlohmann::json::array();
|
||||||
|
}
|
||||||
|
|
||||||
Logger::success("[Config] Loaded config file: " + config_file_path);
|
Logger::success("[Config] Loaded config file: " + config_file_path);
|
||||||
Logger::info("[Config] Loaded config: ");
|
Logger::info("[Config] Loaded config: ");
|
||||||
Logger::code(config.dump(2));
|
Logger::code(config.dump(2));
|
||||||
|
|
|
@ -23,39 +23,43 @@ Routes::Routes(nlohmann::json config) {
|
||||||
return crow::response(200, response.dump());
|
return crow::response(200, response.dump());
|
||||||
});
|
});
|
||||||
|
|
||||||
Logger::info("Registering route \"/update-files\"");
|
if (!config_update_files.is_null()) {
|
||||||
CROW_ROUTE(this->app, "/update-files")
|
Logger::info("Registering route \"/update-files\"");
|
||||||
.methods("POST"_method)
|
CROW_ROUTE(this->app, "/update-files")
|
||||||
.name("Update Files")
|
.methods("POST"_method)
|
||||||
([&config_update_files, &config_tokens](const crow::request &req) {
|
.name("Update Files")
|
||||||
try {
|
([&config_update_files, &config_tokens](const crow::request &req) {
|
||||||
return update_files(config_update_files, config_tokens, req);
|
try {
|
||||||
} catch (const std::exception &e) {
|
return update_files(config_update_files, config_tokens, req);
|
||||||
Logger::error("Unknown error in update_files: " + std::string(e.what()));
|
} catch (const std::exception &e) {
|
||||||
nlohmann::json response = {
|
Logger::error("Unknown error in update_files: " + std::string(e.what()));
|
||||||
{"status", 500},
|
nlohmann::json response = {
|
||||||
{"error", "Internal server error"}
|
{"status", 500},
|
||||||
};
|
{"error", "Internal server error"}
|
||||||
return crow::response(500, response.dump());
|
};
|
||||||
}
|
return crow::response(500, response.dump());
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Logger::info("Registering route \"/run-scripts\"");
|
if (!config_run_scripts.is_null()) {
|
||||||
CROW_ROUTE(this->app, "/run-scripts")
|
Logger::info("Registering route \"/run-scripts\"");
|
||||||
.methods("POST"_method)
|
CROW_ROUTE(this->app, "/run-scripts")
|
||||||
.name("Run Scripts")
|
.methods("POST"_method)
|
||||||
([&config_run_scripts, &config_tokens](const crow::request &req) {
|
.name("Run Scripts")
|
||||||
try {
|
([&config_run_scripts, &config_tokens](const crow::request &req) {
|
||||||
return run_scripts(config_run_scripts, config_tokens, req);
|
try {
|
||||||
} catch (const std::exception &e) {
|
return run_scripts(config_run_scripts, config_tokens, req);
|
||||||
Logger::error("Unknown error in run_scripts: " + std::string(e.what()));
|
} catch (const std::exception &e) {
|
||||||
nlohmann::json response = {
|
Logger::error("Unknown error in run_scripts: " + std::string(e.what()));
|
||||||
{"status", 500},
|
nlohmann::json response = {
|
||||||
{"error", "Internal server error"}
|
{"status", 500},
|
||||||
};
|
{"error", "Internal server error"}
|
||||||
return crow::response(500, response.dump());
|
};
|
||||||
}
|
return crow::response(500, response.dump());
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
Logger::info("Starting server");
|
Logger::info("Starting server");
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in New Issue