From 12de26671b7a261558811a8080bb90a2bde77c1a Mon Sep 17 00:00:00 2001 From: Bartosz Burda Date: Fri, 23 Oct 2026 11:01:21 +0200 Subject: [PATCH 1/2] refactor(gateway): pass REST configuration to the HTTP layer as values Server, SSE and docs parameters are read once at startup and handed to the HTTP layer as plain values. Document that they are not reloaded at runtime. --- docs/config/server.rst | 3 +++ .../ros2_medkit_gateway/core/config.hpp | 13 ++++++++++ .../core/http/handlers/docs_handlers.hpp | 4 ++- .../core/http/handlers/health_handlers.hpp | 6 +++-- .../core/http/rest_server.hpp | 2 +- .../http/handlers/sse_fault_handler.hpp | 7 ++++-- src/ros2_medkit_gateway/src/gateway_node.cpp | 25 ++++++++++++++++++- .../src/http/handlers/docs_handlers.cpp | 16 ++++-------- .../src/http/handlers/health_handlers.cpp | 10 +------- .../src/http/handlers/sse_fault_handler.cpp | 10 +++----- .../src/http/rest_server.cpp | 21 ++++++++-------- .../src/openapi/capability_generator.cpp | 18 +++++-------- .../src/openapi/capability_generator.hpp | 5 +++- .../test/test_capability_generator.cpp | 17 +++++++++---- .../test/test_docs_handlers.cpp | 24 +++++++++++++----- .../test/test_sse_fault_handler.cpp | 8 +++--- 16 files changed, 118 insertions(+), 71 deletions(-) diff --git a/docs/config/server.rst b/docs/config/server.rst index 9966c1544..848dc55ae 100644 --- a/docs/config/server.rst +++ b/docs/config/server.rst @@ -16,6 +16,9 @@ The gateway can be configured via: Out-of-range values ------------------- +Every ``server.*``, ``sse.*`` and ``docs.*`` parameter is read once, when the +gateway starts. A later ``ros2 param set`` does not change the running server. + Where a parameter documents a valid range, that range is enforced when the value is read at startup, and an out-of-range value is never accepted in silence. What happens next differs by parameter, in one of three ways. diff --git a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/config.hpp b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/config.hpp index 7a2c3c33a..037fd2b96 100644 --- a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/config.hpp +++ b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/config.hpp @@ -14,11 +14,24 @@ #pragma once +#include #include #include namespace ros2_medkit_gateway { +/// Raw configuration values the REST layer reads. Clamping happens where the value is used. +struct RestConfig { + int64_t http_thread_pool_size{2}; + int64_t keep_alive_timeout_sec{2}; + int64_t sse_keepalive_interval_sec{30}; + int64_t sse_max_duration_sec{3600}; + bool docs_enabled{true}; + std::string server_host{"localhost"}; + int64_t server_port{8080}; + std::string fault_events_topic; +}; + /** * @brief TLS/HTTPS configuration settings * diff --git a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/docs_handlers.hpp b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/docs_handlers.hpp index c2986d3bf..9188e0bd2 100644 --- a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/docs_handlers.hpp +++ b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/docs_handlers.hpp @@ -16,6 +16,7 @@ #include +#include #include #include @@ -47,7 +48,8 @@ namespace handlers { class DocsHandlers { public: DocsHandlers(HandlerContext & ctx, GatewayNode & node, PluginManager * plugin_mgr, - const openapi::RouteRegistry * route_registry = nullptr); + const openapi::RouteRegistry * route_registry, bool docs_enabled, std::string server_host, + int64_t server_port); ~DocsHandlers(); /// GET /docs - Root capability description. diff --git a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/health_handlers.hpp b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/health_handlers.hpp index 57c145b26..1a22d10c9 100644 --- a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/health_handlers.hpp +++ b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/handlers/health_handlers.hpp @@ -53,9 +53,10 @@ class HealthHandlers { /// @param sse_tracker Shared SSE client counter, so /health can report how /// much of `sse.max_clients` is in use. Optional: a gateway built without /// one simply omits the field. + /// @param docs_enabled Value of `docs.enabled`; hides the Swagger UI entry when false. explicit HealthHandlers(HandlerContext & ctx, const openapi::RouteRegistry * route_registry = nullptr, - std::shared_ptr sse_tracker = nullptr) - : ctx_(ctx), route_registry_(route_registry), sse_tracker_(std::move(sse_tracker)) { + std::shared_ptr sse_tracker = nullptr, bool docs_enabled = true) + : ctx_(ctx), route_registry_(route_registry), sse_tracker_(std::move(sse_tracker)), docs_enabled_(docs_enabled) { } /// GET /health - Health check endpoint @@ -71,6 +72,7 @@ class HealthHandlers { HandlerContext & ctx_; const openapi::RouteRegistry * route_registry_{nullptr}; std::shared_ptr sse_tracker_; + bool docs_enabled_; }; } // namespace handlers diff --git a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/rest_server.hpp b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/rest_server.hpp index 026f2b854..030cd41db 100644 --- a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/rest_server.hpp +++ b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/core/http/rest_server.hpp @@ -61,7 +61,7 @@ class TriggerManager; class RESTServer { public: RESTServer(GatewayNode * node, const std::string & host, int port, const CorsConfig & cors_config, - const AuthConfig & auth_config, const RateLimitConfig & rate_limit_config, + const AuthConfig & auth_config, const RateLimitConfig & rate_limit_config, const RestConfig & rest_config, const TlsConfig & tls_config = TlsConfig{}); ~RESTServer(); diff --git a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/http/handlers/sse_fault_handler.hpp b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/http/handlers/sse_fault_handler.hpp index 79c0f4d2d..18e325f61 100644 --- a/src/ros2_medkit_gateway/include/ros2_medkit_gateway/http/handlers/sse_fault_handler.hpp +++ b/src/ros2_medkit_gateway/include/ros2_medkit_gateway/http/handlers/sse_fault_handler.hpp @@ -81,16 +81,19 @@ class SSEFaultHandler { * @brief Construct SSE fault handler with shared context. * @param ctx The shared handler context * @param client_tracker Shared SSE client counter (across all SSE handlers) + * @param fault_events_topic Fully qualified topic the FaultManager publishes fault events on */ - SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker); + SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker, + const std::string & fault_events_topic); /** * @brief Test-only constructor that overrides the keepalive interval. * @param ctx The shared handler context * @param client_tracker Shared SSE client counter (across all SSE handlers) * @param keepalive_interval Must be positive; non-positive values fall back to the 30s default. + * @param fault_events_topic Fully qualified topic the FaultManager publishes fault events on */ SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker, - std::chrono::milliseconds keepalive_interval); + std::chrono::milliseconds keepalive_interval, const std::string & fault_events_topic); /// Destructor. Signals shutdown, then blocks until every stream closure has /// been destroyed: the per-stream unregister deleter locks queue_mutex_ and diff --git a/src/ros2_medkit_gateway/src/gateway_node.cpp b/src/ros2_medkit_gateway/src/gateway_node.cpp index c4f4e4036..17399c34e 100644 --- a/src/ros2_medkit_gateway/src/gateway_node.cpp +++ b/src/ros2_medkit_gateway/src/gateway_node.cpp @@ -37,6 +37,7 @@ #include "ros2_medkit_gateway/core/faults/fault_scope.hpp" #include "ros2_medkit_gateway/core/logs/log_scope.hpp" #include "ros2_medkit_gateway/core/thread_pool_config.hpp" +#include "ros2_medkit_gateway/fault_manager_paths.hpp" #include "ros2_medkit_gateway/param_utils.hpp" #include "ros2_medkit_gateway/plugins/ros_plugin_context.hpp" #include "ros2_medkit_gateway/ros2/ros2_log_sink.hpp" @@ -1570,8 +1571,30 @@ GatewayNode::GatewayNode(const rclcpp::NodeOptions & options) : Node("ros2_medki }); // Start REST server with configured host, port, CORS, auth, and TLS + RestConfig rest_config; + rest_config.http_thread_pool_size = get_parameter("server.http_thread_pool_size").as_int(); + rest_config.keep_alive_timeout_sec = get_parameter("server.keep_alive_timeout_sec").as_int(); + rest_config.sse_keepalive_interval_sec = get_parameter("sse.keepalive_interval_sec").as_int(); + rest_config.sse_max_duration_sec = get_parameter("sse.max_duration_sec").as_int(); + try { + rest_config.docs_enabled = get_parameter("docs.enabled").as_bool(); + } catch (...) { + rest_config.docs_enabled = true; + } + try { + rest_config.server_host = get_parameter("server.host").as_string(); + } catch (...) { + rest_config.server_host = "localhost"; + } + try { + rest_config.server_port = get_parameter("server.port").as_int(); + } catch (...) { + rest_config.server_port = 8080; + } + rest_config.fault_events_topic = build_fault_manager_events_topic(this); + rest_server_ = std::make_unique(this, server_host_, server_port_, cors_config_, auth_config_, - rate_limit_config_, tls_config_); + rate_limit_config_, rest_config, tls_config_); // Wire trigger handlers into REST server if (trigger_mgr_) { diff --git a/src/ros2_medkit_gateway/src/http/handlers/docs_handlers.cpp b/src/ros2_medkit_gateway/src/http/handlers/docs_handlers.cpp index 0dd427ab2..7769a7b40 100644 --- a/src/ros2_medkit_gateway/src/http/handlers/docs_handlers.cpp +++ b/src/ros2_medkit_gateway/src/http/handlers/docs_handlers.cpp @@ -59,18 +59,12 @@ void DocsHandlers::write_error(httplib::Response & res, int status, const std::s } DocsHandlers::DocsHandlers(HandlerContext & ctx, GatewayNode & node, PluginManager * plugin_mgr, - const openapi::RouteRegistry * route_registry) + const openapi::RouteRegistry * route_registry, bool docs_enabled, std::string server_host, + int64_t server_port) : ctx_(ctx) - , generator_(std::make_unique(ctx, node, plugin_mgr, route_registry)) - , docs_enabled_(true) { - // Read docs.enabled parameter if it has been declared - try { - docs_enabled_ = node.get_parameter("docs.enabled").as_bool(); - } catch (const std::exception &) { - docs_enabled_ = true; // Parameter may not be declared - default to enabled - } catch (...) { - docs_enabled_ = true; - } + , generator_(std::make_unique(ctx, node, plugin_mgr, route_registry, + std::move(server_host), server_port)) + , docs_enabled_(docs_enabled) { } DocsHandlers::~DocsHandlers() = default; diff --git a/src/ros2_medkit_gateway/src/http/handlers/health_handlers.cpp b/src/ros2_medkit_gateway/src/http/handlers/health_handlers.cpp index 54be5ee14..74a09730c 100644 --- a/src/ros2_medkit_gateway/src/http/handlers/health_handlers.cpp +++ b/src/ros2_medkit_gateway/src/http/handlers/health_handlers.cpp @@ -265,15 +265,7 @@ http::Result HealthHandlers::get_root(const http::TypedReques // and it is the only endpoint `docs.enabled` now hides from this list. The // `/docs` routes stay listed when the capability is off, because they stay // mounted and answer 501: this list says what is mounted, not what is on. - bool docs_enabled = true; - if (ctx_.node()) { - try { - docs_enabled = ctx_.node()->get_parameter("docs.enabled").as_bool(); - } catch (...) { - // Parameter may not be declared - default to true - } - } - if (docs_enabled) { + if (docs_enabled_) { endpoints.push_back("GET " + std::string(API_BASE_PATH) + "/swagger-ui"); } #endif diff --git a/src/ros2_medkit_gateway/src/http/handlers/sse_fault_handler.cpp b/src/ros2_medkit_gateway/src/http/handlers/sse_fault_handler.cpp index 58aa8906f..836845981 100644 --- a/src/ros2_medkit_gateway/src/http/handlers/sse_fault_handler.cpp +++ b/src/ros2_medkit_gateway/src/http/handlers/sse_fault_handler.cpp @@ -32,7 +32,6 @@ #include "ros2_medkit_gateway/core/http/error_codes.hpp" #include "ros2_medkit_gateway/core/http/http_utils.hpp" #include "ros2_medkit_gateway/core/models/error_info.hpp" -#include "ros2_medkit_gateway/fault_manager_paths.hpp" #include "ros2_medkit_gateway/gateway_node.hpp" #include "ros2_medkit_gateway/http/detail/primitives.hpp" #include "ros2_medkit_gateway/ros2/conversions/fault_msg_conversions.hpp" @@ -50,12 +49,13 @@ std::string sanitize_sse_event_type(std::string event_type) { } // namespace -SSEFaultHandler::SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker) - : SSEFaultHandler(ctx, std::move(client_tracker), std::chrono::seconds(kKeepaliveIntervalSec)) { +SSEFaultHandler::SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker, + const std::string & fault_events_topic) + : SSEFaultHandler(ctx, std::move(client_tracker), std::chrono::seconds(kKeepaliveIntervalSec), fault_events_topic) { } SSEFaultHandler::SSEFaultHandler(HandlerContext & ctx, std::shared_ptr client_tracker, - std::chrono::milliseconds keepalive_interval) + std::chrono::milliseconds keepalive_interval, const std::string & fault_events_topic) : ctx_(ctx) , client_tracker_(std::move(client_tracker)) , keepalive_interval_(keepalive_interval > std::chrono::milliseconds::zero() @@ -67,8 +67,6 @@ SSEFaultHandler::SSEFaultHandler(HandlerContext & ctx, std::shared_ptrcreate_subscription( diff --git a/src/ros2_medkit_gateway/src/http/rest_server.cpp b/src/ros2_medkit_gateway/src/http/rest_server.cpp index 9d8ebefe7..480385775 100644 --- a/src/ros2_medkit_gateway/src/http/rest_server.cpp +++ b/src/ros2_medkit_gateway/src/http/rest_server.cpp @@ -84,7 +84,7 @@ static_assert(std::is_constructible_vget_parameter("server.http_thread_pool_size").as_int(); + const auto requested_pool_size = rest_config.http_thread_pool_size; const auto http_thread_pool_size = clamp_thread_count(requested_pool_size, 1, 1024); if (static_cast(http_thread_pool_size) != requested_pool_size) { MEDKIT_LOG_WARN(log::Logger("rest_server"), "server.http_thread_pool_size %" PRId64 " clamped to %zu", requested_pool_size, http_thread_pool_size); } - const auto requested_keep_alive = node_->get_parameter("server.keep_alive_timeout_sec").as_int(); + const auto requested_keep_alive = rest_config.keep_alive_timeout_sec; const auto keep_alive_timeout_sec = clamp_keep_alive_timeout(requested_keep_alive, 1, 3600); if (static_cast(keep_alive_timeout_sec) != requested_keep_alive) { MEDKIT_LOG_WARN(log::Logger("rest_server"), "server.keep_alive_timeout_sec %" PRId64 " clamped to %" PRId64, @@ -179,8 +179,8 @@ RESTServer::RESTServer(GatewayNode * node, const std::string & host, int port, c // HealthHandlers as a null pointer and silently omit the field. sse_client_tracker_ = node_->get_sse_client_tracker(); - health_handlers_ = - std::make_unique(*handler_ctx_, route_registry_.get(), sse_client_tracker_); + health_handlers_ = std::make_unique(*handler_ctx_, route_registry_.get(), + sse_client_tracker_, rest_config.docs_enabled); discovery_handlers_ = std::make_unique(*handler_ctx_); data_handlers_ = std::make_unique(*handler_ctx_); lifecycle_handlers_ = std::make_unique( @@ -195,20 +195,20 @@ RESTServer::RESTServer(GatewayNode * node, const std::string & host, int port, c // disagree with no way to see it from outside. constexpr int64_t kMinKeepaliveSec = 1; constexpr int64_t kMaxKeepaliveSec = 3600; - const int64_t keepalive_raw = node_->get_parameter("sse.keepalive_interval_sec").as_int(); + const int64_t keepalive_raw = rest_config.sse_keepalive_interval_sec; const int64_t keepalive_used = std::clamp(keepalive_raw, kMinKeepaliveSec, kMaxKeepaliveSec); if (keepalive_used != keepalive_raw) { MEDKIT_LOG_WARN(log::Logger(node_->get_logger().get_name()), "sse.keepalive_interval_sec %" PRId64 " clamped to %" PRId64, keepalive_raw, keepalive_used); } - sse_fault_handler_ = std::make_unique(*handler_ctx_, sse_client_tracker_, - std::chrono::seconds(keepalive_used)); + sse_fault_handler_ = std::make_unique( + *handler_ctx_, sse_client_tracker_, std::chrono::seconds(keepalive_used), rest_config.fault_events_topic); bulkdata_handlers_ = std::make_unique(*handler_ctx_); // Validated as int64 before narrowing: narrowing first wraps a value above // INT_MAX into a small positive one that passes the check, so 4294967300 // would silently cap every subscription at 4 seconds. The warning names the // requested value, otherwise the operator cannot tell which value was refused. - const int64_t max_duration_raw = node_->get_parameter("sse.max_duration_sec").as_int(); + const int64_t max_duration_raw = rest_config.sse_max_duration_sec; int max_duration_sec = 3600; if (max_duration_raw < 1 || max_duration_raw > std::numeric_limits::max()) { MEDKIT_LOG_WARN(log::Logger("rest_server"), @@ -232,7 +232,8 @@ RESTServer::RESTServer(GatewayNode * node, const std::string & host, int port, c } docs_handlers_ = std::make_unique(*handler_ctx_, *node_, node_->get_plugin_manager(), - route_registry_.get()); + route_registry_.get(), rest_config.docs_enabled, + rest_config.server_host, rest_config.server_port); // Set up global error handlers for SOVD GenericError compliance setup_global_error_handlers(); diff --git a/src/ros2_medkit_gateway/src/openapi/capability_generator.cpp b/src/ros2_medkit_gateway/src/openapi/capability_generator.cpp index 7db0f893f..06a527819 100644 --- a/src/ros2_medkit_gateway/src/openapi/capability_generator.cpp +++ b/src/ros2_medkit_gateway/src/openapi/capability_generator.cpp @@ -109,12 +109,15 @@ void for_each_operation(nlohmann::json & document, Visitor && visit) { } // namespace CapabilityGenerator::CapabilityGenerator(handlers::HandlerContext & ctx, GatewayNode & node, PluginManager * plugin_mgr, - const RouteRegistry * route_registry, DocsCacheBounds bounds) + const RouteRegistry * route_registry, std::string server_host, + int64_t server_port, DocsCacheBounds bounds) : ctx_(ctx) , node_(node) , plugin_mgr_(plugin_mgr) , route_registry_(route_registry) , schema_builder_() + , server_host_(std::move(server_host)) + , server_port_(server_port) , bounds_(bounds) { } @@ -1104,17 +1107,8 @@ bool CapabilityGenerator::validate_entity_hierarchy(const ResolvedPath & resolve // ----------------------------------------------------------------------------- std::string CapabilityGenerator::build_server_url() const { - // Read host/port independently so a missing host doesn't clobber a valid port - std::string host = "localhost"; - int port = 8080; - try { - host = node_.get_parameter("server.host").as_string(); - } catch (...) { - } - try { - port = static_cast(node_.get_parameter("server.port").as_int()); - } catch (...) { - } + std::string host = server_host_; + const int port = static_cast(server_port_); // Use localhost for display if bound to all interfaces if (host == "0.0.0.0") { diff --git a/src/ros2_medkit_gateway/src/openapi/capability_generator.hpp b/src/ros2_medkit_gateway/src/openapi/capability_generator.hpp index 5532a408c..621e6da7a 100644 --- a/src/ros2_medkit_gateway/src/openapi/capability_generator.hpp +++ b/src/ros2_medkit_gateway/src/openapi/capability_generator.hpp @@ -71,7 +71,8 @@ struct DocsCacheBounds { class CapabilityGenerator { public: CapabilityGenerator(handlers::HandlerContext & ctx, GatewayNode & node, PluginManager * plugin_mgr, - const RouteRegistry * route_registry = nullptr, DocsCacheBounds bounds = DocsCacheBounds{}); + const RouteRegistry * route_registry, std::string server_host, int64_t server_port, + DocsCacheBounds bounds = DocsCacheBounds{}); /// Number of documents currently cached. size_t cache_entry_count() const; @@ -236,6 +237,8 @@ class CapabilityGenerator { PluginManager * plugin_mgr_; const RouteRegistry * route_registry_; SchemaBuilder schema_builder_; + std::string server_host_; + int64_t server_port_; DocsCacheBounds bounds_; // Generation-based document cache - invalidated when entity cache changes. diff --git a/src/ros2_medkit_gateway/test/test_capability_generator.cpp b/src/ros2_medkit_gateway/test/test_capability_generator.cpp index 8c34384bb..bd48b7b9d 100644 --- a/src/ros2_medkit_gateway/test/test_capability_generator.cpp +++ b/src/ros2_medkit_gateway/test/test_capability_generator.cpp @@ -142,8 +142,9 @@ class CapabilityGeneratorTest : public ::testing::Test { route_registry_ = std::make_unique(); populate_test_routes(*route_registry_); - generator_ = - std::make_unique(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + generator_ = std::make_unique( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); } void TearDown() override { @@ -758,7 +759,9 @@ TEST_F(CapabilityGeneratorTest, ByteBudgetEvictsBeforeTheEntryCountWould) { // A budget that fits one document of this size but not two. openapi::DocsCacheBounds bounds; bounds.max_bytes = first->size() + 64; - CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), bounds); + CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), + node_->get_parameter("server.host").as_string(), + node_->get_parameter("server.port").as_int(), bounds); ASSERT_TRUE(bounded.generate_serialized("/areas").has_value()); ASSERT_EQ(bounded.cache_entry_count(), 1u); @@ -774,7 +777,9 @@ TEST_F(CapabilityGeneratorTest, ByteBudgetEvictsBeforeTheEntryCountWould) { TEST_F(CapabilityGeneratorTest, DocumentLargerThanTheBudgetIsServedUncached) { openapi::DocsCacheBounds bounds; bounds.max_bytes = 16; - CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), bounds); + CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), + node_->get_parameter("server.host").as_string(), + node_->get_parameter("server.port").as_int(), bounds); auto document = bounded.generate_serialized("/areas"); ASSERT_TRUE(document.has_value()); @@ -787,7 +792,9 @@ TEST_F(CapabilityGeneratorTest, DocumentLargerThanTheBudgetIsServedUncached) { TEST_F(CapabilityGeneratorTest, EntryCountEvictsWhenTheByteBudgetIsSlack) { openapi::DocsCacheBounds bounds; bounds.max_entries = 2; - CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), bounds); + CapabilityGenerator bounded(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), + node_->get_parameter("server.host").as_string(), + node_->get_parameter("server.port").as_int(), bounds); ASSERT_TRUE(bounded.generate_serialized("/areas").has_value()); ASSERT_TRUE(bounded.generate_serialized("/apps").has_value()); diff --git a/src/ros2_medkit_gateway/test/test_docs_handlers.cpp b/src/ros2_medkit_gateway/test/test_docs_handlers.cpp index 80f8ff910..9f4b0ce3f 100644 --- a/src/ros2_medkit_gateway/test/test_docs_handlers.cpp +++ b/src/ros2_medkit_gateway/test/test_docs_handlers.cpp @@ -109,7 +109,9 @@ TEST_F(DocsHandlersTest, DocsDisabledReturns501) { // Override docs.enabled to false (GatewayNode already declares it as true by default) node_->set_parameter(rclcpp::Parameter("docs.enabled", false)); - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; auto result = docs_handlers.handle_docs_root(http::TypedRequest(req)); @@ -125,7 +127,9 @@ TEST_F(DocsHandlersTest, DocsDisabledReturns501) { // @verifies REQ_INTEROP_002 TEST_F(DocsHandlersTest, DocsRootReturnsValidJson) { - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; auto result = docs_handlers.handle_docs_root(http::TypedRequest(req)); @@ -149,7 +153,9 @@ TEST_F(DocsHandlersTest, DocsRootReturnsValidJson) { // `dump(2)`. Re-dumping the parse and comparing is the mechanical check: // indent width, key order and separators all have to agree. TEST_F(DocsHandlersTest, DocsRootBodyIsTheSameBytesADomWouldHaveWritten) { - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; auto result = docs_handlers.handle_docs_root(http::TypedRequest(req)); @@ -161,7 +167,9 @@ TEST_F(DocsHandlersTest, DocsRootBodyIsTheSameBytesADomWouldHaveWritten) { // Same contract on the scoped route, which reaches the response through // `write_json_text` rather than through the typed router. TEST_F(DocsHandlersTest, DocsAnyPathBodyIsTheSameBytesADomWouldHaveWritten) { - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; httplib::Response res; @@ -186,7 +194,9 @@ TEST_F(DocsHandlersTest, DocsAnyPathBodyIsTheSameBytesADomWouldHaveWritten) { // @verifies REQ_INTEROP_002 TEST_F(DocsHandlersTest, DocsAnyPathReturns200ForEntityCollection) { - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; httplib::Response res; @@ -220,7 +230,9 @@ TEST_F(DocsHandlersTest, DocsAnyPathReturns200ForEntityCollection) { // @verifies REQ_INTEROP_002 TEST_F(DocsHandlersTest, DocsAnyPathReturns404ForInvalidPath) { - handlers::DocsHandlers docs_handlers(*ctx_, *node_, node_->get_plugin_manager(), route_registry_.get()); + handlers::DocsHandlers docs_handlers( + *ctx_, *node_, node_->get_plugin_manager(), route_registry_.get(), node_->get_parameter("docs.enabled").as_bool(), + node_->get_parameter("server.host").as_string(), node_->get_parameter("server.port").as_int()); httplib::Request req; httplib::Response res; diff --git a/src/ros2_medkit_gateway/test/test_sse_fault_handler.cpp b/src/ros2_medkit_gateway/test/test_sse_fault_handler.cpp index c51d19fb2..a9d4f62a5 100644 --- a/src/ros2_medkit_gateway/test/test_sse_fault_handler.cpp +++ b/src/ros2_medkit_gateway/test/test_sse_fault_handler.cpp @@ -199,7 +199,7 @@ class SSEFaultHandlerTest : public ::testing::Test { ctx_ = std::make_unique(node_.get(), cors_config, auth_config, tls_config, nullptr); tracker_ = std::make_shared(4); - handler_ = std::make_unique(*ctx_, tracker_); + handler_ = std::make_unique(*ctx_, tracker_, build_fault_manager_events_topic(ctx_->node())); publisher_node_ = std::make_shared("test_sse_fault_handler_publisher_" + std::to_string(test_id)); fault_events_topic_ = ros2_medkit_gateway::build_fault_manager_events_topic(node_.get()); @@ -283,7 +283,7 @@ TEST_F(SSEFaultHandlerTest, HandleStreamSetsSseHeadersAndChunkedProvider) { TEST_F(SSEFaultHandlerTest, HandleStreamRejectsWhenClientLimitIsReached) { auto limited_tracker = std::make_shared(1); - SSEFaultHandler limited_handler(*ctx_, limited_tracker); + SSEFaultHandler limited_handler(*ctx_, limited_tracker, build_fault_manager_events_topic(ctx_->node())); auto req_one = make_stream_request("127.0.0.1"); auto req_two = make_stream_request("127.0.0.2"); @@ -575,7 +575,7 @@ TEST_F(SSEFaultHandlerTest, StreamSanitizesNewlinesInEventType) { TEST_F(SSEFaultHandlerTest, StreamSendsKeepaliveCommentAfterTimeout) { auto fast_tracker = std::make_shared(1); - SSEFaultHandler fast_handler(*ctx_, fast_tracker, 10ms); + SSEFaultHandler fast_handler(*ctx_, fast_tracker, 10ms, build_fault_manager_events_topic(ctx_->node())); auto req = make_stream_request("127.0.0.1"); httplib::Response res; @@ -593,7 +593,7 @@ TEST_F(SSEFaultHandlerTest, NonPositiveKeepaliveOverrideLogsWarning) { testing::internal::CaptureStderr(); { auto warn_tracker = std::make_shared(1); - SSEFaultHandler warn_handler(*ctx_, warn_tracker, 0ms); + SSEFaultHandler warn_handler(*ctx_, warn_tracker, 0ms, build_fault_manager_events_topic(ctx_->node())); } auto logs = testing::internal::GetCapturedStderr(); From 77c9f2c6a9c0e0034eca1486de632651cbe3f9a3 Mon Sep 17 00:00:00 2001 From: Bartosz Burda Date: Fri, 23 Oct 2026 21:43:13 +0200 Subject: [PATCH 2/2] fix(gateway): give the REST layer the validated host and port The REST configuration read server.host and server.port straight from the parameters, while the listener binds the validated values. With server.port set to 80 the listener fell back to 8080 and the OpenAPI servers URL still said port 80. The configuration now takes server_host_ and server_port_. The try/catch blocks around server.host, server.port and docs.enabled are gone, because the node always declares these parameters. --- src/ros2_medkit_gateway/src/gateway_node.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/ros2_medkit_gateway/src/gateway_node.cpp b/src/ros2_medkit_gateway/src/gateway_node.cpp index 17399c34e..a275305e7 100644 --- a/src/ros2_medkit_gateway/src/gateway_node.cpp +++ b/src/ros2_medkit_gateway/src/gateway_node.cpp @@ -1576,21 +1576,9 @@ GatewayNode::GatewayNode(const rclcpp::NodeOptions & options) : Node("ros2_medki rest_config.keep_alive_timeout_sec = get_parameter("server.keep_alive_timeout_sec").as_int(); rest_config.sse_keepalive_interval_sec = get_parameter("sse.keepalive_interval_sec").as_int(); rest_config.sse_max_duration_sec = get_parameter("sse.max_duration_sec").as_int(); - try { - rest_config.docs_enabled = get_parameter("docs.enabled").as_bool(); - } catch (...) { - rest_config.docs_enabled = true; - } - try { - rest_config.server_host = get_parameter("server.host").as_string(); - } catch (...) { - rest_config.server_host = "localhost"; - } - try { - rest_config.server_port = get_parameter("server.port").as_int(); - } catch (...) { - rest_config.server_port = 8080; - } + rest_config.docs_enabled = get_parameter("docs.enabled").as_bool(); + rest_config.server_host = server_host_; + rest_config.server_port = server_port_; rest_config.fault_events_topic = build_fault_manager_events_topic(this); rest_server_ = std::make_unique(this, server_host_, server_port_, cors_config_, auth_config_,