8#include <tier1/convar.h>
23 _sections.emplace_back(std::move(name), std::move(
provider));
28 return !_healthy || _healthy();
34 std::map<std::string, glz::raw_json>
out;
35 for (
const auto& [name,
provider] : _sections)
38 out[name] = glz::validate_json(
section) ? glz::raw_json{R
"({"error":"provider returned invalid JSON"})"}
39 : glz::raw_json{std::move(section)};
41 out["healthy"] = glz::raw_json{
IsHealthy() ?
"true" :
"false"};
48 for (
const auto& [name,
provider] : _sections)
55 out +=
" <invalid JSON from provider>\n";
59 for (
const auto& [
key, value] :
section->get_object())
61 std::string
rendered = value.is_string() ? value.get_string() : value.dump().value_or(std::string{});
67 out += std::format(
" {}\n",
section->dump().value_or(std::string{}));
70 if (!
out.empty() &&
out.back() ==
'\n')
82 if (
args.ArgC() > 1 && std::string_view(
args.Arg(1)) ==
"json")
84 Msg(
"STATUS_JSON %s\n", BuildJson().c_str());
static std::string Write(const T &value)
Serialize value as compact JSON.
static Result< glz::generic > ParseDocument(std::string_view text)
Parse free-form JSON whose shape is not known at compile time.
std::function< std::string()> Provider
Returns this section's payload as JSON text (an object or scalar).
void InstallCommand(std::string_view name, std::string_view helpText, HealthCheck healthy={})
Install the server command that reports this status.
std::string BuildJson() const
One JSON object with a key per section, plus a top-level healthy flag.
bool IsHealthy() const
The registered health result, or true when no predicate is registered.
std::function< bool()> HealthCheck
Optional plugin-specific health predicate.
std::string BuildText() const
Human-readable multi-line rendering of the same sections.
void RegisterSection(std::string name, Provider provider)
Add a section, replacing any existing one with the same name.
static std::string ReadFile(const std::filesystem::path &path)