From 5bb7fac01667133b505708371eb889d1ad3051ca Mon Sep 17 00:00:00 2001 From: Florian Rupprecht Date: Sun, 14 Nov 2021 13:04:19 +0100 Subject: [PATCH 1/2] Get app port --- include/crow/app.h | 5 +++++ include/crow/http_server.h | 3 +++ tests/unittest.cpp | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/include/crow/app.h b/include/crow/app.h index 083cca618..49b3471b6 100644 --- a/include/crow/app.h +++ b/include/crow/app.h @@ -123,6 +123,11 @@ namespace crow return *this; } + std::uint16_t port() + { + return port_; + } + ///Set the connection timeout in seconds (default is 5) self_t& timeout(std::uint8_t timeout) { diff --git a/include/crow/http_server.h b/include/crow/http_server.h index e6efa6fb0..28b493771 100644 --- a/include/crow/http_server.h +++ b/include/crow/http_server.h @@ -147,6 +147,9 @@ namespace crow }); } + port_ = acceptor_.local_endpoint().port(); + handler_->port(port_); + CROW_LOG_INFO << server_name_ << " server is running at " << bindaddr_ <<":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads"; CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs."; diff --git a/tests/unittest.cpp b/tests/unittest.cpp index b0c74abd6..4227ffb64 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -2323,3 +2323,21 @@ TEST_CASE("base64") CHECK(crow::utility::base64decode(sample_bin2_enc,8) == std::string(reinterpret_cast(sample_bin2)).substr(0,4)); CHECK(crow::utility::base64decode(sample_bin2_enc_np, 6) == std::string(reinterpret_cast(sample_bin2)).substr(0,4)); } + + +TEST_CASE("get_port") +{ + SimpleApp app; + + const std::uint16_t port = 12345; + + std::thread runTest([&]() { + app.port(port).run(); + }); + + app.wait_for_server_start(); + CHECK(app.port() == port); + app.stop(); + + runTest.join(); +} \ No newline at end of file From 7f8629075d355cac5a095144c5b96109ae2fd198 Mon Sep 17 00:00:00 2001 From: Farook Al-Sammarraie Date: Mon, 15 Nov 2021 15:52:33 +0300 Subject: [PATCH 2/2] Added newline to end of unittest.cpp --- tests/unittest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 4227ffb64..bc8a951a0 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -2340,4 +2340,4 @@ TEST_CASE("get_port") app.stop(); runTest.join(); -} \ No newline at end of file +}