forked from lthn/blockchain
Add command line options for API server host and port configuration
This commit is contained in:
parent
4474eebe52
commit
d7066c2220
3 changed files with 36 additions and 5 deletions
|
|
@ -7,6 +7,30 @@
|
|||
|
||||
#include <iostream>
|
||||
#include "version.h"
|
||||
#include "common/command_line.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
const command_line::arg_descriptor<uint16_t> arg_api_bind_port = {"api-bind-port", "Port for API server to bind to", 36943};
|
||||
const command_line::arg_descriptor<std::string> 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<uint16_t>();
|
||||
}
|
||||
if (vm.count(arg_api_bind_host.name)) {
|
||||
m_host = vm[arg_api_bind_host.name].as<std::string>();
|
||||
}
|
||||
}
|
||||
|
||||
void ApiServer::run() {
|
||||
|
||||
|
|
|
|||
|
|
@ -2,33 +2,39 @@
|
|||
#define ApiServer_hpp
|
||||
|
||||
#include "currency_core/blockchain_storage.h"
|
||||
|
||||
#include "currency_core/currency_core.h"
|
||||
#include "oatpp/web/server/HttpConnectionHandler.hpp"
|
||||
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
|
||||
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
|
||||
#include "oatpp/core/macro/component.hpp"
|
||||
#include "oatpp-swagger/Resources.hpp"
|
||||
|
||||
#include "oatpp/network/Server.hpp"
|
||||
#include <thread>
|
||||
#include <boost/program_options.hpp>
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
class ApiServer {
|
||||
private:
|
||||
std::thread m_server_thread;
|
||||
std::shared_ptr<oatpp::network::Server> m_server;
|
||||
boost::program_options::variables_map m_vm;
|
||||
|
||||
void run();
|
||||
|
||||
public:
|
||||
static uint16_t m_port;
|
||||
static std::string m_host;
|
||||
|
||||
ApiServer() = default;
|
||||
static void init_options(po::options_description& desc);
|
||||
|
||||
ApiServer(const boost::program_options::variables_map& vm);
|
||||
|
||||
class Components {
|
||||
public:
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ServerConnectionProvider>, serverConnectionProvider)([] {
|
||||
return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8000, oatpp::network::Address::IP_4});
|
||||
return oatpp::network::tcp::server::ConnectionProvider::createShared({ApiServer::m_host, ApiServer::m_port, oatpp::network::Address::IP_4});
|
||||
}());
|
||||
|
||||
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, httpRouter)([] {
|
||||
|
|
|
|||
|
|
@ -201,6 +201,7 @@ int main(int argc, char* argv[])
|
|||
bc_services::bc_offers_service::init_options(desc_cmd_sett);
|
||||
currency::stratum_server::init_options(desc_cmd_sett);
|
||||
tools::db::db_backend_selector::init_options(desc_cmd_sett);
|
||||
ApiServer::init_options(desc_cmd_sett);
|
||||
|
||||
po::options_description desc_options("Allowed options");
|
||||
desc_options.add(desc_cmd_only).add(desc_cmd_sett);
|
||||
|
|
@ -469,7 +470,7 @@ int main(int argc, char* argv[])
|
|||
|
||||
// Initialize API server
|
||||
oatpp::base::Environment::init();
|
||||
ApiServer api_server;
|
||||
ApiServer api_server(vm);
|
||||
api_server.start();
|
||||
|
||||
// Setup signal handler to gracefully stop the main p2p loop
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue