3#include <uwebsockets/App.h>
7#include <nlohmann/json.hpp>
17class MonitoringManager;
85template <
typename Res>
87 return res->writeHeader(
"Access-Control-Allow-Origin", corsOrigin);
107template <
typename Res>
109 return res->writeHeader(
"Access-Control-Expose-Headers",
"X-Wire-Us")
110 ->writeHeader(
"X-Wire-Us", std::to_string(wireUs.count()));
118template <
typename Res>
119void sendJson(Res* res, std::string_view corsOrigin,
const nlohmann::json& body) {
121 ->writeHeader(
"Content-Type",
"application/json")
122 ->end(body.dump(-1,
' ',
false, nlohmann::json::error_handler_t::replace));
133template <
typename Res>
134void sendBytes(Res* res, std::string_view corsOrigin, std::string_view contentType,
135 std::string_view body) {
136 setCorsOrigin(res, corsOrigin)->writeHeader(
"Content-Type", contentType)->end(body);
147template <
typename Res>
148void sendError(Res* res, std::string_view status, std::string_view corsOrigin,
149 std::string_view message,
150 std::optional<std::chrono::microseconds> wireUs = std::nullopt) {
151 auto* r =
setCorsOrigin(res->writeStatus(status), corsOrigin);
155 r->writeHeader(
"Content-Type",
"application/json")
156 ->end(nlohmann::json{{
"error", std::string(message)}}.dump());
164template <
typename Res>
165void sendStatus(Res* res, std::string_view status, std::string_view corsOrigin,
166 std::optional<std::chrono::microseconds> wireUs = std::nullopt) {
167 auto* r =
setCorsOrigin(res->writeStatus(status), corsOrigin);
Registers routes whose handlers run off the event loop.
Definition router.h:274
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:287
Owns the active monitorings and turns each into a lossless stream of recorded rows.
Definition monitoring_manager.h:62
HTTP-transport glue shared by the built-in server and route plug-in libs.
Definition router.cc:14
Res * setWireTime(Res *res, std::chrono::microseconds wireUs)
Attaches the server-measured wire-time header (X-Wire-Us, microseconds) to a response.
Definition web_api.h:108
void sendStatus(Res *res, std::string_view status, std::string_view corsOrigin, std::optional< std::chrono::microseconds > wireUs=std::nullopt)
Writes a bare status response (no body) with the CORS header.
Definition web_api.h:165
void sendJson(Res *res, std::string_view corsOrigin, const nlohmann::json &body)
Writes body as a 200 application/json response with the CORS header.
Definition web_api.h:119
Res * setCorsOrigin(Res *res, std::string_view corsOrigin)
Writes the Access-Control-Allow-Origin header and returns res for chaining.
Definition web_api.h:86
std::function< void(Router &router, const RouteContext &ctx)> RegisterRoutesFn
A route plug-in: registers its routes on the HTTP app.
Definition web_api.h:77
void sendBytes(Res *res, std::string_view corsOrigin, std::string_view contentType, std::string_view body)
Writes body verbatim as a 200 response with the given contentType and the CORS header.
Definition web_api.h:134
void sendError(Res *res, std::string_view status, std::string_view corsOrigin, std::string_view message, std::optional< std::chrono::microseconds > wireUs=std::nullopt)
Writes a status response carrying a {"error": message} JSON body and the CORS header.
Definition web_api.h:148
Definition bus_health_source.h:7
Routes whose handlers cannot block the event loop, because they do not run on it.
The live collaborators a route plug-in needs, handed to it at registration time.
Definition web_api.h:51
mm::node::MonitoringManager & monitoringManager
Monitoring registry (for monitoring-aware plug-ins).
Definition web_api.h:55
std::string_view corsOrigin
Definition web_api.h:60
mm::node::DeviceManager & deviceManager
Device list + SDO/PDO/state/bus access.
Definition web_api.h:53