Motion Master 6.0.0-alpha.86
Next-generation motion control software
Loading...
Searching...
No Matches
web_api.h
Go to the documentation of this file.
1#pragma once
2
3#include <uwebsockets/App.h>
4
5#include <chrono>
6#include <functional>
7#include <nlohmann/json.hpp>
8#include <optional>
9#include <string>
10#include <string_view>
11#include <utility>
12
13#include "api/router.h"
14
15namespace mm::node {
16class DeviceManager;
17class MonitoringManager;
18} // namespace mm::node
19
34namespace mm::api {
35
62
77using RegisterRoutesFn = std::function<void(Router& router, const RouteContext& ctx)>;
78
85template <typename Res>
86Res* setCorsOrigin(Res* res, std::string_view corsOrigin) {
87 return res->writeHeader("Access-Control-Allow-Origin", corsOrigin);
88}
89
107template <typename Res>
108Res* setWireTime(Res* res, std::chrono::microseconds wireUs) {
109 return res->writeHeader("Access-Control-Expose-Headers", "X-Wire-Us")
110 ->writeHeader("X-Wire-Us", std::to_string(wireUs.count()));
111}
112
118template <typename Res>
119void sendJson(Res* res, std::string_view corsOrigin, const nlohmann::json& body) {
120 setCorsOrigin(res, corsOrigin)
121 ->writeHeader("Content-Type", "application/json")
122 ->end(body.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace));
123}
124
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);
137}
138
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);
152 if (wireUs) {
153 r = setWireTime(r, *wireUs);
154 }
155 r->writeHeader("Content-Type", "application/json")
156 ->end(nlohmann::json{{"error", std::string(message)}}.dump());
157}
158
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);
168 if (wireUs) {
169 r = setWireTime(r, *wireUs);
170 }
171 r->end();
172}
173
174} // namespace mm::api
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