WIP: Configuration via Environment Variables #28
@ -1,4 +1,10 @@
|
|||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
#include <cstring> // For std::strcpy
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#ifndef __WIN32__
|
#ifndef __WIN32__
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
@ -344,68 +350,108 @@ void config_init(const string& st_localeServiceName)
|
|||||||
bool isCommonSQL = false;
|
bool isCommonSQL = false;
|
||||||
bool isPlayerSQL = false;
|
bool isPlayerSQL = false;
|
||||||
|
|
||||||
FILE* fpOnlyForDB;
|
// Open the file
|
||||||
|
std::ifstream inputFile(st_configFileName);
|
||||||
|
|
||||||
if (!(fpOnlyForDB = fopen(st_configFileName.c_str(), "r")))
|
// Check if the file is open
|
||||||
{
|
if (!inputFile.is_open()) {
|
||||||
SPDLOG_CRITICAL("Can not open [{}]", st_configFileName);
|
std::cerr << "Error opening file: " << st_configFileName << std::endl;
|
||||||
exit(EXIT_FAILURE);
|
return ;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fgets(buf, 256, fpOnlyForDB))
|
// Container to store the key-value pairs
|
||||||
{
|
std::unordered_map<std::string, char*> keyValuePairs;
|
||||||
parse_token(buf, token_string, value_string);
|
|
||||||
|
|
||||||
TOKEN("BLOCK_LOGIN")
|
std::string line;
|
||||||
{
|
// Read the file line by line
|
||||||
g_stBlockDate = value_string;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("adminpage_ip")
|
// Close the file
|
||||||
{
|
inputFile.close();
|
||||||
FN_add_adminpageIP(value_string);
|
|
||||||
|
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"];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (keyValuePairs.find("ADMINPAGE_IP") != keyValuePairs.end()){
|
||||||
|
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP"]);
|
||||||
//g_stAdminPageIP[0] = value_string;
|
//g_stAdminPageIP[0] = value_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("adminpage_ip1")
|
|
||||||
{
|
if (keyValuePairs.find("ADMINPAGE_IP1") != keyValuePairs.end()){
|
||||||
FN_add_adminpageIP(value_string);
|
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP1"]);
|
||||||
//g_stAdminPageIP[0] = value_string;
|
//g_stAdminPageIP[0] = value_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("adminpage_ip2")
|
|
||||||
{
|
if (keyValuePairs.find("ADMINPAGE_IP2") != keyValuePairs.end()) {
|
||||||
FN_add_adminpageIP(value_string);
|
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP2"]);
|
||||||
//g_stAdminPageIP[1] = value_string;
|
//g_stAdminPageIP[1] = value_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("adminpage_ip3")
|
|
||||||
{
|
if (keyValuePairs.find("ADMINPAGE_IP3") != keyValuePairs.end()) {
|
||||||
FN_add_adminpageIP(value_string);
|
FN_add_adminpageIP(keyValuePairs["ADMINPAGE_IP3"]);
|
||||||
//g_stAdminPageIP[2] = value_string;
|
//g_stAdminPageIP[2] = value_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("adminpage_password")
|
|
||||||
{
|
if (keyValuePairs.find("ADMINPAGE_PASSWORD") != keyValuePairs.end()){
|
||||||
g_stAdminPagePassword = value_string;
|
g_stAdminPagePassword = keyValuePairs["ADMINPAGE_PASSWORD"];
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("hostname")
|
|
||||||
{
|
if (keyValuePairs.find("HOSTNAME") != keyValuePairs.end()) {
|
||||||
g_stHostname = value_string;
|
g_stHostname = keyValuePairs["HOSTNAME"];
|
||||||
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
|
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("channel")
|
|
||||||
{
|
if (keyValuePairs.find("CHANNEL") != keyValuePairs.end()){
|
||||||
str_to_number(g_bChannel, value_string);
|
str_to_number(g_bChannel, keyValuePairs["CHANNEL"]);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("player_sql")
|
|
||||||
{
|
if (keyValuePairs.find("PLAYER_SQL") != keyValuePairs.end()){
|
||||||
const char * line = two_arguments(value_string, 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])
|
||||||
@ -424,12 +470,11 @@ void config_init(const string& st_localeServiceName)
|
|||||||
char buf[1024];
|
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]);
|
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;
|
isPlayerSQL = true;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("common_sql")
|
|
||||||
{
|
if (keyValuePairs.find("COMMON_SQL") != keyValuePairs.end()){
|
||||||
const char * line = two_arguments(value_string, db_host[1], sizeof(db_host[1]), db_user[1], sizeof(db_user[1]));
|
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]));
|
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])
|
||||||
@ -448,12 +493,11 @@ void config_init(const string& st_localeServiceName)
|
|||||||
char buf[1024];
|
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]);
|
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;
|
isCommonSQL = true;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TOKEN("log_sql")
|
|
||||||
{
|
if (keyValuePairs.find("LOG_SQL") != keyValuePairs.end()){
|
||||||
const char * line = two_arguments(value_string, log_host, sizeof(log_host), log_user, sizeof(log_user));
|
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));
|
line = two_arguments(line, log_pwd, sizeof(log_pwd), log_db, sizeof(log_db));
|
||||||
|
|
||||||
if ('\0' != line[0])
|
if ('\0' != line[0])
|
||||||
@ -471,12 +515,7 @@ void config_init(const string& st_localeServiceName)
|
|||||||
|
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port);
|
snprintf(buf, sizeof(buf), "LOG_SQL: %s %s %s %s %d", log_host, log_user, log_pwd, log_db, log_port);
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
//처리가 끝났으니 파일을 닫자.
|
|
||||||
fclose(fpOnlyForDB);
|
|
||||||
|
|
||||||
// CONFIG_SQL_INFO_ERROR
|
// CONFIG_SQL_INFO_ERROR
|
||||||
if (!isCommonSQL)
|
if (!isCommonSQL)
|
||||||
|
Loading…
Reference in New Issue
Block a user