-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
71 lines (63 loc) · 1.51 KB
/
config.cpp
File metadata and controls
71 lines (63 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Created by root on 2025/7/29.
//
#include "config.h"
#include "yaml-cpp/yaml.h"
#include "log.h"
config& config::instance()
{
static config instance;
return instance;
}
bool config::loadConfig(const std::filesystem::path& path)
{
config&config = instance();
try
{
YAML::Node yaml= YAML::LoadFile(path);
std::vector<LogicHost>logicHosts;
auto bindAddr=yaml["listen"]["host"].as<std::string>();
auto bindPort=yaml["listen"]["port"].as<short>();
for (const auto& logicHost : yaml["logical_hosts"])
{
logicHosts.emplace_back(
logicHost["host"].as<std::string>(),
logicHost["name"].as<std::string>()
,logicHost["connect"].as<size_t>());
}
config.setLogicHosts(logicHosts);
config.setBindAddr(bindAddr);
config.setBindPort(bindPort);
LOG(LOG_INFO, "load config success!","")
return true;
}
catch (const std::exception& e)
{
LOG(LOG_ERR, "load config occurred exception: %s", e.what());
return false;
}
}
std::vector<LogicHost> config::getLogicHosts() const
{
return _logic_hosts;
}
std::string config::getBindAddr() const
{
return _bind_addr;
}
short config::getBindPort() const
{
return _bind_port;
}
void config::setBindPort(short port)
{
_bind_port=port;
}
void config::setBindAddr(const std::string& addr)
{
_bind_addr=addr;
}
void config::setLogicHosts(const std::vector<LogicHost>& hosts)
{
_logic_hosts=hosts;
}