From 4c47b6ef1ddd4be8d03b970619721254903db42d Mon Sep 17 00:00:00 2001 From: richard-epsilla Date: Fri, 4 Aug 2023 23:35:53 -0700 Subject: [PATCH] Make port configurable --- engine/main.cpp | 13 ++++- engine/server/server.cpp | 3 +- engine/server/server.hpp | 2 +- engine/server/web_server/web_component.hpp | 8 +-- engine/server/web_server/web_server.cpp | 2 +- engine/server/web_server/web_server.hpp | 66 +++++++++++----------- 6 files changed, 53 insertions(+), 41 deletions(-) diff --git a/engine/main.cpp b/engine/main.cpp index d45124a1..9c1f0b2d 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -14,6 +14,7 @@ void print_help(const std::string &app_name) { << std::endl; std::cout << " Options:" << std::endl; std::cout << " -h --help Print this help." << std::endl; + std::cout << " -p --port port_number Specify the server port." << std::endl; // std::cout << " -c --conf_file filename Read configuration from the file." << std::endl; std::cout << std::endl; } @@ -29,6 +30,7 @@ int main(int argc, char *argv[]) { static struct option long_options[] = {{"conf_file", required_argument, nullptr, 'c'}, {"help", no_argument, nullptr, 'h'}, + {"port", required_argument, 0, 'p'}, {nullptr, 0, nullptr, 0}}; int option_index = 0; @@ -45,7 +47,8 @@ int main(int argc, char *argv[]) { // } int value; - while ((value = getopt_long(argc, argv, "c:dh", long_options, &option_index)) != -1) { + uint16_t port = 8888; + while ((value = getopt_long(argc, argv, "c:p:h", long_options, &option_index)) != -1) { switch (value) { case 'c': { char *config_filename_ptr = strdup(optarg); @@ -54,6 +57,12 @@ int main(int argc, char *argv[]) { std::cout << "Loading configuration from: " << config_filename << std::endl; break; } + case 'p': { + std::string server_port = optarg; + std::cout << "Server port: " << server_port << std::endl; + port = (uint16_t)(stoi(server_port)); + break; + } case 'h': print_help(app_name); return EXIT_SUCCESS; @@ -65,7 +74,7 @@ int main(int argc, char *argv[]) { server.Init(config_filename); - status = server.Start(); + status = server.Start(port); if (status.ok()) { std::cout << "Epsilla Vector Database server started successfully!" << std::endl; } else { diff --git a/engine/server/server.cpp b/engine/server/server.cpp index 96bacd14..cffa7085 100644 --- a/engine/server/server.cpp +++ b/engine/server/server.cpp @@ -18,8 +18,9 @@ void Server::Init(const std::string& config_filename) { config_filename_ = config_filename; } -Status Server::Start() { +Status Server::Start(uint16_t port) { try { + web::WebServer::GetInstance().SetPort(port); return StartService(); } catch (std::exception& ex) { std::string str = "Epsilla VectorDB server encounter exception: " + std::string(ex.what()); diff --git a/engine/server/server.hpp b/engine/server/server.hpp index 9fdb387b..9575edcc 100644 --- a/engine/server/server.hpp +++ b/engine/server/server.hpp @@ -12,7 +12,7 @@ class Server { void Init(const std::string& config_filename); - Status Start(); + Status Start(uint16_t port); void Stop(); private: diff --git a/engine/server/web_server/web_component.hpp b/engine/server/web_server/web_component.hpp index a273c8d1..30538af6 100644 --- a/engine/server/web_server/web_component.hpp +++ b/engine/server/web_server/web_component.hpp @@ -18,16 +18,16 @@ namespace web { class WebComponent { public: - explicit WebComponent(int port): port_(port) {} + explicit WebComponent(uint16_t port): port_(port) {} private: - const int port_; + const uint16_t port_; public: OATPP_CREATE_COMPONENT(std::shared_ptr, server_connection_provider) ([this] { try { - return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", 8888, oatpp::network::Address::IP_4}); + return oatpp::network::tcp::server::ConnectionProvider::createShared({"0.0.0.0", this->port_, oatpp::network::Address::IP_4}); } catch (std::exception& e) { std::string error_msg = "Cannot bind http port " + std::to_string(this->port_) + ". " + e.what() + "(errno: " + std::to_string(errno) + ", details: " + strerror(errno) + ")"; @@ -38,7 +38,7 @@ class WebComponent { OATPP_CREATE_COMPONENT(std::shared_ptr, client_connection_provider) ([this] { - return oatpp::network::tcp::client::ConnectionProvider::createShared({"0.0.0.0", 8888, oatpp::network::Address::IP_4}); + return oatpp::network::tcp::client::ConnectionProvider::createShared({"0.0.0.0", this->port_, oatpp::network::Address::IP_4}); }()); OATPP_CREATE_COMPONENT(std::shared_ptr, http_router)([] { diff --git a/engine/server/web_server/web_server.cpp b/engine/server/web_server/web_server.cpp index d4b77449..e1a66c80 100644 --- a/engine/server/web_server/web_server.cpp +++ b/engine/server/web_server/web_server.cpp @@ -32,7 +32,7 @@ WebServer::StartService() { oatpp::base::Environment::init(); { std::cout << "web server started!" << std::endl; - WebComponent components = WebComponent(std::stoi("8888")); + WebComponent components = WebComponent(port_); /* create ApiControllers and add endpoints to router */ auto user_controller = WebController::createShared(); diff --git a/engine/server/web_server/web_server.hpp b/engine/server/web_server/web_server.hpp index 86c7f167..41aeaec0 100644 --- a/engine/server/web_server/web_server.hpp +++ b/engine/server/web_server/web_server.hpp @@ -13,35 +13,37 @@ namespace server { namespace web { class WebServer { - private: - std::atomic_bool try_stop_; - std::shared_ptr thread_ptr_; - - private: - WebServer() { - try_stop_.store(false); - } - - ~WebServer() = default; - - Status - StartService(); - Status - StopService(); - - public: - static WebServer& - GetInstance() { - static WebServer web_server; - return web_server; - } - - void - Start(); - - void - Stop(); - }; -} -} -} + private: + std::atomic_bool try_stop_; + std::shared_ptr thread_ptr_; + uint16_t port_; + + private: + WebServer() { + try_stop_.store(false); + } + + ~WebServer() = default; + + Status + StartService(); + Status + StopService(); + + public: + static WebServer& GetInstance() { + static WebServer web_server; + return web_server; + } + + void Start(); + + void Stop(); + + void SetPort(uint16_t port) { + port_ = port; + } +}; +} // namespace web +} // namespace server +} // namespace vectordb