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:
+31
-1
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user