Update Config
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
3234dc6e39
commit
e22fa683e0
58
README.md
58
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,12 +80,37 @@ 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": {
|
"tokens": {
|
||||||
|
"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
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
### `/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": {
|
"owner/repo-name": {
|
||||||
"branch": "main",
|
"branch": "main",
|
||||||
"files": {
|
"files": {
|
||||||
|
@ -98,28 +123,9 @@ The configuration file can be found in `/services/gh-wh-handler/config.json` and
|
||||||
"..."
|
"..."
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
|
||||||
"run-scripts": {
|
|
||||||
"owner/repo-name": {
|
|
||||||
"branch": "main",
|
|
||||||
"actions": [
|
|
||||||
"command",
|
|
||||||
"script",
|
|
||||||
]
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"tokens": {
|
|
||||||
"owner/repo-name": "token"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 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.
|
|
||||||
|
|
||||||
Since only the `/update-files` endpoint is implemented, the configuration file may not contain the `run-scripts` field.
|
|
||||||
|
|
||||||
## 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.
|
||||||
|
|
|
@ -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,6 +23,7 @@ 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("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)
|
||||||
|
@ -39,7 +40,9 @@ Routes::Routes(nlohmann::json config) {
|
||||||
return crow::response(500, response.dump());
|
return crow::response(500, response.dump());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!config_run_scripts.is_null()) {
|
||||||
Logger::info("Registering route \"/run-scripts\"");
|
Logger::info("Registering route \"/run-scripts\"");
|
||||||
CROW_ROUTE(this->app, "/run-scripts")
|
CROW_ROUTE(this->app, "/run-scripts")
|
||||||
.methods("POST"_method)
|
.methods("POST"_method)
|
||||||
|
@ -56,6 +59,7 @@ Routes::Routes(nlohmann::json config) {
|
||||||
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