Files
Build-System/registry/service/Main.cpp
T
2026-11-27 01:43:06 +03:00

83 lines
2.9 KiB
C++

#include <Void/Build/BootstrapConfig.hpp>
#include <Void/Build/Registry/RegistryService.hpp>
#include <Void/Configuration/Argparse.hpp>
#include <Void/Configuration/Env.hpp>
#include <Void/System/SignalWatcher.hpp>
#include <Void/Text/Str.hpp>
#include <cstdint>
#include <iostream>
#include <string>
int main(int argc, char **argv) {
std::string data_dir;
std::string web_root;
std::string bind_address;
std::string port_str;
std::string pass_env;
const std::string default_web_root =
(std::filesystem::path{Void::Build::Bootstrap::source_dir} / "registry" / "web").string();
Void::Argparse::Parser parser{
"vbs-registry",
"Loopback-default VoidBuildSystem package registry service and web administration UI."};
parser
.option_default("--data-dir", "", "registry metadata and CAS directory", data_dir,
"registry-data")
.option_default("--web-root", "", "static web admin assets directory", web_root,
default_web_root)
.option_default("--bind", "", "IPv4 bind address", bind_address, "127.0.0.1")
.option_default("--port", "", "listening TCP port (0 for ephemeral)", port_str, "8080")
.option_default("--admin-password-env", "",
"environment variable holding bootstrap admin password", pass_env,
"VBS_REGISTRY_ADMIN_PASSWORD");
const auto parsed = parser.parse_detailed(argc, argv);
if (parsed.is_help()) {
std::cout << parsed.message;
return 0;
}
if (parsed.is_error()) {
std::cerr << "vbs-registry: " << parsed.message << '\n';
return 2;
}
int port_int = 0;
if (!Void::Str::to_int(port_str, port_int) || port_int < 0 || port_int > 65535) {
std::cerr << "vbs-registry: invalid --port value '" << port_str << "'\n";
return 2;
}
Void::Build::Registry::RegistryConfig cfg;
cfg.data_dir = data_dir;
cfg.web_root = web_root;
cfg.bind_address = bind_address;
cfg.port = static_cast<std::uint16_t>(port_int);
if (auto pw = Void::Configuration::get_env(pass_env); pw) {
cfg.bootstrap_admin_password = std::move(*pw);
}
auto signals = Void::Process::SignalWatcher::create();
auto svc = Void::Build::Registry::RegistryService::open(std::move(cfg));
if (!svc) {
std::cerr << "vbs-registry: failed opening registry: " << svc.error().message() << '\n';
return 1;
}
if (auto started = (*svc)->start_http(); !started) {
std::cerr << "vbs-registry: failed starting HTTP server: " << started.error().message()
<< '\n';
return 1;
}
std::cout << "vbs-registry listening on http://" << bind_address << ":" << (*svc)->bound_port()
<< " (SIGINT/SIGTERM to stop)\n"
<< std::flush;
if (signals) {
(void)signals->wait();
}
(*svc)->stop_http();
return 0;
}