1
0
Fork 0
forked from lthn/blockchain

Refactor ApiServer to manage server lifecycle with start, stop, and wait methods

This commit is contained in:
snider 2025-10-15 20:58:57 +01:00
parent 785a65958e
commit 3a2ccb55c6
3 changed files with 40 additions and 7 deletions

View file

@ -21,11 +21,11 @@ void ApiServer::run() {
auto infoController = std::make_shared<InfoController>(); auto infoController = std::make_shared<InfoController>();
docEndpoints->append(infoController->getEndpoints()); docEndpoints->append(infoController->getEndpoints());
auto blockController = std::make_shared<BlockController>(); // auto blockController = std::make_shared<BlockController>();
docEndpoints->append(blockController->getEndpoints()); // docEndpoints->append(blockController->getEndpoints());
router->addController(infoController); router->addController(infoController);
router->addController(blockController); // router->addController(blockController);
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo) 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); 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 */ /* 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 */ /* 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())); 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 */ /* 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();
}
}

View file

@ -10,7 +10,15 @@
#include "oatpp/core/macro/component.hpp" #include "oatpp/core/macro/component.hpp"
#include "oatpp-swagger/Resources.hpp" #include "oatpp-swagger/Resources.hpp"
#include "oatpp/network/Server.hpp"
#include <thread>
class ApiServer { class ApiServer {
private:
std::thread m_server_thread;
std::shared_ptr<oatpp::network::Server> m_server;
void run();
public: public:
@ -44,7 +52,9 @@ public:
}; };
void run(); void start();
void stop();
void wait();
}; };

View file

@ -1,12 +1,19 @@
#include "ApiServer.hpp" #include "ApiServer.hpp"
#include "oatpp/core/base/Environment.hpp" #include "oatpp/core/base/Environment.hpp"
#include "common/util.h"
int main(int argc, const char * argv[]) { int main(int argc, const char * argv[]) {
oatpp::base::Environment::init(); oatpp::base::Environment::init();
ApiServer server; ApiServer server;
server.run();
tools::signal_handler::install([&server] {
server.stop();
});
server.start();
server.wait();
/* Destroy oatpp Environment */ /* Destroy oatpp Environment */
oatpp::base::Environment::destroy(); oatpp::base::Environment::destroy();