Skip to content

Commit

Permalink
Merge pull request #29 from epsilla-cloud/port
Browse files Browse the repository at this point in the history
Make port configurable
  • Loading branch information
ricki-epsilla authored Aug 5, 2023
2 parents a3ce697 + 4c47b6e commit d8e3064
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 41 deletions.
13 changes: 11 additions & 2 deletions engine/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion engine/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion engine/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Server {

void Init(const std::string& config_filename);

Status Start();
Status Start(uint16_t port);
void Stop();

private:
Expand Down
8 changes: 4 additions & 4 deletions engine/server/web_server/web_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<oatpp::network::ServerConnectionProvider>, 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) + ")";
Expand All @@ -38,7 +38,7 @@ class WebComponent {

OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, 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<oatpp::web::server::HttpRouter>, http_router)([] {
Expand Down
2 changes: 1 addition & 1 deletion engine/server/web_server/web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
66 changes: 34 additions & 32 deletions engine/server/web_server/web_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,37 @@ namespace server {
namespace web {

class WebServer {
private:
std::atomic_bool try_stop_;
std::shared_ptr<std::thread> 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<std::thread> 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

0 comments on commit d8e3064

Please sign in to comment.