WIP: Configuration via Environment Variables #28

Draft
Tr0n wants to merge 4 commits from Tr0n/server:config/env into nightly
Showing only changes of commit 8f9e9dc602 - Show all commits

View File

@ -1,4 +1,10 @@
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_map>
#include <string>
#include <cstring> // For std::strcpy
#include <sstream>
#ifndef __WIN32__
#include <ifaddrs.h>
@ -344,139 +350,172 @@ void config_init(const string& st_localeServiceName)
bool isCommonSQL = false;
bool isPlayerSQL = false;
FILE* fpOnlyForDB;
// Open the file
std::ifstream inputFile(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;
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();
if (!(fpOnlyForDB = fopen(st_configFileName.c_str(), "r")))
{
SPDLOG_CRITICAL("Can not open [{}]", st_configFileName);
exit(EXIT_FAILURE);
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"];
}
while (fgets(buf, 256, fpOnlyForDB))
{
parse_token(buf, token_string, value_string);
if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]);
//g_stAdminPageIP[0] = value_string;
}
TOKEN("BLOCK_LOGIN")
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"];
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
}
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]));
line = two_arguments(line, db_pwd[0], sizeof(db_pwd[0]), db_db[0], sizeof(db_db[0]));
if ('\0' != line[0])
{
g_stBlockDate = value_string;
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(mysql_db_port[0], buf);
}
TOKEN("adminpage_ip")
if (!*db_host[0] || !*db_user[0] || !*db_pwd[0] || !*db_db[0])
{
FN_add_adminpageIP(value_string);
//g_stAdminPageIP[0] = value_string;
SPDLOG_CRITICAL("PLAYER_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
TOKEN("adminpage_ip1")
char buf[1024];
snprintf(buf, sizeof(buf), "PLAYER_SQL: %s %s %s %s %d", db_host[0], db_user[0], db_pwd[0], db_db[0], mysql_db_port[0]);
isPlayerSQL = true;
}
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]));
line = two_arguments(line, db_pwd[1], sizeof(db_pwd[1]), db_db[1], sizeof(db_db[1]));
if ('\0' != line[0])
{
FN_add_adminpageIP(value_string);
//g_stAdminPageIP[0] = value_string;
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(mysql_db_port[1], buf);
}
TOKEN("adminpage_ip2")
if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1])
{
FN_add_adminpageIP(value_string);
//g_stAdminPageIP[1] = value_string;
SPDLOG_CRITICAL("COMMON_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
TOKEN("adminpage_ip3")
char buf[1024];
snprintf(buf, sizeof(buf), "COMMON_SQL: %s %s %s %s %d", db_host[1], db_user[1], db_pwd[1], db_db[1], mysql_db_port[1]);
isCommonSQL = true;
}
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));
line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db));
if ('\0' != line[0])
{
FN_add_adminpageIP(value_string);
//g_stAdminPageIP[2] = value_string;
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(log_port, buf);
}
TOKEN("adminpage_password")
if (!*log_host || !*log_user || !*log_pwd || !*log_db)
{
g_stAdminPagePassword = value_string;
SPDLOG_CRITICAL("LOG_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
TOKEN("hostname")
{
g_stHostname = value_string;
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
continue;
}
TOKEN("channel")
{
str_to_number(g_bChannel, value_string);
continue;
}
TOKEN("player_sql")
{
const char * line = two_arguments(value_string, 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])
{
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(mysql_db_port[0], buf);
}
if (!*db_host[0] || !*db_user[0] || !*db_pwd[0] || !*db_db[0])
{
SPDLOG_CRITICAL("PLAYER_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
char buf[1024];
snprintf(buf, sizeof(buf), "PLAYER_SQL: %s %s %s %s %d", db_host[0], db_user[0], db_pwd[0], db_db[0], mysql_db_port[0]);
isPlayerSQL = true;
continue;
}
TOKEN("common_sql")
{
const char * line = two_arguments(value_string, 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])
{
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(mysql_db_port[1], buf);
}
if (!*db_host[1] || !*db_user[1] || !*db_pwd[1] || !*db_db[1])
{
SPDLOG_CRITICAL("COMMON_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
char buf[1024];
snprintf(buf, sizeof(buf), "COMMON_SQL: %s %s %s %s %d", db_host[1], db_user[1], db_pwd[1], db_db[1], mysql_db_port[1]);
isCommonSQL = true;
continue;
}
TOKEN("log_sql")
{
const char * line = two_arguments(value_string, 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])
{
char buf[256];
one_argument(line, buf, sizeof(buf));
str_to_number(log_port, buf);
}
if (!*log_host || !*log_user || !*log_pwd || !*log_db)
{
SPDLOG_CRITICAL("LOG_SQL syntax: logsql <host user password db>");
exit(EXIT_FAILURE);
}
char buf[1024];
snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port);
continue;
}
}
//처리가 끝났으니 파일을 닫자.
fclose(fpOnlyForDB);
char buf[1024];
snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port);
}
// CONFIG_SQL_INFO_ERROR
if (!isCommonSQL)