From 841a8df9074aa5fe51c7adacf7c080eb86717648 Mon Sep 17 00:00:00 2001 From: vansangpfiev Date: Wed, 18 Dec 2024 05:19:43 +0700 Subject: [PATCH] feat: support host parameter for server (#1805) --- engine/main.cc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/engine/main.cc b/engine/main.cc index b79859ef3..5cc6c740e 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -51,7 +51,8 @@ #error "Unsupported platform!" #endif -void RunServer(std::optional port, bool ignore_cout) { +void RunServer(std::optional host, std::optional port, + bool ignore_cout) { #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) signal(SIGINT, SIG_IGN); #elif defined(_WIN32) @@ -62,9 +63,16 @@ void RunServer(std::optional port, bool ignore_cout) { reinterpret_cast(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()); @@ -72,6 +80,7 @@ void RunServer(std::optional port, bool ignore_cout) { CTL_ERR("Error update " << config_path.string() << result.error()); } } + if (!ignore_cout) { std::cout << "Host: " << config.apiServerHost << " Port: " << config.apiServerPort << "\n"; @@ -283,6 +292,7 @@ int main(int argc, char* argv[]) { // avoid printing logs to terminal is_server = true; + std::optional server_host; std::optional server_port; bool ignore_cout_log = false; #if defined(_WIN32) @@ -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") { @@ -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) { @@ -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; }