#include "ApiServer.hpp" #include "controller/InfoController.hpp" #include "controller/BlockController.hpp" #include "oatpp/network/Server.hpp" #include "oatpp-swagger/Controller.hpp" #include #include "version.h" #include "common/command_line.h" namespace { const command_line::arg_descriptor arg_api_bind_port = {"api-bind-port", "Port for API server to bind to", 36943}; const command_line::arg_descriptor arg_api_bind_host = {"api-bind-host", "IP/Hostname for API server to bind to", "127.0.0.1"}; } uint16_t ApiServer::m_port = 8000; std::string ApiServer::m_host = "127.0.0.1"; void ApiServer::init_options(boost::program_options::options_description& desc) { command_line::add_arg(desc, arg_api_bind_port); command_line::add_arg(desc, arg_api_bind_host); } ApiServer::ApiServer(const boost::program_options::variables_map& vm) : m_vm(vm) { if (vm.count(arg_api_bind_port.name)) { m_port = vm[arg_api_bind_port.name].as(); } if (vm.count(arg_api_bind_host.name)) { m_host = vm[arg_api_bind_host.name].as(); } } void ApiServer::run() { /* Register Components in scope of run() method */ Components components; /* Get router component */ OATPP_COMPONENT(std::shared_ptr, router); auto docEndpoints = std::make_shared(); auto infoController = std::make_shared(); docEndpoints->append(infoController->getEndpoints()); // auto blockController = std::make_shared(); // docEndpoints->append(blockController->getEndpoints()); router->addController(infoController); // router->addController(blockController); OATPP_CREATE_COMPONENT(std::shared_ptr, swaggerDocumentInfo) ([] { oatpp::swagger::DocumentInfo::Builder builder; builder .setTitle("Lethean Blockchain API") .setDescription("OpenAPI for Lethean Blockchain") .setVersion(PROJECT_VERSION) .setContactName("Lethean") .setContactUrl("https://lt.hn/") .setLicenseName("EUPL-1.2") .setLicenseUrl("https://joinup.ec.europa.eu/software/page/eupl/licence-eupl") .addServer("http://" + ApiServer::m_host + ":" + std::to_string(ApiServer::m_port), "Local Daemon") .addServer("http://seed.lethean.io:36943", "Seed Server"); return builder.build(); }()); /* Create a Swagger-UI controller and add its endpoints to the router */ auto swaggerController = oatpp::swagger::Controller::createShared(*docEndpoints); router->addController(swaggerController); /* Get a connection handler component */ OATPP_COMPONENT(std::shared_ptr, connectionHandler); /* Get a connection provider component */ OATPP_COMPONENT(std::shared_ptr, connectionProvider); /* Create a server which takes provided TCP connections and passes them to the HTTP connection handler */ m_server = std::make_shared(connectionProvider, connectionHandler); /* Print server port */ OATPP_LOGI("lethean-api", "Server running, API Docs: http://127.0.0.1:%s/swagger/ui", static_cast(connectionProvider->getProperty("port").getData())); /* Run server */ 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(); } }