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

@@ -1,10 +1,11 @@
#include "../httplib.h"
#include "../httplib.h"
//#include <windows.h>
//#include "Service/AuthorizationService.cpp"
//#include "Service/FileProcessingService.cpp"
#include "../Service/AuthorizationService.cpp"
#include "../Service/FileProcessingService.cpp"
#include "../Models/App.cpp"
//#include "Service/Logger.cpp"
class PublishController
@@ -20,6 +21,8 @@ private:
Logger& logger_;
int last_available_port = 8081;
//std::string publish_app_path = "C:/Temps/";// Todo delete if not needed
public:
@@ -29,49 +32,47 @@ public:
}
public:
void process_publish(const httplib::Request& req, httplib::Response& res)
std::string process_publish(const httplib::Request& req, App* app)
{
//if (this->authorization.is_user_authorized())
if (true)//Todo change to is user authorized
{
const auto& content = req.files.begin()->second.content;
const auto& content = req.files.begin()->second.content;
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
const auto& filename = this->publish_app_path + req.files.begin()->second.filename;
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
if (file_processing->save_file(filename, content)) {
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".rar") {
//if (true) {//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS
if (file_processing->save_file(filename, content)) {
std::string random_string = generate_random_string(20);//TODO VERY IMPORTANT CHANGE THIS RANDOM GENERATING TO GENERATE UNIQUE STRING
file_processing->unzip(filename, this->publish_app_path + app->get_user_id());//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS
file_processing->unzip(filename, this->publish_app_path + random_string);
/*check_port_and_increase_if_not_available();
this->dotnet_publish(this->publish_app_path + random_string);
file_processing->adjust_nginx_configuration_and_reloud(app->get_name(), std::to_string(last_available_port));*/
res.set_content("File uploaded successfully: " + filename, "text/plain");
}
else {
res.status = 500;
res.set_content("Failed to save file, please ensure you are putting rar file"
+ filename, "text/plain");
}
//file_processing->create_service_file(app->get_name());//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS
//this->dotnet_publish(this->publish_app_path + app->get_user_id(), last_available_port);//TODO UNCOMMIT WHEN STARING TO WRITE PUBLISH PROCESS
//Todo introduce old binary file
file_processing->delete_file(filename);
return "File uploaded successfully: " + filename;
}
else {
res.status = 400;
res.set_content("Invalid file type. Only .rar files are allowed.",
"text/plain");
return "Failed to save file, please ensure you are putting rar file" + filename;
}
}
else
{
//Todo add logging and exiting from function with bead request
else {
return "Invalid file type. Only .rar files are allowed." + filename;
}
}
private:
void dotnet_publish(const std::string& path)
{
void dotnet_publish(const std::string& path, int port)
{//Todo adjust to build setting from mongodb
std::string dll_file_name = file_processing->find_file_by_suffix(path, "exe");
//Todo introduce deleting old rar file after publishing
size_t pos = dll_file_name.find(".exe");
if (pos != std::string::npos) {
dll_file_name.replace(pos, 4, ".dll");
@@ -84,6 +85,41 @@ private:
commandThread.detach();
}
void check_port_and_increase_if_not_available()
{
while (true)
{
std::string port_str = std::to_string(last_available_port);
std::string command = "ss -tuln | grep :" + port_str;
auto request = std::async(std::launch::async, &PublishController::execute_command, this, command);
std::string response = request.get();
if (!response.empty())
{
last_available_port++;
}
else
{
break;
}
}
}
std::string execute_command(const std::string& command) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(command.c_str(), "r"), pclose);
if (!pipe) throw std::runtime_error("popen() failed!");
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
static std::string generate_random_string(size_t length, const std::string& char_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
std::random_device rd;
std::mt19937 generator(rd());