Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get app port #276

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/crow/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions include/crow/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
18 changes: 18 additions & 0 deletions tests/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2323,3 +2323,21 @@ TEST_CASE("base64")
CHECK(crow::utility::base64decode(sample_bin2_enc,8) == std::string(reinterpret_cast<char const*>(sample_bin2)).substr(0,4));
CHECK(crow::utility::base64decode(sample_bin2_enc_np, 6) == std::string(reinterpret_cast<char const*>(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();
}