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 isPlayerSQL = false;
const std::string config_keys[] = {
"BLOCK_LOGIN",
"ADMINPAGE_IP",
"ADMINPAGE_IP1",
"ADMINPAGE_IP2",
"ADMINPAGE_IP3",
"ADMINPAGE_PASSWORD",
"HOSTNAME",
"CHANNEL",
"PLAYER_SQL",
"COMMON_SQL",
"LOG_SQL"
};
// 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;
// Open the file
std::ifstream inputFile(st_configFileName);
std::ifstream config_file(st_configFileName);
// Check if the file is open
if (!inputFile.is_open()) {
std::cerr << "Error opening file: " << st_configFileName << std::endl;
return ;
}
// Container to store the key-value pairs
std::unordered_map<std::string, char*> keyValuePairs;
if (!config_file.is_open()) {
SPDLOG_WARN("Error opening config file, can't fall back for missing envs.");
} else {
std::string line;
// Read the file line by line
while (std::getline(inputFile, line)) {
std::istringstream lineStream(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(lineStream, key, ':')) {
if (std::getline(line_stream, 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());
if (std::getline(line_stream >> std::ws, value)) {
// Store the key and value in the map
keyValuePairs[key] = valueCStr;
cfg_file_entries[key] = value;
}
}
}
config_file.close();
}
// Close the file
inputFile.close();
for (auto& kvp : keyValuePairs) {
for (auto& config_key : config_keys) {
// Check if the key is an environment variable
const char* envValue = std::getenv(kvp.first.c_str());
const char* envValue = std::getenv(config_key.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);
// 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;
}
}
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;
for (auto& kvp : config) {
SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second.c_str());
}
if (keyValuePairs.find("BLOCK_LOGIN") != keyValuePairs.end()){
g_stBlockDate = keyValuePairs["BLOCK_LOGIN"];
}
g_stBlockDate = config["BLOCK_LOGIN"];
g_stAdminPageIP.push_back(config["ADMINPAGE_IP"]);
g_stAdminPageIP.push_back(config["ADMINPAGE_IP1"]);
g_stAdminPageIP.push_back(config["ADMINPAGE_IP2"]);
g_stAdminPageIP.push_back(config["ADMINPAGE_IP3"]);
if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]);
//g_stAdminPageIP[0] = value_string;
}
g_stAdminPagePassword = config["ADMINPAGE_PASSWORD"];
if (keyValuePairs.find("ADMINPAGE_IP1") != keyValuePairs.end()){
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP1"]);
//g_stAdminPageIP[0] = value_string;
}
if (keyValuePairs.find("ADMINPAGE_IP2") != keyValuePairs.end()) {
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP2"]);
//g_stAdminPageIP[1] = value_string;
}
if (keyValuePairs.find("ADMINPAGE_IP3") != keyValuePairs.end()) {
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP3"]);
//g_stAdminPageIP[2] = value_string;
}
if (keyValuePairs.find("ADMINPAGE_PASSWORD") != keyValuePairs.end()){
g_stAdminPagePassword = keyValuePairs["ADMINPAGE_PASSWORD"];
}
if (keyValuePairs.find("HOSTNAME") != keyValuePairs.end()) {
g_stHostname = keyValuePairs["HOSTNAME"];
g_stHostname = config["HOSTNAME"];
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(keyValuePairs["PLAYER_SQL"], db_host[0], sizeof(db_host[0]), db_user[0], sizeof(db_user[0]));
{
const char * line = two_arguments(config["PLAYER_SQL"].c_str(), 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]));
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]));
if ('\0' != line[0])
@ -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));
if ('\0' != line[0])