Add getListeners() method to the HttpAppFramework class (#559)

This commit is contained in:
An Tao 2020-09-07 00:51:07 +08:00 committed by GitHub
parent 3a00ffde47
commit e032f9bd0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 3 deletions

View File

@ -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())}

View File

@ -1143,6 +1143,15 @@ class HttpAppFramework : public trantor::NonCopyable
*/
virtual size_t getCurrentThreadIndex() const = 0;
/**
* @brief Get the addresses of listeners.
*
* @return std::vector<trantor::InetAddress>
* @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<trantor::InetAddress> getListeners() const = 0;
private:
virtual void registerHttpController(
const std::string &pathPattern,

View File

@ -995,3 +995,8 @@ const std::function<HttpResponsePtr(HttpStatusCode)>
{
return customErrorHandler_;
}
std::vector<trantor::InetAddress> HttpAppFrameworkImpl::getListeners() const
{
return listenerManagerPtr_->getListeners();
}

View File

@ -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<trantor::InetAddress> getListeners() const override;
inline static HttpAppFrameworkImpl &instance()
{
static HttpAppFrameworkImpl instance;

View File

@ -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 *);

View File

@ -260,3 +260,13 @@ void ListenerManager::stopListening()
}
#endif
}
std::vector<trantor::InetAddress> ListenerManager::getListeners() const
{
std::vector<trantor::InetAddress> listeners;
for (auto &server : servers_)
{
listeners.emplace_back(server->address());
}
return listeners;
}

View File

@ -21,7 +21,10 @@
#include <string>
#include <vector>
#include <memory>
namespace trantor
{
class InetAddress;
}
namespace drogon
{
class ListenerManager : public trantor::NonCopyable
@ -44,6 +47,7 @@ class ListenerManager : public trantor::NonCopyable
std::function<HttpResponsePtr(const HttpRequestPtr &)>>
&syncAdvices);
void startListening();
std::vector<trantor::InetAddress> getListeners() const;
~ListenerManager();
trantor::EventLoop *getIOLoop(size_t id) const;

@ -1 +1 @@
Subproject commit cdf853aca05261273bf271bf6fe6011245e75e13
Subproject commit 8ca21f124d4d6b75fded94d28b69d1448c286cbd