60
60
// Global var to signal drogon to shutdown
61
61
volatile bool shutdown_signal;
62
62
63
- void RunServer (std::optional<std::string> host, std::optional<int > port,
64
- bool ignore_cout) {
63
+ struct ServerParams {
64
+ std::optional<std::string> server_host;
65
+ std::optional<int > server_port;
66
+ std::optional<std::string> api_keys;
67
+ std::optional<std::string> cors;
68
+ std::optional<std::string> allowed_origins;
69
+ };
70
+
71
+ void SetupServer (const ServerParams& params) {
72
+ auto config = file_manager_utils::GetCortexConfig ();
73
+
74
+ if (params.server_host && *(params.server_host ) != config.apiServerHost ) {
75
+ config.apiServerHost = *(params.server_host );
76
+ }
77
+
78
+ if (params.server_port &&
79
+ *(params.server_port ) != std::stoi (config.apiServerPort )) {
80
+ config.apiServerPort = std::to_string (*(params.server_port ));
81
+ }
82
+
83
+ if (params.api_keys ) {
84
+ config.apiKeys = string_utils::SplitBy (*(params.api_keys ), " ," );
85
+ }
86
+
87
+ if (params.cors ) {
88
+ config.enableCors = *(params.cors ) == " ON" ;
89
+ }
90
+
91
+ if (params.allowed_origins ) {
92
+ config.allowedOrigins =
93
+ string_utils::SplitBy (*(params.allowed_origins ), " ," );
94
+ }
95
+
96
+ auto result = file_manager_utils::UpdateCortexConfig (config);
97
+ if (result.has_error ()) {
98
+ CTL_ERR (result.error ());
99
+ }
100
+ }
101
+
102
+ void RunServer (bool ignore_cout) {
65
103
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
66
104
auto signal_handler = +[](int sig) -> void {
67
105
std::cout << " \r Caught interrupt signal:" << sig << " , shutting down\n " ;
@@ -81,23 +119,6 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
81
119
reinterpret_cast <PHANDLER_ROUTINE>(console_ctrl_handler), TRUE );
82
120
#endif
83
121
auto config = file_manager_utils::GetCortexConfig ();
84
- if (host.has_value () || port.has_value ()) {
85
- if (host.has_value () && *host != config.apiServerHost ) {
86
- config.apiServerHost = *host;
87
- }
88
-
89
- if (port.has_value () && *port != std::stoi (config.apiServerPort )) {
90
- config.apiServerPort = std::to_string (*port);
91
- }
92
-
93
- auto config_path = file_manager_utils::GetConfigurationPath ();
94
- auto result =
95
- config_yaml_utils::CortexConfigMgr::GetInstance ().DumpYamlConfig (
96
- config, config_path.string ());
97
- if (result.has_error ()) {
98
- CTL_ERR (" Error update " << config_path.string () << result.error ());
99
- }
100
- }
101
122
102
123
if (!ignore_cout) {
103
124
std::cout << " Host: " << config.apiServerHost
@@ -434,7 +455,8 @@ void print_help() {
434
455
" ~/cortexcpp)\n " ;
435
456
std::cout << " --host Host name (default: 127.0.0.1)\n " ;
436
457
std::cout << " --port Port number (default: 39281)\n " ;
437
- std::cout << " --api_configs Keys to acess API endpoints\n " ;
458
+ std::cout << " --api_keys Keys to acess API endpoints\n " ;
459
+ std::cout << " --cors Enable CORS (ON|OFF)\n " ;
438
460
std::cout << " --ignore_cout Ignore cout output\n " ;
439
461
std::cout << " --loglevel Set log level\n " ;
440
462
@@ -461,9 +483,7 @@ int main(int argc, char* argv[]) {
461
483
// avoid printing logs to terminal
462
484
is_server = true ;
463
485
464
- std::optional<std::string> server_host;
465
- std::optional<int > server_port;
466
- std::optional<std::string> api_keys;
486
+ ServerParams params;
467
487
bool ignore_cout_log = false ;
468
488
#if defined(_WIN32)
469
489
for (int i = 0 ; i < argc; i++) {
@@ -477,11 +497,19 @@ int main(int argc, char* argv[]) {
477
497
file_manager_utils::cortex_data_folder_path =
478
498
cortex::wc::WstringToUtf8 (v);
479
499
} else if (command == L" --host" ) {
480
- server_host = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
500
+ params. server_host = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
481
501
} else if (command == L" --port" ) {
482
- server_port = std::stoi (argv[i + 1 ]);
502
+ params. server_port = std::stoi (argv[i + 1 ]);
483
503
} else if (command == L" --api_keys" ) {
484
- api_keys = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
504
+ params.api_keys = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
505
+ } else if (command == L" --cors" ) {
506
+ params.cors = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
507
+ if (*(params.cors ) != " ON" && *(params.cors ) != " OFF" ) {
508
+ print_help ();
509
+ return 0 ;
510
+ }
511
+ } else if (command == L" --allowed_origins" ) {
512
+ params.allowed_origins = cortex::wc::WstringToUtf8 (argv[i + 1 ]);
485
513
} else if (command == L" --ignore_cout" ) {
486
514
ignore_cout_log = true ;
487
515
} else if (command == L" --loglevel" ) {
@@ -499,11 +527,19 @@ int main(int argc, char* argv[]) {
499
527
} else if (strcmp (argv[i], " --data_folder_path" ) == 0 ) {
500
528
file_manager_utils::cortex_data_folder_path = argv[i + 1 ];
501
529
} else if (strcmp (argv[i], " --host" ) == 0 ) {
502
- server_host = argv[i + 1 ];
530
+ params. server_host = argv[i + 1 ];
503
531
} else if (strcmp (argv[i], " --port" ) == 0 ) {
504
- server_port = std::stoi (argv[i + 1 ]);
532
+ params. server_port = std::stoi (argv[i + 1 ]);
505
533
} else if (strcmp (argv[i], " --api_keys" ) == 0 ) {
506
- api_keys = argv[i + 1 ];
534
+ params.api_keys = argv[i + 1 ];
535
+ } else if (strcmp (argv[i], " --cors" ) == 0 ) {
536
+ params.cors = argv[i + 1 ];
537
+ if (*(params.cors ) != " ON" && *(params.cors ) != " OFF" ) {
538
+ print_help ();
539
+ return 0 ;
540
+ }
541
+ } else if (strcmp (argv[i], " --allowed_origins" ) == 0 ) {
542
+ params.allowed_origins = argv[i + 1 ];
507
543
} else if (strcmp (argv[i], " --ignore_cout" ) == 0 ) {
508
544
ignore_cout_log = true ;
509
545
} else if (strcmp (argv[i], " --loglevel" ) == 0 ) {
@@ -539,14 +575,7 @@ int main(int argc, char* argv[]) {
539
575
}
540
576
}
541
577
542
- if (api_keys) {
543
- auto config = file_manager_utils::GetCortexConfig ();
544
- config.apiKeys = string_utils::SplitBy (*api_keys, " ," );
545
- auto result = file_manager_utils::UpdateCortexConfig (config);
546
- if (result.has_error ()) {
547
- CTL_ERR (result.error ());
548
- }
549
- }
578
+ SetupServer (params);
550
579
551
580
// check if migration is needed
552
581
if (auto res = cortex::migr::MigrationManager (
@@ -568,6 +597,6 @@ int main(int argc, char* argv[]) {
568
597
}
569
598
}
570
599
571
- RunServer (server_host, server_port, ignore_cout_log);
600
+ RunServer (ignore_cout_log);
572
601
return 0 ;
573
602
}
0 commit comments