-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain_config.cpp
More file actions
77 lines (63 loc) · 2.54 KB
/
main_config.cpp
File metadata and controls
77 lines (63 loc) · 2.54 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
// Copyright (c) 2023-2024 Percona and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "binsrv/main_config.hpp"
#include <cstddef>
#include <filesystem>
#include <fstream>
#include <ios>
#include <stdexcept>
#include <string>
#include <string_view>
#include <boost/json/parse.hpp>
// Needed for log_severity's operator <<
#include "binsrv/log_severity.hpp" // IWYU pragma: keep
// Needed for replication_mode_type's operator <<
#include "binsrv/replication_mode_type.hpp" // IWYU pragma: keep
// Needed for storage_backend_type's operator <<
#include "binsrv/storage_backend_type.hpp" // IWYU pragma: keep
// Needed for ssl_mode_type's operator <<
#include "easymysql/ssl_mode_type.hpp" // IWYU pragma: keep
#include "util/exception_location_helpers.hpp"
#include "util/nv_tuple_from_json.hpp"
namespace binsrv {
main_config::main_config(std::string_view file_name) {
static constexpr std::size_t max_file_size{1048576U};
const std::filesystem::path file_path{file_name};
std::ifstream ifs{file_path};
if (!ifs.is_open()) {
util::exception_location().raise<std::runtime_error>(
"cannot open configuration file");
}
auto file_size = std::filesystem::file_size(file_path);
if (file_size == 0ULL) {
util::exception_location().raise<std::out_of_range>(
"configuration file is empty");
}
if (file_size > max_file_size) {
util::exception_location().raise<std::out_of_range>(
"configuration file is too large");
}
std::string file_content(file_size, 'x');
if (!ifs.read(std::data(file_content),
static_cast<std::streamoff>(file_size))) {
util::exception_location().raise<std::runtime_error>(
"cannot read configuration file content");
}
auto json_value = boost::json::parse(file_content);
util::nv_tuple_from_json(json_value, impl_);
validate();
}
void main_config::validate() const { root().get<"connection">().validate(); }
} // namespace binsrv