diff --git a/examples/simple_example/main.cc b/examples/simple_example/main.cc index 844132fb..8bc95140 100644 --- a/examples/simple_example/main.cc +++ b/examples/simple_example/main.cc @@ -345,6 +345,14 @@ int main() auto resp = HttpResponse::newFileResponse("index.html"); resp->setExpiredTime(0); app().setCustom404Page(resp); + app().addListener("0.0.0.0", 0); + app().registerBeginningAdvice([]() { + auto addresses = app().getListeners(); + for (auto &address : addresses) + { + LOG_INFO << address.toIpPort() << " LISTEN"; + } + }); std::cout << "Date: " << std::string{drogon::utils::getHttpFullDate( trantor::Date::now())} diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index e6957f0c..1d300993 100644 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -1143,6 +1143,15 @@ class HttpAppFramework : public trantor::NonCopyable */ virtual size_t getCurrentThreadIndex() const = 0; + /** + * @brief Get the addresses of listeners. + * + * @return std::vector + * @note This method should be called after calling the app().run(). One + * could run this method in an AOP join point (such as the BeginningAdvice). + */ + virtual std::vector getListeners() const = 0; + private: virtual void registerHttpController( const std::string &pathPattern, diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index d5423497..70ee708a 100644 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -995,3 +995,8 @@ const std::function { return customErrorHandler_; } + +std::vector HttpAppFrameworkImpl::getListeners() const +{ + return listenerManagerPtr_->getListeners(); +} \ No newline at end of file diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 9b49bf27..6ea3a239 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -441,7 +441,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework const std::string &name = "default", const bool isFast = false, const std::string &characterSet = "") override; - + virtual std::vector getListeners() const override; inline static HttpAppFrameworkImpl &instance() { static HttpAppFrameworkImpl instance; diff --git a/lib/src/HttpServer.h b/lib/src/HttpServer.h index f64f182c..7de16a72 100644 --- a/lib/src/HttpServer.h +++ b/lib/src/HttpServer.h @@ -82,6 +82,11 @@ class HttpServer : trantor::NonCopyable server_.enableSSL(certPath, keyPath); } + const trantor::InetAddress &address() const + { + return server_.address(); + } + private: void onConnection(const trantor::TcpConnectionPtr &conn); void onMessage(const trantor::TcpConnectionPtr &, trantor::MsgBuffer *); diff --git a/lib/src/ListenerManager.cc b/lib/src/ListenerManager.cc index 4768b623..837da953 100644 --- a/lib/src/ListenerManager.cc +++ b/lib/src/ListenerManager.cc @@ -260,3 +260,13 @@ void ListenerManager::stopListening() } #endif } + +std::vector ListenerManager::getListeners() const +{ + std::vector listeners; + for (auto &server : servers_) + { + listeners.emplace_back(server->address()); + } + return listeners; +} \ No newline at end of file diff --git a/lib/src/ListenerManager.h b/lib/src/ListenerManager.h index 9119ec1e..b70812aa 100644 --- a/lib/src/ListenerManager.h +++ b/lib/src/ListenerManager.h @@ -21,7 +21,10 @@ #include #include #include - +namespace trantor +{ +class InetAddress; +} namespace drogon { class ListenerManager : public trantor::NonCopyable @@ -44,6 +47,7 @@ class ListenerManager : public trantor::NonCopyable std::function> &syncAdvices); void startListening(); + std::vector getListeners() const; ~ListenerManager(); trantor::EventLoop *getIOLoop(size_t id) const; diff --git a/trantor b/trantor index cdf853ac..8ca21f12 160000 --- a/trantor +++ b/trantor @@ -1 +1 @@ -Subproject commit cdf853aca05261273bf271bf6fe6011245e75e13 +Subproject commit 8ca21f124d4d6b75fded94d28b69d1448c286cbd