feat: implement MoloApp plans 01-15 with integration

Full C++23 framework tree: runtime, observability, security,
data, app, realtime, supervisor, clients, CLI, example plus
enterprise tenancy, audit, operability, governance,
supplychain. Postgres via docker compose for tests.
This commit is contained in:
2026-10-23 18:20:04 +02:00
parent 54068f1953
commit 3c4334cec9
1068 changed files with 100899 additions and 2 deletions
+124
View File
@@ -0,0 +1,124 @@
#include "clients/rest/errors.h"
#include "clients/headers.h"
namespace molo::clients::rest {
namespace {
std::string str_field(const body::Json& value, const std::string& key) {
const body::Json* found = value.find(key);
if (found && found->is_string()) return found->as_string();
return "";
}
} // namespace
ProblemDetails ProblemDetails::parse(const body::Json& value) {
ProblemDetails problem;
if (!value.is_object()) return problem;
std::string type = str_field(value, "type");
if (!type.empty()) problem.type = type;
problem.title = str_field(value, "title");
const body::Json* status = value.find("status");
if (status && status->is_number())
problem.status = (int)status->as_number();
problem.detail = str_field(value, "detail");
problem.instance = str_field(value, "instance");
const body::Json* errors = value.find("errors");
if (errors && errors->is_array()) {
for (const auto& item : errors->as_array()) {
if (!item.is_object()) continue;
FieldFailure failure;
failure.field = str_field(item, "field");
if (failure.field.empty()) failure.field = str_field(item, "loc");
failure.message = str_field(item, "message");
if (failure.message.empty()) failure.message = str_field(item, "msg");
failure.type = str_field(item, "type");
problem.errors.push_back(failure);
}
}
return problem;
}
ProblemDetails ProblemDetails::from_response(const ClientResponse& response) {
ProblemDetails problem;
problem.status = response.status();
problem.title = "HTTP " + std::to_string(response.status());
if (response.content_type() == content_type() ||
response.content_type() == "application/json") {
try {
body::Json parsed = response.json();
if (parsed.is_object() &&
(parsed.has("title") || parsed.has("detail"))) {
problem = ProblemDetails::parse(parsed);
if (problem.status == 0) problem.status = response.status();
return problem;
}
} catch (...) {
// Fall through to the generic shape below.
}
}
if (!response.body().empty() && response.body().size() < 4096)
problem.detail = response.body();
return problem;
}
body::Json ProblemDetails::to_json() const {
body::Json value = body::Json::object();
value.set("type", body::Json(type));
value.set("title", body::Json(title));
value.set("status", body::Json(status));
if (!detail.empty()) value.set("detail", body::Json(detail));
if (!instance.empty()) value.set("instance", body::Json(instance));
if (!errors.empty()) {
body::Json list = body::Json::array();
for (const auto& item : errors) {
body::Json entry = body::Json::object();
entry.set("field", body::Json(item.field));
entry.set("message", body::Json(item.message));
if (!item.type.empty()) entry.set("type", body::Json(item.type));
list.push(std::move(entry));
}
value.set("errors", std::move(list));
}
return value;
}
std::string ProblemDetails::summary() const {
std::string out = std::to_string(status);
if (!title.empty()) {
out += " ";
out += title;
}
if (!detail.empty()) {
out += ": ";
out += detail;
}
return out;
}
RestError::RestError(ProblemDetails problem)
: std::runtime_error("molo::clients::rest: " + problem.summary()),
problem_(std::move(problem)) {}
void raise_for_status(const ClientResponse& response) {
if (response.status() >= 400) throw RestError(ProblemDetails::from_response(response));
}
ClientResponse problem_response(int status, const std::string& title,
const std::string& detail,
const std::string& instance) {
ProblemDetails problem;
problem.type = "https://molo.app/problems/" + std::to_string(status);
problem.title = title;
problem.status = status;
problem.detail = detail;
problem.instance = instance;
HeaderMap headers;
headers.set("Content-Type", ProblemDetails::content_type());
std::string body = problem.to_json().dump();
headers.set("Content-Length", std::to_string(body.size()));
return ClientResponse(status, headers, body);
}
} // namespace molo::clients::rest
+53
View File
@@ -0,0 +1,53 @@
#pragma once
// RFC 9457 problem details plus raise-for-status.
#include <stdexcept>
#include <string>
#include <vector>
#include "clients/body/json.h"
#include "clients/response.h"
namespace molo::clients::rest {
struct FieldFailure {
std::string field;
std::string message;
std::string type;
};
struct ProblemDetails {
std::string type = "about:blank";
std::string title;
int status = 0;
std::string detail;
std::string instance;
std::vector<FieldFailure> errors;
static ProblemDetails parse(const body::Json& value);
static ProblemDetails from_response(const ClientResponse& response);
body::Json to_json() const;
std::string summary() const;
static const char* content_type() { return "application/problem+json"; }
};
class RestError : public std::runtime_error {
public:
explicit RestError(ProblemDetails problem);
const ProblemDetails& problem() const { return problem_; }
int status() const { return problem_.status; }
private:
ProblemDetails problem_;
};
// Throws RestError when status >= 400, carrying parsed problem details.
void raise_for_status(const ClientResponse& response);
// Builds a problem-details ClientResponse (server-side helper for tests).
ClientResponse problem_response(int status, const std::string& title,
const std::string& detail,
const std::string& instance = "");
} // namespace molo::clients::rest
+47
View File
@@ -0,0 +1,47 @@
#include "clients/rest/idempotency.h"
#include <random>
#include "clients/caps.h"
namespace molo::clients::rest {
std::string IdempotencyKey::generate() {
static thread_local std::mt19937_64 rng{std::random_device{}()};
static const char* kDigits = "0123456789abcdef";
std::string key;
key.reserve(32);
for (int i = 0; i < 32; ++i) key.push_back(kDigits[rng() % 16]);
return key;
}
std::optional<IdempotentResult> InMemoryIdempotencyStore::lookup(
const std::string& key) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = entries_.find(key);
if (it == entries_.end()) return std::nullopt;
if (std::chrono::steady_clock::now() - it->second.stored_at >
Caps::kIdempotencyTtl) {
entries_.erase(it);
return std::nullopt;
}
return it->second;
}
void InMemoryIdempotencyStore::store(const std::string& key,
IdempotentResult result) {
std::lock_guard<std::mutex> lock(mutex_);
if (entries_.size() >= Caps::kIdempotencyMaxEntries && !entries_.count(key)) {
// Loud skip: evict nothing silently is worse, so drop the oldest entry.
entries_.erase(entries_.begin());
}
result.stored_at = std::chrono::steady_clock::now();
entries_[key] = std::move(result);
}
std::size_t InMemoryIdempotencyStore::size() const {
std::lock_guard<std::mutex> lock(mutex_);
return entries_.size();
}
} // namespace molo::clients::rest
+48
View File
@@ -0,0 +1,48 @@
#pragma once
// Idempotency-Key sender plus the keyed-result store interface. The in-memory
// store is the single-process dev default; part 04 KV backs production.
#include <chrono>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include "clients/body/json.h"
namespace molo::clients::rest {
class IdempotencyKey {
public:
static std::string generate();
static const char* header() { return "Idempotency-Key"; }
};
struct IdempotentResult {
int status = 0;
std::string body;
std::string content_type;
std::chrono::steady_clock::time_point stored_at;
};
class IdempotencyStore {
public:
virtual ~IdempotencyStore() = default;
virtual std::optional<IdempotentResult> lookup(const std::string& key) = 0;
virtual void store(const std::string& key, IdempotentResult result) = 0;
};
class InMemoryIdempotencyStore : public IdempotencyStore {
public:
std::optional<IdempotentResult> lookup(const std::string& key) override;
void store(const std::string& key, IdempotentResult result) override;
std::size_t size() const;
private:
mutable std::mutex mutex_;
std::map<std::string, IdempotentResult> entries_;
};
} // namespace molo::clients::rest
+112
View File
@@ -0,0 +1,112 @@
#include "clients/rest/paginator.h"
#include <utility>
#include "clients/caps.h"
#include "clients/rest/errors.h"
#include "clients/url.h"
namespace molo::clients::rest {
Paginator::Paginator(Client& client, std::string collection_url)
: client_(client), url_(std::move(collection_url)) {}
Paginator& Paginator::per_page(int n) {
if (n < 1) n = 1;
if (n > Caps::kPageMax) n = Caps::kPageMax;
per_page_ = n;
return *this;
}
Paginator& Paginator::query_arg(const std::string& name,
const std::string& value) {
extra_[name] = value;
return *this;
}
Page Paginator::parse_envelope(const ClientResponse& response) {
body::Json doc = response.json();
Page page;
if (doc.is_array()) {
// Bare array: single page, no meta.
page.items = doc;
page.total = (long)doc.as_array().size();
return page;
}
const body::Json* data = doc.find("data");
if (data && data->is_array()) page.items = *data;
const body::Json* meta = doc.find("meta");
if (meta && meta->is_object()) {
const body::Json* p = meta->find("page");
const body::Json* pp = meta->find("per_page");
const body::Json* t = meta->find("total");
const body::Json* cursor = meta->find("next_cursor");
if (p && p->is_number()) page.page = (int)p->as_number();
if (pp && pp->is_number()) page.per_page = (int)pp->as_number();
if (t && t->is_number()) page.total = (long)t->as_number();
if (cursor && cursor->is_string()) page.next_cursor = cursor->as_string();
}
const body::Json* links = doc.find("links");
if (links && links->is_object()) {
const body::Json* next = links->find("next");
if (next && next->is_string()) page.next_link = next->as_string();
}
return page;
}
Task<Page> Paginator::fetch_page(int page) {
if (page < 1) page = 1;
Url url = Url::parse(url_);
url.set_query_arg("page", std::to_string(page));
url.set_query_arg("per_page", std::to_string(per_page_));
for (const auto& kv : extra_) url.set_query_arg(kv.first, kv.second);
ClientResponse response = co_await client_.get(url.str());
raise_for_status(response);
co_return parse_envelope(response);
}
Page Paginator::fetch_page_blocking(int page) {
return block_on(fetch_page(page));
}
Task<std::vector<body::Json>> Paginator::fetch_all() {
std::vector<body::Json> all;
// Cursor style when the first page advertises it, else page numbers.
Page first = co_await fetch_page(1);
for (const auto& item : first.items.as_array()) all.push_back(item);
if (!first.next_cursor.empty() || !first.next_link.empty()) {
std::string cursor = first.next_cursor;
std::string link = first.next_link;
for (int hop = 1; hop < Caps::kPaginatorMaxPages; ++hop) {
if (cursor.empty() && link.empty()) break;
Url next_url = Url::parse(url_);
next_url.set_query_arg("cursor", cursor);
ClientResponse response =
co_await client_.get(!link.empty() ? link : next_url.str());
raise_for_status(response);
Page next = parse_envelope(response);
if (next.items.as_array().empty()) break;
for (const auto& item : next.items.as_array()) all.push_back(item);
cursor = next.next_cursor;
link = next.next_link;
}
co_return all;
}
if (first.items.as_array().empty()) co_return all;
long total = first.total;
if (total >= 0 && (long)all.size() >= total) co_return all;
for (int page = 2; page <= Caps::kPaginatorMaxPages; ++page) {
Page next = co_await fetch_page(page);
if (next.items.as_array().empty()) break;
for (const auto& item : next.items.as_array()) all.push_back(item);
if (total >= 0 && (long)all.size() >= total) break;
if ((int)next.items.as_array().size() < per_page_) break;
}
co_return all;
}
std::vector<body::Json> Paginator::fetch_all_blocking() {
return block_on(fetch_all());
}
} // namespace molo::clients::rest
+51
View File
@@ -0,0 +1,51 @@
#pragma once
// Pagination walker for the standard envelope:
// {data:[...], meta:{page,per_page,total}, links:{next}}.
// Supports page+per_page and cursor styles with static bounds.
#include <map>
#include <string>
#include <vector>
#include "clients/body/json.h"
#include "clients/client.h"
#include "clients/response.h"
#include "clients/task.h"
namespace molo::clients::rest {
struct Page {
body::Json items = body::Json::array(); // The data array.
int page = 1;
int per_page = 0;
long total = -1;
std::string next_cursor;
std::string next_link;
};
class Paginator {
public:
Paginator(Client& client, std::string collection_url);
Paginator& per_page(int n);
Paginator& query_arg(const std::string& name, const std::string& value);
// Fetches one page (1-based) and parses the envelope.
Task<Page> fetch_page(int page);
Page fetch_page_blocking(int page);
// Walks pages until data is empty or caps hit. Returns all items.
Task<std::vector<body::Json>> fetch_all();
std::vector<body::Json> fetch_all_blocking();
static Page parse_envelope(const ClientResponse& response);
private:
Client& client_;
std::string url_;
int per_page_ = 20;
std::map<std::string, std::string> extra_;
};
} // namespace molo::clients::rest
+111
View File
@@ -0,0 +1,111 @@
#include "clients/rest/resource.h"
#include <utility>
#include "clients/rest/idempotency.h"
#include "clients/url.h"
namespace molo::clients::rest {
Resource::Resource(Client& client, std::string collection)
: client_(client), collection_(std::move(collection)) {}
Resource::Resource(Client& client, std::string base_url,
std::string collection)
: client_(client),
base_url_(std::move(base_url)),
collection_(std::move(collection)) {}
Resource& Resource::idempotency(std::shared_ptr<IdempotencyStore> store) {
idempotency_ = std::move(store);
return *this;
}
std::string Resource::member_url(const std::string& id) const {
return base_url_ + collection_ + "/" + Url::encode_component(id);
}
std::string Resource::collection_url(
const std::map<std::string, std::string>& query) const {
std::string url = base_url_ + collection_;
bool first = true;
for (const auto& kv : query) {
url.push_back(first ? '?' : '&');
first = false;
url += Url::encode_component(kv.first);
url.push_back('=');
url += Url::encode_component(kv.second);
}
return url;
}
Task<ClientResponse> Resource::list(
const std::map<std::string, std::string>& query) {
co_return co_await client_.get(collection_url(query));
}
Task<ClientResponse> Resource::get(const std::string& id) {
co_return co_await client_.get(member_url(id));
}
Task<ClientResponse> Resource::create(const body::Json& doc) {
ClientRequest req = ClientRequest::post(collection_url());
req.json(doc);
req.idempotency_key(IdempotencyKey::generate());
co_return co_await client_.send(std::move(req));
}
Task<ClientResponse> Resource::create(const std::string& body,
const std::string& content_type) {
ClientRequest req = ClientRequest::post(collection_url());
req.bytes(body, content_type);
req.idempotency_key(IdempotencyKey::generate());
co_return co_await client_.send(std::move(req));
}
Task<ClientResponse> Resource::update(const std::string& id,
const body::Json& doc) {
ClientRequest req = ClientRequest::put(member_url(id));
req.json(doc);
co_return co_await client_.send(std::move(req));
}
Task<ClientResponse> Resource::patch(const std::string& id,
const body::Json& doc) {
ClientRequest req = ClientRequest::patch(member_url(id));
req.json(doc);
co_return co_await client_.send(std::move(req));
}
Task<ClientResponse> Resource::remove(const std::string& id) {
co_return co_await client_.remove(member_url(id));
}
ClientResponse Resource::list_blocking(
const std::map<std::string, std::string>& query) {
return block_on(list(query));
}
ClientResponse Resource::get_blocking(const std::string& id) {
return block_on(get(id));
}
ClientResponse Resource::create_blocking(const body::Json& doc) {
return block_on(create(doc));
}
ClientResponse Resource::update_blocking(const std::string& id,
const body::Json& doc) {
return block_on(update(id, doc));
}
ClientResponse Resource::patch_blocking(const std::string& id,
const body::Json& doc) {
return block_on(patch(id, doc));
}
ClientResponse Resource::remove_blocking(const std::string& id) {
return block_on(remove(id));
}
} // namespace molo::clients::rest
+57
View File
@@ -0,0 +1,57 @@
#pragma once
// Fluent CRUD helper over a plural-noun collection path.
#include <map>
#include <memory>
#include <string>
#include "clients/body/json.h"
#include "clients/client.h"
#include "clients/request.h"
#include "clients/response.h"
#include "clients/task.h"
namespace molo::clients::rest {
class IdempotencyStore;
class Resource {
public:
Resource(Client& client, std::string collection);
Resource(Client& client, std::string base_url, std::string collection);
Resource& idempotency(std::shared_ptr<IdempotencyStore> store);
const std::string& collection() const { return collection_; }
std::string member_url(const std::string& id) const;
std::string collection_url(
const std::map<std::string, std::string>& query = {}) const;
Task<ClientResponse> list(
const std::map<std::string, std::string>& query = {});
Task<ClientResponse> get(const std::string& id);
Task<ClientResponse> create(const body::Json& doc);
Task<ClientResponse> create(const std::string& body,
const std::string& content_type);
Task<ClientResponse> update(const std::string& id, const body::Json& doc);
Task<ClientResponse> patch(const std::string& id, const body::Json& doc);
Task<ClientResponse> remove(const std::string& id);
ClientResponse list_blocking(
const std::map<std::string, std::string>& query = {});
ClientResponse get_blocking(const std::string& id);
ClientResponse create_blocking(const body::Json& doc);
ClientResponse update_blocking(const std::string& id, const body::Json& doc);
ClientResponse patch_blocking(const std::string& id, const body::Json& doc);
ClientResponse remove_blocking(const std::string& id);
private:
Client& client_;
std::string base_url_;
std::string collection_;
std::shared_ptr<IdempotencyStore> idempotency_;
};
} // namespace molo::clients::rest