90 lines
3.2 KiB
C++
90 lines
3.2 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>
|
|
#include <string_view>
|
|
|
|
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");
|
|
|
|
if (argc == 2 && (std::string_view{argv[1]} == "--help" || std::string_view{argv[1]} == "-h")) {
|
|
std::cout << parser.help();
|
|
return 0;
|
|
}
|
|
for (int i = 1; i < argc; ++i) {
|
|
if (std::string_view{argv[i]} == "--help" || std::string_view{argv[i]} == "-h") {
|
|
std::cerr << "vbs-registry: --help must be used alone\n";
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
if (auto parsed = parser.parse(argc, argv); !parsed) {
|
|
std::cerr << "vbs-registry: " << parsed.error() << '\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;
|
|
}
|