Adjust app introduce port changinf logic and process file processing fuctnions + adjust drawio file diagramm

This commit is contained in:
Hepatica
2024-08-17 00:09:51 +02:00
parent 9215ad7a8f
commit 2356358163
8 changed files with 267 additions and 350 deletions

View File

@@ -7,6 +7,7 @@
#include <sys/stat.h>
#include "CommandService.cpp"
#include "Logger.cpp"
#include <string.h>
class FileProcessingService
{
Logger& logger_;
@@ -18,6 +19,79 @@ public:
}
void adjust_nginx_configuration_and_reloud(const std::string& filename, std::string port)
{
std::string file_path = "/etc/nginx/nginx.conf";
std::string new_text =
"server {\n"
" listen 443 ssl;\n"
" server_name " + filename + ".almavid.ru;\n\n"
" ssl_certificate /etc/letsencrypt/live/almavid.ru/fullchain.pem;\n"
" ssl_certificate_key /etc/letsencrypt/live/almavid.ru/privkey.pem;\n"
" ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\n"
" ssl_ciphers HIGH:!aNULL:!MD5;\n\n"
" client_max_body_size 2G; // Allow file uploads up to 2GB\n\n"
" location / {\n"
" proxy_pass http://localhost:" + port + ";\n"
" proxy_set_header Host $host;\n"
" proxy_set_header X-Real-IP $remote_addr;\n"
" proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n"
" proxy_set_header X-Forwarded-Proto $scheme;\n\n"
" # Support for WebSocket\n"
" proxy_http_version 1.1;\n"
" proxy_set_header Upgrade $http_upgrade;\n"
" proxy_set_header Connection \"upgrade\";\n"
" }\n\n"
"}\n";
std::ofstream file(file_path, std::ios::app);
if (!file.is_open()) {
logger_.log(INFO, "Error: Could not open file " + file_path + strerror(errno) + '\n');
return;
}
file << new_text << '\n';
file.close();
// Reload Nginx to apply the changes
/*int result = std::system("sudo systemctl reload nginx");
if (result != 0) {
std::cerr << "Error: Failed to reload Nginx" << std::endl;
return 1;
}*/
std::string command = "sudo systemctl reload nginx";
std::thread commandThread(&CommandService::execute_command, command);
commandThread.join();
logger_.log(INFO, "Nginx reloaded successfully.");
}
void delete_file(const std::string& file_path) const
{
try {
// Delete the original file
if (std::filesystem::exists(file_path)) {
std::filesystem::remove(file_path);
logger_.log(INFO, "Deleted file: " + file_path);
}
// Delete the directory recursively//Todo test if method will not work
/*if (std::filesystem::exists(final_files_directory)) {
std::filesystem::remove_all(final_files_directory);
logger_.log(INFO, "Deleted directory: " + final_files_directory);
}*/
}
catch (const std::filesystem::filesystem_error& e) {
logger_.log(ERROR, "Error during deletion: " + std::string(e.what()));
}
}
bool save_file(const std::string& filename, const std::string& content) {
logger_.log(INFO, "Start saving file method");
@@ -35,6 +109,48 @@ public:
return ofs.good();
}
void create_service_file(std::string name)
{
logger_.log(INFO, "Start create_service_file");
std::string filename = "/etc/systemd/system/" + name + ".service";
std::ofstream serviceFile(filename);
if (serviceFile.is_open()) {
serviceFile << "[Unit]\n";
serviceFile << "Description=" << name << " Service\n";
serviceFile << "After=network.target\n\n";
serviceFile << "[Service]\n";
serviceFile << "ExecStart=/home/danilt2000/SpCloud/" + name + "/build/ " + name + "\n";
//serviceFile << "ExecStart=/home/danilt2000/SpCloud/SpCloudMain/build/SpCloudMain\n";
serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/" + name + "/build\n";
//serviceFile << "WorkingDirectory=/home/danilt2000/SpCloud/SpCloudMain/build\n";
serviceFile << "Restart=always\n";
serviceFile << "User=danilt2000\n";
serviceFile << "Environment=PATH=/usr/bin\n";
serviceFile << "Environment=NODE_ENV=production\n\n";
serviceFile << "[Install]\n";
serviceFile << "WantedBy=multi-user.target\n";
std::string command = "sudo systemctl daemon-reload";
std::thread commandThread(&CommandService::execute_command, command);
commandThread.join();
//Todo check service ->sudo systemctl status <service-name>.service
serviceFile.close();
logger_.log(INFO, "Service file " + filename + " created successfully.\n");
}
else {
logger_.log(INFO, "Unable to open file " + filename + " for writing: " + strerror(errno) + "\n");
}
}
void create_directory(const std::string& path) {
std::filesystem::create_directories(path);
}
@@ -60,8 +176,4 @@ public:
}
return ""; //todo add throwing exception
}
/*private:
std::mutex file_mutex; */// Мьютекс для синхронизации доступа к файлу
};