Motion Master 6.0.0-alpha.50
Next-generation motion control software
Loading...
Searching...
No Matches
http_server.h
Go to the documentation of this file.
1#pragma once
2
3#include <uwebsockets/App.h>
4
5#include <atomic>
6#include <expected>
7#include <functional>
8#include <future>
9#include <string>
10#include <thread>
11#include <vector>
12
13#include "api/web_api.h"
14#include "game_loop.h" // GameLoopHealth (returned by the GET /api/game-loop callback)
15
16namespace mm::node {
17class DeviceManager;
19} // namespace mm::node
20
30 public:
39 std::function<std::expected<void, std::string>(std::string driver, std::string adapter)>;
40
45 using GetLogFn = std::function<std::vector<std::string>()>;
46
53 using RefreshCertFn = std::function<std::expected<void, std::string>()>;
54
61 using GetGameLoopHealthFn = std::function<GameLoopHealth()>;
62
71 using SetGameLoopPeriodFn = std::function<std::expected<void, std::string>(uint32_t periodUs)>;
72
74 struct Config {
75 uint16_t port = 61447;
76 std::string certFile;
77 std::string keyFile;
78 std::string version;
79 std::string
90 std::string corsOrigin{"https://motion-master.synapticon.com"};
91 };
92
98 HttpServer(Config config, mm::node::DeviceManager& deviceManager,
99 mm::node::MonitoringManager& monitoringManager);
100
102 ~HttpServer();
103
112 bool start();
113
117 void stop();
118
129
130 private:
131 void run();
132
133 Config config_;
134 mm::node::DeviceManager& deviceManager_;
135 mm::node::MonitoringManager& monitoringManager_;
136 std::vector<mm::api::RegisterRoutesFn> routeModules_;
137 std::atomic<bool> running_{false};
138 std::thread thread_;
139 std::atomic<uWS::Loop*> loop_{nullptr};
140 std::atomic<uWS::SSLApp*> app_{nullptr};
142 std::promise<bool> listenResult_;
143};
HTTPS REST API server.
Definition http_server.h:29
std::function< std::expected< void, std::string >(uint32_t periodUs)> SetGameLoopPeriodFn
Callback type for PUT /api/game-loop.
Definition http_server.h:71
~HttpServer()
Destructor. Calls stop() if the server is still running.
Definition http_server.cc:277
std::function< std::expected< void, std::string >(std::string driver, std::string adapter)> InitDeviceManagerFn
Callback type for POST /api/init.
Definition http_server.h:39
void addRoutes(mm::api::RegisterRoutesFn module)
Registers a route plug-in to be wired in when the server starts.
Definition http_server.cc:285
std::function< std::vector< std::string >()> GetLogFn
Callback type for GET /api/log.
Definition http_server.h:45
std::function< std::expected< void, std::string >()> RefreshCertFn
Callback type for POST /api/cert/refresh.
Definition http_server.h:53
void stop()
Stops the server, closes the listen socket, and joins the thread.
Definition http_server.cc:304
std::function< GameLoopHealth()> GetGameLoopHealthFn
Callback type for GET /api/game-loop.
Definition http_server.h:61
bool start()
Starts the server background thread and begins accepting connections.
Definition http_server.cc:293
Owns the fieldbus driver and node collection, and drives PDO exchange.
Definition device_manager.h:183
Owns the active monitorings and turns each into a lossless stream of recorded rows.
Definition monitoring_manager.h:46
std::function< void(uWS::SSLApp &app, const RouteContext &ctx)> RegisterRoutesFn
A route plug-in: registers its routes on the HTTP app.
Definition web_api.h:57
Definition http_server.h:16
The whole config file. Top-level keys map to these members.
Definition config.h:100
Snapshot of game-loop real-time health for the GET /api/game-loop diagnostic endpoint....
Definition game_loop.h:14
Server configuration.
Definition http_server.h:74
RefreshCertFn refreshCert
Handler for POST /api/cert/refresh; fetches+installs a cert.
Definition http_server.h:84
GetGameLoopHealthFn getGameLoopHealth
Handler for GET /api/game-loop; RT loop health snapshot.
Definition http_server.h:86
GetLogFn getLog
Handler for GET /api/log; returns buffered log entries.
Definition http_server.h:83
std::string startedConfig
Serialized JSON of the boot config; served at GET /api/config.
Definition http_server.h:80
std::string version
Application version string served at GET /api/version.
Definition http_server.h:78
InitDeviceManagerFn initDeviceManager
Handler for POST /api/init; required for API-driven init.
Definition http_server.h:82
uint16_t port
TCP port to listen on (TLS).
Definition http_server.h:75
std::string certFile
Path to the TLS certificate (PEM).
Definition http_server.h:76
std::string keyFile
Path to the TLS private key (PEM).
Definition http_server.h:77
std::string corsOrigin
Value sent in Access-Control-Allow-Origin. Defaults to the production PWA origin.
Definition http_server.h:90
SetGameLoopPeriodFn setGameLoopPeriod
Definition http_server.h:88