Files

182 lines
7.7 KiB
C++
Raw Permalink Normal View History

#include "IndexCatalog.hpp"
#include <cctype>
#include <map>
#include <set>
#include "../fw/Templates.hpp"
namespace molo::example::app {
namespace {
bool has_prefix(const std::string& route, const std::string& prefix) {
if (route.compare(0, prefix.size(), prefix) != 0) return false;
if (route.size() == prefix.size()) return true;
// Whole-segment match, so "/data" does not claim "/database". A prefix with
// a scheme ("http://") is a plain string prefix.
if (prefix.back() == '/') return true;
char next = route[prefix.size()];
return next == '/' || next == '?';
}
const char* kVendorKeys =
"Needs vendor API keys (XAI_API_KEY, ANTHROPIC_API_KEY, OPENAI_API_KEY, "
"OPENROUTER_API_KEY, DEEPSEEK_API_KEY, META_API_KEY, ...) in the environment "
"or .env.json.local. Without a key the page still loads and calls answer "
"503 not_configured; paid routes are dry runs unless confirmed.";
} // namespace
IndexCatalog::IndexCatalog() {
groups_ = {
{"routing", "Routing and views",
"Paths, class views, routers, app chaining, redirects, dependency injection.", "",
{"/routing", "/middleware", "/app1", "/redirect", "/conditional", "/context", "/di", "/hello",
"/greet", "/deep"}},
{"requests", "Requests, forms and templates",
"Forms, validation with all field errors, templates, content negotiation, uploads, i18n.",
"", {"/forms", "/validate", "/templates", "/formats", "/flash", "/compress", "/i18n",
"/uploads"}},
{"data", "Data and events",
"ORM on SQLite and Postgres with self-healing, key-value store, cache, pubsub, events, replication.",
"", {"/orm", "/data", "/kv", "/cache", "/pubsub", "/events", "/replication"}},
{"security", "Security",
"Auth, sessions, CSRF, CORS, rate limits, security headers, CSP, crypto, roles, upload and input guards.",
"", {"/auth", "/sessions", "/security", "/csp", "/crypto"}},
{"static", "Static and embedded files",
"Assets from disk and from the binary, with ETags and range requests.", "",
{"/static", "/embedded"}},
{"realtime", "Realtime",
"WebSocket, Socket.IO, server-sent events, streaming, passthrough, WebDAV, reverse tunnels.",
"", {"/ws", "/socketio", "/sse", "/streaming", "/webdav", "/expose"}},
{"observability", "Observability",
"Metrics, Prometheus scrape, tracing, structured logging, live charts, health.", "",
{"/metrics", "/telemetry", "/logging", "/stats", "/health", "/healthz", "/readyz",
"/startupz"}},
{"operations", "Operations and delivery",
"Supervisor and tasks, REST client, operability tools, OpenAPI and docs, release workflow, supply chain.",
"", {"/supervisor", "/restclient", "/clients", "/operability", "/cli", "/workflow", "/supplychain",
"/openapi.json", "/docs"}},
{"doos", "Sandboxed containers",
"Rootless user-level containers: status page, create, inspect, delete (demo store).", "",
{"/doos"}},
{"tenancy", "Multi-tenancy",
"Tenant resolution, data isolation, quotas, feature flags, per-tenant config and metrics.",
"", {"/tenancy"}},
{"audit", "Audit and compliance",
"Hash-chained audit log, tamper detection, PII masking, retention, export and erasure.",
"", {"/audit"}},
{"governance", "API governance",
"API keys, rate tiers, versioning with Deprecation and Sunset, linter, OpenAPI diff, problem+json.",
"", {"/governance", "/v1", "/v2"}},
{"google", "Google Cloud and voice",
"Gemini, Google Cloud APIs and the live spoken conversation. Without a key pages load and calls answer 503 not_configured.",
"GOOGLE_API_KEY", {"/google", "/voice"}},
{"agent", "Agent Platform",
"Gemini Enterprise Agent Platform modules: models, runtime, sessions, sandbox, registry, armor, live.",
"GOOGLE_API_KEY", {"/agent"}},
{"vendors", "LLM vendors", kVendorKeys, "vendor API keys", {"/ai"}},
{"tools", "Tools", "Load testing and other helpers outside the app.", "",
{"http://", "https://"}},
};
other_ = {"other", "Other", "Registered by the app, not yet grouped.", "", {}};
}
std::string IndexCatalog::pretty_name(const std::string& feature) {
if (feature.empty()) return feature;
if (feature.find(' ') != std::string::npos ||
std::isupper(static_cast<unsigned char>(feature[0]))) {
std::string out = feature;
out[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(out[0])));
return out;
}
static const std::map<std::string, std::string> kTokens = {
{"ai", "AI"}, {"api", "API"}, {"orm", "ORM"},
{"sse", "SSE"}, {"kv", "KV"}, {"cli", "CLI"},
{"csp", "CSP"}, {"tls", "TLS"}, {"webdav", "WebDAV"},
{"socketio", "Socket.IO"}, {"openapi", "OpenAPI"}, {"openai", "OpenAI"},
{"xai", "xAI"}, {"sqlite", "SQLite"}, {"postgres", "Postgres"},
{"deepseek", "DeepSeek"}, {"openrouter", "OpenRouter"},
{"huggingface", "Hugging Face"}, {"restclient", "REST client"},
};
std::string out;
std::string token;
auto flush = [&] {
if (token.empty()) return;
if (!out.empty()) out += ' ';
auto it = kTokens.find(token);
if (it != kTokens.end()) {
out += it->second;
} else {
if (out.empty()) token[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(token[0])));
out += token;
}
token.clear();
};
for (char c : feature) {
if (c == '-' || c == '_') flush();
else token += c;
}
flush();
return out;
}
const FeatureGroup& IndexCatalog::group_for(const std::string& route) const {
for (const auto& g : groups_) {
for (const auto& p : g.prefixes) {
if (has_prefix(route, p)) return g;
}
}
return other_;
}
std::string IndexCatalog::render(
const std::vector<std::pair<std::string, std::string>>& rows,
std::size_t* feature_count, std::size_t* group_count) const {
using fw::html_escape;
std::map<std::string, std::vector<FeatureRow>> by_group;
std::set<std::string> seen;
std::size_t total = 0;
for (const auto& [name, route] : rows) {
if (!seen.insert(name + '\n' + route).second) continue;
by_group[group_for(route).id].push_back({pretty_name(name), route});
++total;
}
std::vector<const FeatureGroup*> order;
for (const auto& g : groups_) order.push_back(&g);
order.push_back(&other_);
std::string chips = "<nav class=\"chips\" aria-label=\"Groups\">";
std::string body;
std::size_t groups = 0;
for (const FeatureGroup* g : order) {
auto it = by_group.find(g->id);
if (it == by_group.end()) continue;
++groups;
chips += "<a href=\"#g-" + html_escape(g->id) + "\">" + html_escape(g->title) + "</a>";
body += "<section class=\"group\" id=\"g-" + html_escape(g->id) + "\">\n<h2>" +
html_escape(g->title) + " <span class=\"count\">" +
std::to_string(it->second.size()) + "</span>";
if (!g->key_hint.empty()) {
body += " <span class=\"badge\" title=\"" + html_escape(g->key_hint) +
"\">needs API key</span>";
}
body += "</h2>\n<p class=\"hint\">" + html_escape(g->blurb) +
"</p>\n<div class=\"scroll\"><table>\n<thead><tr><th>Feature</th>"
"<th class=\"route\">Route</th></tr></thead>\n<tbody>\n";
for (const auto& r : it->second) {
body += "<tr><td><a href=\"" + html_escape(r.route) + "\">" + html_escape(r.name) +
"</a></td><td class=\"route\"><code>" + html_escape(r.route) +
"</code></td></tr>\n";
}
body += "</tbody></table></div>\n</section>\n";
}
chips += "</nav>\n";
if (feature_count) *feature_count = total;
if (group_count) *group_count = groups;
return chips + body;
}
} // namespace molo::example::app