| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "RcfConfig.h"
- #include <yaml-cpp/yaml.h>
- #include <fstream>
- using namespace std;
- #pragma comment(lib,"libyaml-cppmd-mt.lib")
- #define YAMLCONFIGFILEPATH "RcfConfig.yml"
- RcfConfig::RcfConfig()
- {
- try
- {
- YAML::Node node = YAML::LoadFile(YAMLCONFIGFILEPATH);
- if (node["Rcf"])
- {
- this->m_RcfIp = node["Rcf"]["IP"].as<string>();
- this->m_RcfPort = node["Rcf"]["PORT"].as<int>();
- }
- if (node["Redis"])
- {
- this->m_RedisIp = node["Redis"]["IP"].as<string>();
- this->m_RedisPort = node["Redis"]["PORT"].as<int>();
- if (node["Redis"]["PWD"].IsNull())
- this->m_RedisPwd = "";
- else
- this->m_RedisPwd = node["Redis"]["PWD"].as<string>();
- }
- if (node["Http"])
- {
- this->m_HttpPort = node["Http"]["PORT"].as<int>();
- }
- if (node["Https"])
- {
- this->m_HttpsPort = node["Https"]["PORT"].as<int>();
- }
- }
- catch (const std::exception&)
- {
- // 配置文件 读取异常,重新初始化配置文件
- YAML::Node node;
- std::ofstream fout(YAMLCONFIGFILEPATH);
- node["Rcf"]["IP"] = "127.0.0.1";
- node["Rcf"]["PORT"] = 5001;
- node["Redis"]["IP"] = "127.0.0.1";
- node["Redis"]["PORT"] = 6379;
- node["Redis"]["PWD"] = "";
- node["Http"]["PORT"] = 8000;
- node["Https"]["PORT"] = 8443;
- fout << node;
- fout.close();
- this->m_RcfIp = node["Rcf"]["IP"].as<string>();
- this->m_RcfPort = node["Rcf"]["PORT"].as<int>();
- this->m_RedisIp = node["Redis"]["IP"].as<string>();
- this->m_RedisPort = node["Redis"]["PORT"].as<int>();
- this->m_HttpPort = node["Http"]["PORT"].as<int>();
- this->m_HttpsPort = node["Https"]["PORT"].as<int>();
- }
- }
- RcfConfig RcfConfig::instance;
|