forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_upd_cmd.cc
More file actions
105 lines (93 loc) · 3.07 KB
/
config_upd_cmd.cc
File metadata and controls
105 lines (93 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "config_upd_cmd.h"
#include "commands/server_start_cmd.h"
#include "common/api_server_configuration.h"
#include "utils/curl_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/string_utils.h"
#include "utils/url_parser.h"
namespace {
inline Json::Value NormalizeJson(
const std::unordered_map<std::string, std::string> options) {
Json::Value root;
for (const auto& [key, value] : options) {
if (CONFIGURATIONS.find(key) == CONFIGURATIONS.end()) {
continue;
}
auto config = CONFIGURATIONS.at(key);
if (config.accept_value == "[on|off]") {
if (string_utils::EqualsIgnoreCase("on", value)) {
root[key] = true;
} else if (string_utils::EqualsIgnoreCase("off", value)) {
root[key] = false;
}
} else if (config.accept_value == "comma separated") {
auto origins = string_utils::SplitBy(value, ",");
Json::Value origin_array(Json::arrayValue);
for (const auto& origin : origins) {
origin_array.append(origin);
}
root[key] = origin_array;
} else if (config.accept_value == "string") {
root[key] = value;
} else {
CTL_ERR("Not support configuration type: " << config.accept_value
<< " for config key: " << key);
}
}
CTL_DBG("Normalized config update request: " << root.toStyledString());
return root;
}
}; // namespace
void commands::ConfigUpdCmd::Exec(
const std::string& host, int port,
const std::unordered_map<std::string, std::string>& options) {
auto non_null_opts = std::unordered_map<std::string, std::string>();
for (const auto& [key, value] : options) {
// In case of api_keys, we allow empty value
if (value.empty() && key != "api_keys") {
continue;
}
non_null_opts[key] = value;
}
if (non_null_opts.size() == 1) {
for (const auto& [key, value] : non_null_opts) {
if (key == "api_keys") {
auto config = file_manager_utils::GetCortexConfig();
config.apiKeys = string_utils::SplitBy(value, ",");
auto result = file_manager_utils::UpdateCortexConfig(config);
if (result.has_error()) {
CLI_LOG_ERROR(result.error());
} else {
CLI_LOG("Configuration updated successfully!");
}
return;
}
}
}
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}
auto url = url_parser::Url{
/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "configs"},
/* .queries = */ {},
};
auto json = NormalizeJson(non_null_opts);
if (json.empty()) {
CLI_LOG_ERROR("Invalid configuration options provided");
return;
}
auto update_cnf_result =
curl_utils::SimplePatchJson(url.ToFullPath(), json.toStyledString());
if (update_cnf_result.has_error()) {
CLI_LOG_ERROR(update_cnf_result.error());
return;
}
CLI_LOG("Configuration updated successfully!");
}