-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.cpp
More file actions
113 lines (98 loc) · 3.63 KB
/
Copy pathConfig.cpp
File metadata and controls
113 lines (98 loc) · 3.63 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// Created by george on 20.07.26.
//
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <string>
#include <vector>
#include <optional>
#include <stdexcept>
#include <thread>
#include "Config.h"
namespace config {
Config* Config::instance = nullptr;
/* Constructor for config:
* Config file is a yaml config file with two nodes: general and servers. Servers node contains array of servers.
* Any server has ip and port. Also, for SSL case (https server) proper SSL certificate should be provided.
*/
Config::Config(std::string const& fileName)
: mConfigFilePath(fileName)
{
YAML::Node root = YAML::LoadFile(fileName); // кине YAML::BadFile, якщо файл не знайдено
mGeneralConfig = root["general"].as<GeneralConfig>();
if (root["servers"]) {
mServerConfigs = root["servers"].as<std::vector<ServerNode>>();
}
}
/* getter for instance */
Config* Config::get() {
return Config::instance;
}
/* call for constructor for config and load config as result */
void Config::initialize(std::string const& fileName) {
try {
Config::instance = new Config(fileName);
}
catch (const YAML::BadFile& e) {
std::cerr << "Cannot get config from file " << e.what() << "\n";
Config::instance = nullptr;
}
catch (const YAML::Exception& e) {
std::cerr << "Config file error: " << e.what() << "\n";
Config::instance = nullptr;
}
}
void Config::finalize() {
delete Config::instance;
}
ServerNode& Config::server(size_t id) {
if (id > (serverCount() - 1)) throw std::out_of_range("Cannot access server with id " + std::to_string(id));
return mServerConfigs[id];
}
GeneralConfig& Config::general() {
return mGeneralConfig;
}
}
// YAML lib specific - parsing yaml
namespace YAML {
template <>
struct convert<config::ServerNode> {
static bool decode(const Node& node, config::ServerNode& rhs) {
if (!node.IsMap()) return false;
rhs.documentRoot = node["root"].as<std::string>();
rhs.ip = node["ip"].as<std::string>();
rhs.port = node["port"].as<unsigned short>();
if (node["ssl"]) {
rhs.ssl = node["ssl"].as<std::string>();
}
return true;
}
};
template <>
struct convert<config::GeneralConfig> {
static bool decode(const Node& node, config::GeneralConfig& rhs) {
if (!node.IsMap()) return false;
rhs.name = node["name"].as<std::string>();
rhs.debug = node["debug"].as<bool>(false); // значення за замовчуванням
auto coreNumber = std::thread::hardware_concurrency();
if (node["capacity"]) {
std::string capacityStr = node["capacity"].as<std::string>();
if (!capacityStr.empty() && capacityStr.length() < 4) {
if (capacityStr.rfind('%') == (capacityStr.length() - 1))
rhs.capacity = coreNumber * std::stoi(capacityStr) / 100;
else {
auto capacity = std::stoi(capacityStr);
if (capacity > 0 && capacity < coreNumber - 1)
rhs.capacity = capacity;
else
rhs.capacity = coreNumber / 2;
}
}
}
else {
rhs.capacity = 80 * coreNumber / 100;
}
return true;
}
};
} // namespace YAML