remove dependency on config file (use env as config)

This commit is contained in:
Tr0n 2024-06-05 21:53:50 +02:00
parent 8f9e9dc602
commit 2b9651febc
No known key found for this signature in database

View File

@ -350,108 +350,87 @@ void config_init(const string& st_localeServiceName)
bool isCommonSQL = false; bool isCommonSQL = false;
bool isPlayerSQL = false; bool isPlayerSQL = false;
// Open the file const std::string config_keys[] = {
std::ifstream inputFile(st_configFileName); "BLOCK_LOGIN",
"ADMINPAGE_IP",
// Check if the file is open "ADMINPAGE_IP1",
if (!inputFile.is_open()) { "ADMINPAGE_IP2",
std::cerr << "Error opening file: " << st_configFileName << std::endl; "ADMINPAGE_IP3",
return ; "ADMINPAGE_PASSWORD",
} "HOSTNAME",
"CHANNEL",
// Container to store the key-value pairs "PLAYER_SQL",
std::unordered_map<std::string, char*> keyValuePairs; "COMMON_SQL",
"LOG_SQL"
std::string line; };
// Read the file line by line
while (std::getline(inputFile, line)) {
std::istringstream lineStream(line);
std::string key;
// Get the key
if (std::getline(lineStream, key, ':')) {
std::string value;
// Get the value
if (std::getline(lineStream >> std::ws, value)) {
// Allocate memory for the value
char* valueCStr = new char[value.length() + 1];
std::strcpy(valueCStr, value.c_str());
// Store the key and value in the map
keyValuePairs[key] = valueCStr;
}
}
}
// Close the file
inputFile.close();
for (auto& kvp : keyValuePairs) {
// Check if the key is an environment variable
const char* envValue = std::getenv(kvp.first.c_str());
if (envValue) {
// Free the old value memory
delete[] kvp.second;
// Allocate memory for the new environment variable value
kvp.second = new char[std::strlen(envValue) + 1];
std::strcpy(kvp.second, envValue);
}
char buffer[256];
std::snprintf(buffer, sizeof(buffer), "KEY: %s, VALUE: %s", kvp.first.c_str(), kvp.second);
SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second);
std::cout << buffer << std::endl;
}
if (keyValuePairs.find("BLOCK_LOGIN") != keyValuePairs.end()){
g_stBlockDate = keyValuePairs["BLOCK_LOGIN"];
}
// Container to store the config file key-value pairs
std::unordered_map<std::string, std::string> cfg_file_entries;
// Container to store the final config with env overrides
std::unordered_map<std::string, std::string> config;
if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){ // Open the file
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]); std::ifstream config_file(st_configFileName);
//g_stAdminPageIP[0] = value_string;
// Check if the file is open
if (!config_file.is_open()) {
SPDLOG_WARN("Error opening config file, can't fall back for missing envs.");
} else {
std::string line;
// Read the config file
while (std::getline(config_file, line)) {
std::istringstream line_stream(line);
std::string key;
// Get the key
if (std::getline(line_stream, key, ':')) {
std::string value;
// Get the value
if (std::getline(line_stream >> std::ws, value)) {
// Store the key and value in the map
cfg_file_entries[key] = value;
}
}
}
config_file.close();
} }
for (auto& config_key : config_keys) {
if (keyValuePairs.find("ADMINPAGE_IP1") != keyValuePairs.end()){ // Check if the key is an environment variable
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP1"]); const char* envValue = std::getenv(config_key.c_str());
//g_stAdminPageIP[0] = value_string; if (envValue) {
// Update the value in the map with the environment variable value
config[config_key] = envValue;
continue;
}
// Fallback to config file entries when there's no env override
if (cfg_file_entries.find(config_key) != cfg_file_entries.end()){
config[config_key] = cfg_file_entries[config_key];
continue;
}
} }
for (auto& kvp : config) {
if (keyValuePairs.find("ADMINPAGE_IP2") != keyValuePairs.end()) { SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second.c_str());
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP2"]);
//g_stAdminPageIP[1] = value_string;
} }
g_stBlockDate = config["BLOCK_LOGIN"];
if (keyValuePairs.find("ADMINPAGE_IP3") != keyValuePairs.end()) {
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP3"]);
//g_stAdminPageIP[2] = value_string;
}
g_stAdminPageIP.push_back(config["ADMINPAGE_IP"]);
if (keyValuePairs.find("ADMINPAGE_PASSWORD") != keyValuePairs.end()){ g_stAdminPageIP.push_back(config["ADMINPAGE_IP1"]);
g_stAdminPagePassword = keyValuePairs["ADMINPAGE_PASSWORD"]; g_stAdminPageIP.push_back(config["ADMINPAGE_IP2"]);
} g_stAdminPageIP.push_back(config["ADMINPAGE_IP3"]);
g_stAdminPagePassword = config["ADMINPAGE_PASSWORD"];
if (keyValuePairs.find("HOSTNAME") != keyValuePairs.end()) { g_stHostname = config["HOSTNAME"];
g_stHostname = keyValuePairs["HOSTNAME"]; SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
}
str_to_number(g_bChannel, config["CHANNEL"].c_str());
if (keyValuePairs.find("CHANNEL") != keyValuePairs.end()){
str_to_number(g_bChannel, keyValuePairs["CHANNEL"]);
}
{
if (keyValuePairs.find("PLAYER_SQL") != keyValuePairs.end()){ const char * line = two_arguments(config["PLAYER_SQL"].c_str(), db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0]));
const char * line = two_arguments(keyValuePairs["PLAYER_SQL"], db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0]));
line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0])); line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0]));
if ('\0' != line[0]) if ('\0' != line[0])
@ -473,8 +452,8 @@ void config_init(const string& st_localeServiceName)
} }
if (keyValuePairs.find("COMMON_SQL") != keyValuePairs.end()){ {
const char * line = two_arguments(keyValuePairs["COMMON_SQL"], db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1])); const char * line = two_arguments(config["COMMON_SQL"].c_str(), db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1]));
line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1])); line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1]));
if ('\0' != line[0]) if ('\0' != line[0])
@ -486,7 +465,7 @@ void config_init(const string& st_localeServiceName)
if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1]) if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1])
{ {
SPDLOG_CRITICAL("COMMON_SQL syntax: logsql <host user password db>"); SPDLOG_CRITICAL("COMMON_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -496,8 +475,8 @@ void config_init(const string& st_localeServiceName)
} }
if (keyValuePairs.find("LOG_SQL") != keyValuePairs.end()){ {
const char * line = two_arguments(keyValuePairs["LOG_SQL"], log_host, sizeof(log_host), log_user, sizeof(log_user)); const char * line = two_arguments(config["LOG_SQL"].c_str(), log_host, sizeof(log_host), log_user, sizeof(log_user));
line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db)); line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db));
if ('\0' != line[0]) if ('\0' != line[0])
@ -509,7 +488,7 @@ void config_init(const string& st_localeServiceName)
if (!*log_host || !*log_user || !*log_pwd || !*log_db) if (!*log_host || !*log_user || !*log_pwd || !*log_db)
{ {
SPDLOG_CRITICAL("LOG_SQL syntax: logsql <host user password db>"); SPDLOG_CRITICAL("LOG_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }