Fix Indentations and Shutdown Handling

This commit is contained in:
Elia Zammuto 2021-05-11 22:19:29 +02:00
parent fd8cbf0c7d
commit 04421d84a3

View File

@ -120,25 +120,34 @@ namespace confighttp
response->write(data.str()); response->write(data.str());
}); });
pt::ptree inputTree, fileTree; pt::ptree inputTree, fileTree;
try { try
{
//TODO: Input Validation //TODO: Input Validation
pt::read_json(ss, inputTree); pt::read_json(ss, inputTree);
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = inputTree.get<int>("index"); int index = inputTree.get<int>("index");
BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty(); BOOST_LOG(info) << inputTree.get_child("prep-cmd").empty();
if(inputTree.get_child("prep-cmd").empty())inputTree.erase("prep-cmd"); if (inputTree.get_child("prep-cmd").empty())
inputTree.erase("prep-cmd");
inputTree.erase("index"); inputTree.erase("index");
if(index == -1){ if (index == -1)
{
apps_node.push_back(std::make_pair("", inputTree)); apps_node.push_back(std::make_pair("", inputTree));
} else { }
else
{
//Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick //Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick
pt::ptree newApps; pt::ptree newApps;
int i = 0; int i = 0;
for (const auto& kv : apps_node) { for (const auto &kv : apps_node)
if(i == index){ {
if (i == index)
{
newApps.push_back(std::make_pair("", inputTree)); newApps.push_back(std::make_pair("", inputTree));
} else { }
else
{
newApps.push_back(std::make_pair("", kv.second)); newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
@ -149,7 +158,9 @@ namespace confighttp
pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
outputTree.put("status", "true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} catch (std::exception &e) { }
catch (std::exception &e)
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid Input JSON"); outputTree.put("error", "Invalid Input JSON");
@ -167,21 +178,27 @@ namespace confighttp
response->write(data.str()); response->write(data.str());
}); });
pt::ptree fileTree; pt::ptree fileTree;
try { try
{
pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::read_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
auto &apps_node = fileTree.get_child("apps"s); auto &apps_node = fileTree.get_child("apps"s);
int index = stoi(request->path_match[1]); int index = stoi(request->path_match[1]);
BOOST_LOG(info) << index; BOOST_LOG(info) << index;
if(index <= 0){ if (index <= 0)
{
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid Index"); outputTree.put("error", "Invalid Index");
return; return;
} else { }
else
{
//Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick //Unfortuantely Boost PT does not allow to directly edit the array, copt should do the trick
pt::ptree newApps; pt::ptree newApps;
int i = 0; int i = 0;
for (const auto& kv : apps_node) { for (const auto &kv : apps_node)
if(i != index){ {
if (i != index)
{
newApps.push_back(std::make_pair("", kv.second)); newApps.push_back(std::make_pair("", kv.second));
} }
i++; i++;
@ -192,7 +209,9 @@ namespace confighttp
pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree); pt::write_json(SUNSHINE_ASSETS_DIR "/" APPS_JSON, fileTree);
outputTree.put("status", "true"); outputTree.put("status", "true");
proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON); proc::refresh(SUNSHINE_ASSETS_DIR "/" APPS_JSON);
} catch (std::exception &e) { }
catch (std::exception &e)
{
BOOST_LOG(warning) << e.what(); BOOST_LOG(warning) << e.what();
outputTree.put("status", "false"); outputTree.put("status", "false");
outputTree.put("error", "Invalid File JSON"); outputTree.put("error", "Invalid File JSON");
@ -232,8 +251,25 @@ namespace confighttp
shutdown_event->raise(true); shutdown_event->raise(true);
return; return;
} }
auto accept_and_run = [&](auto *http_server) {
try
{
http_server->accept_and_run();
}
catch (boost::system::system_error &err)
{
// It's possible the exception gets thrown after calling http_server->stop() from a different thread
if (shutdown_event->peek())
{
return;
}
std::thread tcp{&https_server_t::accept_and_run, &http_server}; BOOST_LOG(fatal) << "Couldn't start Configuration HTTP server to ports ["sv << PORT_HTTPS << ", "sv << PORT_HTTP << "]: "sv << err.what();
shutdown_event->raise(true);
return;
}
};
std::thread tcp{accept_and_run, &http_server};
// Wait for any event // Wait for any event
shutdown_event->view(); shutdown_event->view();