feat(workflow): example stats page with embedded Chart.js, inline form validation errors

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-24 01:59:27 +02:00
co-authored by Claude Sonnet 5.5
parent 16a0bb0de4
commit 00af65613d
36 changed files with 1274 additions and 138 deletions
+31 -1
View File
@@ -1,5 +1,6 @@
#include "App.hpp"
#include <chrono>
#include <stdexcept>
#include "Caps.hpp"
@@ -32,7 +33,35 @@ void Application::mount(const std::string& path,
mounts_.emplace_back(path, std::move(sub));
}
namespace {
// Pattern of the route matched for the request now being served on this
// thread; read once by dispatch() for the stats sample.
thread_local std::string g_matched_route;
} // namespace
void Application::observe(std::shared_ptr<Stats> stats) {
stats_ = std::move(stats);
}
Response Application::dispatch(Request req) {
if (!stats_) return dispatch_untimed(std::move(req));
g_matched_route.clear();
RequestSample sample;
sample.method = req.method;
sample.bytes_in = req.body.size();
auto start = std::chrono::steady_clock::now();
Response res = dispatch_untimed(std::move(req));
sample.micros = (long)std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - start)
.count();
sample.route = g_matched_route;
sample.status = res.status;
sample.bytes_out = res.body.size();
stats_->record_request(sample);
return res;
}
Response Application::dispatch_untimed(Request req) {
if (req.body.size() > caps::kMaxRequestBody)
return TextResponse("body too large", 413);
try {
@@ -77,7 +106,8 @@ Response Application::dispatch_inner(Request req, int depth) {
return target->run_chain(std::move(req), depth + 1);
}
Handler handler;
if (router_.match(req, handler)) return handler(std::move(req));
if (router_.match(req, handler, &g_matched_route))
return handler(std::move(req));
bool path_exists = false;
for (const auto& r : router_.routes()) {
std::map<std::string, std::string> params;
+7
View File
@@ -12,6 +12,7 @@
#include "Http.hpp"
#include "Router.hpp"
#include "Stats.hpp"
namespace molo::example::fw {
@@ -46,7 +47,10 @@ class Application : public std::enable_shared_from_this<Application> {
const std::string& name_prefix = "");
void mount(const std::string& path, std::shared_ptr<Application> sub);
// Outermost entry. Records one RequestSample per call when a Stats sink
// is attached with observe().
Response dispatch(Request req);
void observe(std::shared_ptr<Stats> stats);
const std::vector<Route>& routes() const;
@@ -54,6 +58,9 @@ class Application : public std::enable_shared_from_this<Application> {
Response dispatch_inner(Request req, int depth);
Response run_chain(Request req, int depth);
Response dispatch_untimed(Request req);
std::shared_ptr<Stats> stats_;
Router router_;
std::vector<std::pair<MiddlewareSlot, Middleware>> middlewares_;
std::vector<std::pair<std::string, std::shared_ptr<Application>>> mounts_;
+10
View File
@@ -269,4 +269,14 @@ std::string WebdavStore::propfind_xml(const std::string& root) const {
return out + "</D:multistatus>";
}
std::size_t SessionStore::size() const {
std::lock_guard<std::mutex> lock(mu_);
return sessions_.size();
}
std::size_t WebdavStore::size() const {
std::lock_guard<std::mutex> lock(mu_);
return files_.size();
}
} // namespace molo::example::fw
+2
View File
@@ -20,6 +20,7 @@ class SessionStore {
void put(const std::string& id, const std::string& key,
const std::string& value);
void rotate(const std::string& old_id, std::string& new_id);
std::size_t size() const;
private:
mutable std::mutex mu_;
@@ -104,6 +105,7 @@ class WebdavStore {
bool get(const std::string& path, std::string& out) const;
bool remove(const std::string& path);
std::string propfind_xml(const std::string& root) const;
std::size_t size() const;
private:
mutable std::mutex mu_;
+16 -1
View File
@@ -39,15 +39,30 @@ void Cache::put(const std::string& key, const std::string& value, long ttl_sec,
bool Cache::lookup(const std::string& key, std::string& out, long now) {
std::lock_guard<std::mutex> lock(mu_);
auto it = data_.find(key);
if (it == data_.end()) return false;
if (it == data_.end()) {
++misses_;
return false;
}
if (it->second.expires_at <= now) {
data_.erase(it);
++misses_;
return false;
}
out = it->second.value;
++hits_;
return true;
}
long Cache::hits() const {
std::lock_guard<std::mutex> lock(mu_);
return hits_;
}
long Cache::misses() const {
std::lock_guard<std::mutex> lock(mu_);
return misses_;
}
void Cache::invalidate(const std::string& key) {
std::lock_guard<std::mutex> lock(mu_);
data_.erase(key);
+4
View File
@@ -37,10 +37,14 @@ class Cache {
bool lookup(const std::string& key, std::string& out, long now);
void invalidate(const std::string& key);
std::size_t size() const;
long hits() const;
long misses() const;
private:
mutable std::mutex mu_;
std::map<std::string, CacheEntry> data_;
long hits_ = 0;
long misses_ = 0;
};
using PubsubHandler = std::function<void(const std::string& message)>;
+5
View File
@@ -47,6 +47,11 @@ double Metrics::get(const std::string& name) const {
return it == values_.end() ? 0.0 : it->second;
}
std::map<std::string, double> Metrics::snapshot() const {
std::lock_guard<std::mutex> lock(mu_);
return values_;
}
std::string Metrics::render_prometheus() const {
std::lock_guard<std::mutex> lock(mu_);
std::string out;
+1
View File
@@ -42,6 +42,7 @@ class Metrics {
void gauge(const std::string& name, double value);
double get(const std::string& name) const;
std::string render_prometheus() const;
std::map<std::string, double> snapshot() const;
private:
mutable std::mutex mu_;
+2 -1
View File
@@ -104,7 +104,7 @@ void Router::include_router(const std::string& prefix, const Router& other,
}
}
bool Router::match(Request& req, Handler& out) const {
bool Router::match(Request& req, Handler& out, std::string* pattern) const {
for (const auto& r : routes_) {
bool method_ok = false;
for (Method m : r.methods) {
@@ -118,6 +118,7 @@ bool Router::match(Request& req, Handler& out) const {
if (match_pattern(r.pattern, req.path, params)) {
for (auto& [k, v] : params) req.path_params[k] = v;
out = r.handler;
if (pattern) *pattern = r.pattern;
return true;
}
}
+1 -1
View File
@@ -38,7 +38,7 @@ class Router {
void include_router(const std::string& prefix, const Router& other,
const std::string& name_prefix = "");
bool match(Request& req, Handler& out) const;
bool match(Request& req, Handler& out, std::string* pattern = nullptr) const;
const std::vector<Route>& routes() const;
+30 -4
View File
@@ -5,6 +5,7 @@
#include <sys/socket.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <sstream>
@@ -18,6 +19,11 @@ Server::Server(std::shared_ptr<Application> app) : app_(std::move(app)) {
Server::~Server() { stop(); }
void Server::set_stats(std::shared_ptr<Stats> stats) {
stats_ = std::move(stats);
if (stats_) stats_->set_connection_cap(caps::kMaxConnections);
}
bool Server::start(const std::string& host, int port) {
if (running_) return false;
listen_fd_ = ::socket(AF_INET, SOCK_STREAM, 0);
@@ -87,28 +93,44 @@ void Server::loop() {
struct timeval tv{};
tv.tv_sec = caps::kServerTimeoutSec;
::setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
handle_one(fd);
if (stats_) stats_->connection_opened();
std::size_t bytes_in = 0, bytes_out = 0;
ConnEnd how = ConnEnd::Closed;
handle_one(fd, bytes_in, bytes_out, how);
::close(fd);
if (stats_) stats_->connection_closed(how, bytes_in, bytes_out);
++served;
}
// The accept loop stops at the cap; the listener stays open but unserved.
if (stats_ && served >= caps::kMaxConnections) stats_->connection_rejected();
}
void Server::handle_one(int fd) {
void Server::handle_one(int fd, std::size_t& bytes_in,
std::size_t& bytes_out, ConnEnd& how) {
std::string raw;
char buf[4096];
while (raw.size() < caps::kMaxHeaderBytes) {
ssize_t n = ::recv(fd, buf, sizeof(buf), 0);
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
how = ConnEnd::Timeout;
if (n <= 0) break;
bytes_in += (std::size_t)n;
raw.append(buf, (std::size_t)n);
if (raw.find("\r\n\r\n") != std::string::npos) break;
}
std::size_t hdr_end = raw.find("\r\n\r\n");
if (hdr_end == std::string::npos) return;
if (hdr_end == std::string::npos) {
if (how == ConnEnd::Closed) how = ConnEnd::Malformed;
return;
}
std::string head = raw.substr(0, hdr_end);
std::string rest = raw.substr(hdr_end + 4);
std::istringstream lines(head);
std::string line;
if (!std::getline(lines, line)) return;
if (!std::getline(lines, line)) {
how = ConnEnd::Malformed;
return;
}
if (!line.empty() && line.back() == '\r') line.pop_back();
std::istringstream rl(line);
std::string method_s, target, version;
@@ -148,7 +170,10 @@ void Server::handle_one(int fd) {
while (req.body.size() < content_length &&
req.body.size() < caps::kMaxRequestBody) {
ssize_t n = ::recv(fd, buf, sizeof(buf), 0);
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
how = ConnEnd::Timeout;
if (n <= 0) break;
bytes_in += (std::size_t)n;
req.body.append(buf, (std::size_t)n);
}
Response res = app_->dispatch(std::move(req));
@@ -172,6 +197,7 @@ void Server::handle_one(int fd) {
ssize_t n = ::send(fd, wire.data() + sent, wire.size() - sent, 0);
if (n <= 0) break;
sent += (std::size_t)n;
bytes_out += (std::size_t)n;
}
}
+5 -1
View File
@@ -22,6 +22,8 @@ class Server {
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
// Optional sink for connection counters; set before start().
void set_stats(std::shared_ptr<Stats> stats);
bool start(const std::string& host, int port);
void stop();
int port() const;
@@ -29,9 +31,11 @@ class Server {
private:
void loop();
void handle_one(int fd);
void handle_one(int fd, std::size_t& bytes_in, std::size_t& bytes_out,
ConnEnd& how);
std::shared_ptr<Application> app_;
std::shared_ptr<Stats> stats_;
int listen_fd_ = -1;
int port_ = 0;
std::atomic<bool> running_{false};
+218
View File
@@ -0,0 +1,218 @@
#include "Stats.hpp"
#include <algorithm>
#include <cstdio>
namespace molo::example::fw {
namespace {
// Upper bounds in microseconds; the last bucket is open-ended.
constexpr std::array<long, 7> kBounds = {500, 1000, 5000, 10000,
50000, 100000, 500000};
std::string quote(const std::string& s) { return "\"" + json_escape(s) + "\""; }
} // namespace
Stats::Stats() {
ring_.reserve(stats_caps::kLatencyRing);
started_ = now();
}
void Stats::set_clock(long (*now_sec)()) {
std::lock_guard<std::mutex> lock(mu_);
clock_ = now_sec;
}
long Stats::now() const {
if (clock_) return clock_();
return (long)std::chrono::duration_cast<std::chrono::seconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
}
Stats::Bucket& Stats::bucket_for(long slot) {
Bucket& b = buckets_[(std::size_t)(slot % (long)stats_caps::kBuckets)];
if (b.slot != slot) {
b = Bucket{};
b.slot = slot;
}
return b;
}
void Stats::record_request(const RequestSample& s) {
std::lock_guard<std::mutex> lock(mu_);
const bool failed = s.status >= 400;
++requests_;
if (failed) ++errors_;
bytes_in_ += (long)s.bytes_in;
bytes_out_ += (long)s.bytes_out;
micros_ += s.micros;
++by_method_[method_name(s.method)];
++by_status_[s.status];
std::string key = s.route.empty() ? "(unmatched)" : s.route;
if (!by_route_.count(key) && by_route_.size() >= stats_caps::kMaxRoutes)
key = "(other)";
RouteStat& r = by_route_[key];
++r.count;
if (failed) ++r.errors;
r.micros += s.micros;
r.max_micros = std::max(r.max_micros, s.micros);
r.bytes_out += (long)s.bytes_out;
std::size_t h = 0;
while (h < kBounds.size() && s.micros >= kBounds[h]) ++h;
++histogram_[h];
if (ring_.size() < stats_caps::kLatencyRing) {
ring_.push_back(s.micros);
} else {
ring_[ring_next_] = s.micros;
ring_next_ = (ring_next_ + 1) % stats_caps::kLatencyRing;
}
Bucket& b = bucket_for(now() / stats_caps::kBucketSec);
++b.requests;
if (failed) ++b.errors;
b.bytes_in += (long)s.bytes_in;
b.bytes_out += (long)s.bytes_out;
b.micros += s.micros;
}
void Stats::connection_opened() {
std::lock_guard<std::mutex> lock(mu_);
++conn_total_;
++conn_active_;
conn_peak_ = std::max(conn_peak_, conn_active_);
++bucket_for(now() / stats_caps::kBucketSec).connections;
}
void Stats::connection_closed(ConnEnd how, std::size_t in, std::size_t out) {
std::lock_guard<std::mutex> lock(mu_);
if (conn_active_ > 0) --conn_active_;
if (how == ConnEnd::Timeout) ++conn_timeout_;
if (how == ConnEnd::Malformed) ++conn_malformed_;
conn_bytes_in_ += (long)in;
conn_bytes_out_ += (long)out;
}
void Stats::connection_rejected() {
std::lock_guard<std::mutex> lock(mu_);
++conn_rejected_;
}
void Stats::set_connection_cap(long cap) {
std::lock_guard<std::mutex> lock(mu_);
conn_cap_ = cap;
}
long Stats::total_requests() const {
std::lock_guard<std::mutex> lock(mu_);
return requests_;
}
long Stats::total_connections() const {
std::lock_guard<std::mutex> lock(mu_);
return conn_total_;
}
long Stats::active_connections() const {
std::lock_guard<std::mutex> lock(mu_);
return conn_active_;
}
std::string Stats::to_json() const {
std::lock_guard<std::mutex> lock(mu_);
const long t = now();
auto num = [](long v) { return std::to_string(v); };
auto dbl = [](double v) {
char buf[48];
std::snprintf(buf, sizeof(buf), "%.3f", v);
return std::string(buf);
};
std::string out = "{";
out += "\"generated_at\":" + num(t) + ",\"uptime_sec\":" + num(t - started_);
out += ",\"totals\":{\"requests\":" + num(requests_) +
",\"errors\":" + num(errors_) + ",\"bytes_in\":" + num(bytes_in_) +
",\"bytes_out\":" + num(bytes_out_) + ",\"avg_ms\":" +
dbl(requests_ ? micros_ / 1000.0 / (double)requests_ : 0.0) + "}";
out += ",\"by_method\":{";
bool first = true;
for (const auto& [k, v] : by_method_) {
out += (first ? "" : ",") + quote(k) + ":" + num(v);
first = false;
}
out += "},\"by_status\":{";
first = true;
for (const auto& [k, v] : by_status_) {
out += (first ? "" : ",") + quote(std::to_string(k)) + ":" + num(v);
first = false;
}
out += "},\"by_route\":[";
std::vector<std::pair<std::string, RouteStat>> routes(by_route_.begin(),
by_route_.end());
std::sort(routes.begin(), routes.end(), [](const auto& a, const auto& b) {
return a.second.count > b.second.count;
});
first = true;
for (const auto& [k, r] : routes) {
out += std::string(first ? "" : ",") + "{\"route\":" + quote(k) +
",\"count\":" + num(r.count) + ",\"errors\":" + num(r.errors) +
",\"avg_ms\":" + dbl(r.micros / 1000.0 / (double)r.count) +
",\"max_ms\":" + dbl(r.max_micros / 1000.0) +
",\"bytes_out\":" + num(r.bytes_out) + "}";
first = false;
}
out += "]";
static const char* kLabels[8] = {"<0.5ms", "<1ms", "<5ms", "<10ms",
"<50ms", "<100ms", "<500ms", ">=500ms"};
out += ",\"latency\":{\"histogram\":[";
for (std::size_t i = 0; i < histogram_.size(); ++i)
out += std::string(i ? "," : "") + "{\"bucket\":" + quote(kLabels[i]) +
",\"count\":" + num(histogram_[i]) + "}";
std::vector<long> sorted = ring_;
std::sort(sorted.begin(), sorted.end());
auto pct = [&](double p) {
if (sorted.empty()) return 0.0;
std::size_t idx = (std::size_t)(p * (double)(sorted.size() - 1));
return sorted[idx] / 1000.0;
};
out += "],\"p50_ms\":" + dbl(pct(0.50)) + ",\"p95_ms\":" + dbl(pct(0.95)) +
",\"p99_ms\":" + dbl(pct(0.99)) + ",\"samples\":" +
num((long)sorted.size()) + "}";
out += ",\"connections\":{\"total\":" + num(conn_total_) +
",\"active\":" + num(conn_active_) + ",\"peak\":" + num(conn_peak_) +
",\"timeouts\":" + num(conn_timeout_) + ",\"malformed\":" +
num(conn_malformed_) + ",\"rejected\":" + num(conn_rejected_) +
",\"cap\":" + num(conn_cap_) + ",\"bytes_in\":" +
num(conn_bytes_in_) + ",\"bytes_out\":" + num(conn_bytes_out_) + "}";
// Oldest first, one entry per bucket slot including empty ones.
const long cur = t / stats_caps::kBucketSec;
const long n = (long)stats_caps::kBuckets;
out += ",\"series\":{\"bucket_sec\":" + num(stats_caps::kBucketSec) +
",\"points\":[";
for (long i = n - 1; i >= 0; --i) {
const long slot = cur - i;
const Bucket& b = buckets_[(std::size_t)(slot % n)];
const bool live = b.slot == slot;
const long reqs = live ? b.requests : 0;
out += std::string(i == n - 1 ? "" : ",") + "{\"t\":" +
num(slot * stats_caps::kBucketSec) +
",\"requests\":" + num(reqs) +
",\"errors\":" + num(live ? b.errors : 0) +
",\"bytes_in\":" + num(live ? b.bytes_in : 0) +
",\"bytes_out\":" + num(live ? b.bytes_out : 0) +
",\"avg_ms\":" + dbl(reqs ? b.micros / 1000.0 / (double)reqs : 0.0) +
",\"connections\":" + num(live ? b.connections : 0) + "}";
}
out += "]}}";
return out;
}
} // namespace molo::example::fw
+108
View File
@@ -0,0 +1,108 @@
#pragma once
// Request and connection statistics behind /stats. One mutex, fixed-size
// storage, static caps: 90 ten-second buckets (15 min of history), 256
// distinct routes, a 1024-sample latency ring. Everything past a cap is
// counted under "(other)" or dropped oldest-first, never grown.
// Gap: real export (Prometheus, OTel) lives in 02-observability.md.
#include <array>
#include <chrono>
#include <cstddef>
#include <map>
#include <mutex>
#include <string>
#include <vector>
#include "Http.hpp"
namespace molo::example::fw {
namespace stats_caps {
inline constexpr std::size_t kBuckets = 90;
inline constexpr long kBucketSec = 10;
inline constexpr std::size_t kMaxRoutes = 256;
inline constexpr std::size_t kLatencyRing = 1024;
} // namespace stats_caps
struct RequestSample {
Method method = Method::Get;
std::string route; // matched pattern, empty when no route matched
int status = 200;
std::size_t bytes_in = 0;
std::size_t bytes_out = 0;
long micros = 0;
};
enum class ConnEnd { Closed, Timeout, Malformed };
class Stats {
public:
Stats();
void record_request(const RequestSample& sample);
void connection_opened();
void connection_closed(ConnEnd how, std::size_t bytes_in,
std::size_t bytes_out);
void connection_rejected();
void set_connection_cap(long cap);
// Test hook: current time in seconds; defaults to the wall clock.
void set_clock(long (*now_sec)());
// JSON document consumed by the /stats page: totals, by_method, by_status,
// by_route, latency histogram + percentiles, connections, time series.
std::string to_json() const;
long total_requests() const;
long total_connections() const;
long active_connections() const;
private:
struct Bucket {
long slot = -1; // epoch seconds / kBucketSec; -1 = unused
long requests = 0;
long errors = 0;
long bytes_in = 0;
long bytes_out = 0;
long micros = 0;
long connections = 0;
};
struct RouteStat {
long count = 0;
long errors = 0;
long micros = 0;
long max_micros = 0;
long bytes_out = 0;
};
long now() const;
Bucket& bucket_for(long slot);
mutable std::mutex mu_;
long (*clock_)() = nullptr;
long started_ = 0;
long requests_ = 0;
long errors_ = 0;
long bytes_in_ = 0;
long bytes_out_ = 0;
long micros_ = 0;
std::map<std::string, long> by_method_;
std::map<int, long> by_status_;
std::map<std::string, RouteStat> by_route_;
std::array<long, 8> histogram_{};
std::vector<long> ring_;
std::size_t ring_next_ = 0;
std::array<Bucket, stats_caps::kBuckets> buckets_{};
long conn_total_ = 0;
long conn_active_ = 0;
long conn_peak_ = 0;
long conn_timeout_ = 0;
long conn_malformed_ = 0;
long conn_rejected_ = 0;
long conn_cap_ = 0;
long conn_bytes_in_ = 0;
long conn_bytes_out_ = 0;
};
} // namespace molo::example::fw
+51 -10
View File
@@ -129,6 +129,43 @@ std::string Environment::render_source(const std::string& name,
return render_blocks(src, ctx, depth);
}
namespace {
// Position of the "{% <close> %}" matching an already-open tag, honoring
// nested "{% <open> ... %}" tags of the same kind. When else_pos is given it
// receives the depth-zero "{% else %}" position (npos if none).
std::size_t find_matching(const std::string& src, std::size_t from,
const std::string& open, const std::string& close,
std::size_t* else_pos = nullptr) {
int depth = 0;
if (else_pos) *else_pos = std::string::npos;
std::size_t i = from;
while (true) {
std::size_t t = src.find("{%", i);
if (t == std::string::npos) return std::string::npos;
std::size_t e = src.find("%}", t);
if (e == std::string::npos) return std::string::npos;
std::size_t a = t + 2;
while (a < e && src[a] == ' ') ++a;
std::size_t b = e;
while (b > a && src[b - 1] == ' ') --b;
std::string tag = src.substr(a, b - a);
if (tag.rfind(open + " ", 0) == 0) {
++depth;
} else if (tag == close) {
if (depth == 0) return t;
--depth;
} else if (tag == "else" && depth == 0 && else_pos &&
*else_pos == std::string::npos) {
*else_pos = t;
}
i = e + 2;
}
}
} // namespace
std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
int depth) const {
std::string out;
@@ -185,16 +222,20 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
std::string tag = trim(src.substr(t + 2, e - t - 2));
if (tag.rfind("if ", 0) == 0) {
std::string cond = trim(tag.substr(3));
std::size_t end = src.find("{% endif %}", e);
std::size_t els_pos = std::string::npos;
std::size_t end = find_matching(src, e + 2, "if", "endif", &els_pos);
if (end == std::string::npos) break;
std::string inner = src.substr(e + 2, end - e - 2);
std::size_t els = inner.find("{% else %}");
std::string yes = els == std::string::npos ? inner
: inner.substr(0, els);
std::string no =
els == std::string::npos ? "" : inner.substr(els + 10);
std::size_t close_end = src.find("%}", end) + 2;
std::string yes, no;
if (els_pos == std::string::npos) {
yes = src.substr(e + 2, end - e - 2);
} else {
yes = src.substr(e + 2, els_pos - e - 2);
no = src.substr(src.find("%}", els_pos) + 2,
end - src.find("%}", els_pos) - 2);
}
out += render_blocks(ctx_truthy(ctx, cond) ? yes : no, ctx, depth + 1);
i = end + 11;
i = close_end;
} else if (tag.rfind("for ", 0) == 0) {
std::string rest = trim(tag.substr(4));
std::size_t in = rest.find(" in ");
@@ -204,7 +245,7 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
std::string list = in == std::string::npos
? ""
: trim(rest.substr(in + 4));
std::size_t end = src.find("{% endfor %}", e);
std::size_t end = find_matching(src, e + 2, "for", "endfor");
if (end == std::string::npos) break;
std::string inner = src.substr(e + 2, end - e - 2);
auto it = ctx.find(list);
@@ -220,7 +261,7 @@ std::string Environment::render_blocks(const std::string& src, TemplateCtx& ctx,
}
}
}
i = end + 12;
i = src.find("%}", end) + 2;
} else if (tag.rfind("include ", 0) == 0) {
std::string rest = trim(tag.substr(8));
if (rest.size() >= 2 && rest.front() == '"') {
+18 -7
View File
@@ -15,6 +15,12 @@ const std::vector<FieldError>& ValidationError::failures() const {
return failures_;
}
std::map<std::string, std::string> ValidationError::by_field() const {
std::map<std::string, std::string> out;
for (const auto& f : failures_) out.emplace(f.field, f.message);
return out;
}
std::string ValidationError::to_json() const {
std::string out = "{\"error\":\"validation_error\",\"failures\":[";
bool first = true;
@@ -70,30 +76,35 @@ std::map<std::string, std::string> Model::validate(
if (!f.default_value.empty()) clean[f.name] = f.default_value;
continue;
}
failures.push_back({f.name, "field is required"});
failures.push_back({f.name, "is required"});
continue;
}
if (f.type != FieldType::Integer && value.size() < f.min_len)
failures.push_back({f.name, "too short"});
failures.push_back(
{f.name, "must be at least " + std::to_string(f.min_len) +
" characters"});
else if (f.type != FieldType::Integer && value.size() > f.max_len)
failures.push_back({f.name, "too long"});
failures.push_back(
{f.name, "must be at most " + std::to_string(f.max_len) +
" characters"});
else if (f.type == FieldType::Integer) {
long n = 0;
if (!parse_long(value, n)) {
failures.push_back({f.name, "not an integer"});
failures.push_back({f.name, "must be a whole number"});
} else if (f.has_range && (n < f.min_value || n > f.max_value)) {
failures.push_back({f.name, "out of range"});
failures.push_back({f.name, "must be between " + std::to_string(f.min_value) +
" and " + std::to_string(f.max_value)});
} else {
clean[f.name] = value;
}
continue;
} else if (f.type == FieldType::Email && !looks_like_email(value)) {
failures.push_back({f.name, "not an email address"});
failures.push_back({f.name, "must be a valid email address"});
continue;
} else if (f.type == FieldType::Boolean &&
value != "true" && value != "false" && value != "1" &&
value != "0") {
failures.push_back({f.name, "not a boolean"});
failures.push_back({f.name, "must be true or false"});
continue;
} else {
clean[f.name] = value;
+2
View File
@@ -38,6 +38,8 @@ class ValidationError : public HttpError {
explicit ValidationError(std::vector<FieldError> failures);
const std::vector<FieldError>& failures() const;
std::string to_json() const;
// First failure per field, for inline display next to each input.
std::map<std::string, std::string> by_field() const;
private:
std::vector<FieldError> failures_;