Compare commits
4 Commits
master
...
config/env
Author | SHA1 | Date | |
---|---|---|---|
e642a6db52 | |||
04fdc78ea4 | |||
2b9651febc | |||
8f9e9dc602 |
@ -1,4 +1,6 @@
|
||||
#include "stdafx.h"
|
||||
#include <fstream>
|
||||
#include <unordered_map>
|
||||
#include <sstream>
|
||||
#ifndef __WIN32__
|
||||
#include <ifaddrs.h>
|
||||
@ -342,68 +344,87 @@ void config_init(const string& st_localeServiceName)
|
||||
bool isCommonSQL = false;
|
||||
bool isPlayerSQL = false;
|
||||
|
||||
FILE* fpOnlyForDB;
|
||||
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"
|
||||
};
|
||||
|
||||
if (!(fpOnlyForDB = fopen(st_configFileName.c_str(), "r")))
|
||||
{
|
||||
SPDLOG_CRITICAL("Can not open [{}]", st_configFileName);
|
||||
exit(EXIT_FAILURE);
|
||||
// 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 config_file(st_configFileName);
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
while (fgets(buf, 256, fpOnlyForDB))
|
||||
{
|
||||
parse_token(buf, token_string, value_string);
|
||||
|
||||
TOKEN("BLOCK_LOGIN")
|
||||
{
|
||||
g_stBlockDate = value_string;
|
||||
for (auto& config_key : config_keys) {
|
||||
// Check if the key is an environment variable
|
||||
const char* envValue = std::getenv(config_key.c_str());
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
TOKEN("adminpage_ip")
|
||||
{
|
||||
FN_add_adminpageIP(value_string);
|
||||
//g_stAdminPageIP[0] = value_string;
|
||||
for (auto& kvp : config) {
|
||||
SPDLOG_INFO("{}: {}", kvp.first.c_str(), kvp.second.c_str());
|
||||
}
|
||||
|
||||
TOKEN("adminpage_ip1")
|
||||
{
|
||||
FN_add_adminpageIP(value_string);
|
||||
//g_stAdminPageIP[0] = value_string;
|
||||
}
|
||||
g_stBlockDate = config["BLOCK_LOGIN"];
|
||||
|
||||
TOKEN("adminpage_ip2")
|
||||
{
|
||||
FN_add_adminpageIP(value_string);
|
||||
//g_stAdminPageIP[1] = value_string;
|
||||
}
|
||||
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"]);
|
||||
|
||||
TOKEN("adminpage_ip3")
|
||||
{
|
||||
FN_add_adminpageIP(value_string);
|
||||
//g_stAdminPageIP[2] = value_string;
|
||||
}
|
||||
g_stAdminPagePassword = config["ADMINPAGE_PASSWORD"];
|
||||
|
||||
TOKEN("adminpage_password")
|
||||
{
|
||||
g_stAdminPagePassword = value_string;
|
||||
}
|
||||
|
||||
TOKEN("hostname")
|
||||
{
|
||||
g_stHostname = value_string;
|
||||
g_stHostname = config["HOSTNAME"];
|
||||
SPDLOG_INFO("HOSTNAME: {}", g_stHostname);
|
||||
continue;
|
||||
}
|
||||
|
||||
TOKEN("channel")
|
||||
{
|
||||
str_to_number(g_bChannel, value_string);
|
||||
continue;
|
||||
}
|
||||
str_to_number(g_bChannel, config["CHANNEL"].c_str());
|
||||
|
||||
TOKEN("player_sql")
|
||||
{
|
||||
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(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])
|
||||
@ -422,12 +443,11 @@ void config_init(const string& st_localeServiceName)
|
||||
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]));
|
||||
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])
|
||||
@ -446,12 +466,11 @@ void config_init(const string& st_localeServiceName)
|
||||
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));
|
||||
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])
|
||||
@ -469,12 +488,7 @@ void config_init(const string& st_localeServiceName)
|
||||
|
||||
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);
|
||||
|
||||
// CONFIG_SQL_INFO_ERROR
|
||||
if (!isCommonSQL)
|
||||
|
Loading…
Reference in New Issue
Block a user