Skip to content

Commit

Permalink
feat: support host parameter for server (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
vansangpfiev authored Dec 17, 2024
1 parent c9f15a2 commit 841a8df
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@
#error "Unsupported platform!"
#endif

void RunServer(std::optional<int> port, bool ignore_cout) {
void RunServer(std::optional<std::string> host, std::optional<int> port,
bool ignore_cout) {
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
signal(SIGINT, SIG_IGN);
#elif defined(_WIN32)
Expand All @@ -62,16 +63,24 @@ void RunServer(std::optional<int> port, bool ignore_cout) {
reinterpret_cast<PHANDLER_ROUTINE>(console_ctrl_handler), true);
#endif
auto config = file_manager_utils::GetCortexConfig();
if (port.has_value() && *port != std::stoi(config.apiServerPort)) {
if (host.has_value() || port.has_value()) {
if (host.has_value() && *host != config.apiServerHost) {
config.apiServerHost = *host;
}

if (port.has_value() && *port != std::stoi(config.apiServerPort)) {
config.apiServerPort = std::to_string(*port);
}

auto config_path = file_manager_utils::GetConfigurationPath();
config.apiServerPort = std::to_string(*port);
auto result =
config_yaml_utils::CortexConfigMgr::GetInstance().DumpYamlConfig(
config, config_path.string());
if (result.has_error()) {
CTL_ERR("Error update " << config_path.string() << result.error());
}
}

if (!ignore_cout) {
std::cout << "Host: " << config.apiServerHost
<< " Port: " << config.apiServerPort << "\n";
Expand Down Expand Up @@ -283,6 +292,7 @@ int main(int argc, char* argv[]) {
// avoid printing logs to terminal
is_server = true;

std::optional<std::string> server_host;
std::optional<int> server_port;
bool ignore_cout_log = false;
#if defined(_WIN32)
Expand All @@ -296,6 +306,8 @@ int main(int argc, char* argv[]) {
std::wstring v = argv[i + 1];
file_manager_utils::cortex_data_folder_path =
cortex::wc::WstringToUtf8(v);
} else if (command == L"--host") {
server_host = cortex::wc::WstringToUtf8(argv[i + 1]);
} else if (command == L"--port") {
server_port = std::stoi(argv[i + 1]);
} else if (command == L"--ignore_cout") {
Expand All @@ -312,6 +324,8 @@ int main(int argc, char* argv[]) {
file_manager_utils::cortex_config_file_path = argv[i + 1];
} else if (strcmp(argv[i], "--data_folder_path") == 0) {
file_manager_utils::cortex_data_folder_path = argv[i + 1];
} else if (strcmp(argv[i], "--host") == 0) {
server_host = argv[i + 1];
} else if (strcmp(argv[i], "--port") == 0) {
server_port = std::stoi(argv[i + 1]);
} else if (strcmp(argv[i], "--ignore_cout") == 0) {
Expand Down Expand Up @@ -367,6 +381,6 @@ int main(int argc, char* argv[]) {
}
}

RunServer(server_port, ignore_cout_log);
RunServer(server_host, server_port, ignore_cout_log);
return 0;
}

0 comments on commit 841a8df

Please sign in to comment.