forked from lthn/blockchain
Refactor ApiServer to manage server lifecycle with start, stop, and wait methods
This commit is contained in:
parent
785a65958e
commit
3a2ccb55c6
3 changed files with 40 additions and 7 deletions
|
|
@ -21,11 +21,11 @@ void ApiServer::run() {
|
|||
auto infoController = std::make_shared<InfoController>();
|
||||
docEndpoints->append(infoController->getEndpoints());
|
||||
|
||||
auto blockController = std::make_shared<BlockController>();
|
||||
docEndpoints->append(blockController->getEndpoints());
|
||||
// auto blockController = std::make_shared<BlockController>();
|
||||
// docEndpoints->append(blockController->getEndpoints());
|
||||
|
||||
router->addController(infoController);
|
||||
router->addController(blockController);
|
||||
// router->addController(blockController);
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo)
|
||||
([]
|
||||
|
|
@ -55,12 +55,28 @@ void ApiServer::run() {
|
|||
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, connectionProvider);
|
||||
|
||||
/* Create a server which takes provided TCP connections and passes them to the HTTP connection handler */
|
||||
oatpp::network::Server server(connectionProvider, connectionHandler);
|
||||
m_server = std::make_shared<oatpp::network::Server>(connectionProvider, connectionHandler);
|
||||
|
||||
/* Print server port */
|
||||
OATPP_LOGI("lethean-api", "Server running, API Docs: http://127.0.0.1:%s/swagger/ui", static_cast<const char*>(connectionProvider->getProperty("port").getData()));
|
||||
|
||||
/* Run server */
|
||||
server.run();
|
||||
m_server->run();
|
||||
|
||||
}
|
||||
|
||||
void ApiServer::start() {
|
||||
m_server_thread = std::thread(&ApiServer::run, this);
|
||||
}
|
||||
|
||||
void ApiServer::stop() {
|
||||
if (m_server) {
|
||||
m_server->stop();
|
||||
}
|
||||
}
|
||||
|
||||
void ApiServer::wait() {
|
||||
if (m_server_thread.joinable()) {
|
||||
m_server_thread.join();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,7 +10,15 @@
|
|||
#include "oatpp/core/macro/component.hpp"
|
||||
#include "oatpp-swagger/Resources.hpp"
|
||||
|
||||
#include "oatpp/network/Server.hpp"
|
||||
#include <thread>
|
||||
|
||||
class ApiServer {
|
||||
private:
|
||||
std::thread m_server_thread;
|
||||
std::shared_ptr<oatpp::network::Server> m_server;
|
||||
|
||||
void run();
|
||||
|
||||
public:
|
||||
|
||||
|
|
@ -44,7 +52,9 @@ public:
|
|||
|
||||
};
|
||||
|
||||
void run();
|
||||
void start();
|
||||
void stop();
|
||||
void wait();
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,19 @@
|
|||
#include "ApiServer.hpp"
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
#include "common/util.h"
|
||||
|
||||
int main(int argc, const char * argv[]) {
|
||||
|
||||
oatpp::base::Environment::init();
|
||||
|
||||
ApiServer server;
|
||||
server.run();
|
||||
|
||||
tools::signal_handler::install([&server] {
|
||||
server.stop();
|
||||
});
|
||||
|
||||
server.start();
|
||||
server.wait();
|
||||
|
||||
/* Destroy oatpp Environment */
|
||||
oatpp::base::Environment::destroy();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue