added JSON support to select the services

This commit is contained in:
2025-05-27 21:33:15 +02:00
parent d9d0643dfc
commit 1e9a377c2a
19 changed files with 26060 additions and 139 deletions

59
src/htmx_helper.cpp Normal file
View File

@@ -0,0 +1,59 @@
//
// Created by lukas on 5/11/25.
//
#include <array>
#include "systemd.h"
#include "htmx_helper.h"
#include "json_settings.h"
using namespace std;
HtmxTableRow create_service_table_row(string_view service_name, string_view service_id) {
HtmxTableRow row;
const bool isRunning = systemd::is_service_active(service_id);
const bool isEnabled = systemd::is_service_active(service_id);
const auto running = isRunning ? "Running" : "Stopped";
const auto enabled = isEnabled ? "Enabled" : "Disabled";
row.add(service_name);
row.add_status_box(running, isRunning);
row.add_status_box(enabled, isEnabled);
// create buttons
row.add_button("/toggle-service", service_name, isRunning ? "Stop" : "Start");
row.add_button("/restart-service", service_name, "Restart");
row.complete();
return row;
}
HtmxTableRow create_error_table_row(string_view error) {
HtmxTableRow row;
row.add("Error");
row.add(error);
return row;
}
HtmxTable create_service_table() {
constexpr array<string_view, 3> cols = {
"Service", "Status", "State"
};
HtmxTable table(cols);
auto settings = AppSettings::loadAppSettings();
if(settings.has_value()){
AppSettings& data = settings.value();
for (auto& service : data.services) {
table.add_row(create_service_table_row(service.name, service.service));
}
}
else {
table.add_row(create_error_table_row(settings.error()));
}
return table;
}