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:
@@ -0,0 +1,63 @@
|
||||
#pragma once
|
||||
// Spec: molo::app::middleware::Chain. Composes middleware in registration
|
||||
// order around a terminal handler. Category order is enforced at use():
|
||||
// adding a layer with a lower category than the previous one throws
|
||||
// ChainOrderError (startup error, R7). Layer count capped at 64.
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "../async/task.h"
|
||||
#include "../caps.h"
|
||||
#include "../http_msg/request.h"
|
||||
#include "../http_msg/response.h"
|
||||
#include "middleware.h"
|
||||
|
||||
namespace molo::app::middleware {
|
||||
|
||||
class ChainOrderError : public std::runtime_error {
|
||||
public:
|
||||
explicit ChainOrderError(const std::string& msg) : std::runtime_error(msg) {}
|
||||
};
|
||||
|
||||
class Chain {
|
||||
public:
|
||||
void use(std::shared_ptr<Middleware> layer) {
|
||||
if (layers_.size() >= caps::kMaxMiddlewareLayers) {
|
||||
throw ChainOrderError("too many middleware layers");
|
||||
}
|
||||
if (!layers_.empty() && layer->category() < layers_.back()->category()) {
|
||||
throw ChainOrderError(
|
||||
"middleware order violation: '" + layer->name() + "' (" +
|
||||
category_name(layer->category()) + ") registered after '" +
|
||||
layers_.back()->name() + "' (" + category_name(layers_.back()->category()) + ")");
|
||||
}
|
||||
layers_.push_back(std::move(layer));
|
||||
}
|
||||
|
||||
async::Task<http_msg::Response> run(
|
||||
http_msg::Request& req,
|
||||
std::function<async::Task<http_msg::Response>(http_msg::Request&)> terminal) {
|
||||
std::function<async::Task<http_msg::Response>(http_msg::Request&)> next = terminal;
|
||||
for (std::size_t i = layers_.size(); i-- > 0;) {
|
||||
std::shared_ptr<Middleware> layer = layers_[i];
|
||||
auto inner = next;
|
||||
next = [layer, inner](http_msg::Request& r) -> async::Task<http_msg::Response> {
|
||||
Next guard(inner);
|
||||
co_return co_await layer->handle(r, guard);
|
||||
};
|
||||
}
|
||||
co_return co_await next(req);
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<Middleware>>& layers() const { return layers_; }
|
||||
std::size_t size() const { return layers_.size(); }
|
||||
|
||||
private:
|
||||
std::vector<std::shared_ptr<Middleware>> layers_;
|
||||
};
|
||||
|
||||
} // namespace molo::app::middleware
|
||||
Reference in New Issue
Block a user