diff options
Diffstat (limited to 'src/config.cpp')
-rw-r--r-- | src/config.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/src/config.cpp b/src/config.cpp new file mode 100644 index 0000000..aec29a4 --- /dev/null +++ b/src/config.cpp | |||
@@ -0,0 +1,85 @@ | |||
1 | #include <filesystem> | ||
2 | #include <vector> | ||
3 | #include <fstream> | ||
4 | #include <format> | ||
5 | #include <utility> | ||
6 | |||
7 | #include <ryml.hpp> | ||
8 | #include <ryml_std.hpp> | ||
9 | #include <mw/error.hpp> | ||
10 | |||
11 | #include "config.hpp" | ||
12 | |||
13 | namespace { | ||
14 | |||
15 | mw::E<std::vector<char>> readFile(const std::filesystem::path& path) | ||
16 | { | ||
17 | std::ifstream f(path, std::ios::binary); | ||
18 | std::vector<char> content; | ||
19 | content.reserve(102400); | ||
20 | content.assign(std::istreambuf_iterator<char>(f), | ||
21 | std::istreambuf_iterator<char>()); | ||
22 | if(f.bad() || f.fail()) | ||
23 | { | ||
24 | return std::unexpected(mw::runtimeError( | ||
25 | std::format("Failed to read file {}", path.string()))); | ||
26 | } | ||
27 | |||
28 | return content; | ||
29 | } | ||
30 | |||
31 | } // namespace | ||
32 | |||
33 | mw::E<Configuration> Configuration::fromYaml(const std::filesystem::path& path) | ||
34 | { | ||
35 | auto buffer = readFile(path); | ||
36 | if(!buffer.has_value()) | ||
37 | { | ||
38 | return std::unexpected(buffer.error()); | ||
39 | } | ||
40 | |||
41 | ryml::Tree tree = ryml::parse_in_place(ryml::to_substr(*buffer)); | ||
42 | Configuration config; | ||
43 | if(tree["listen-address"].readable()) | ||
44 | { | ||
45 | tree["listen-address"] >> config.listen_address; | ||
46 | } | ||
47 | if(tree["listen-port"].readable()) | ||
48 | { | ||
49 | tree["listen-port"] >> config.listen_port; | ||
50 | } | ||
51 | if(tree["socket-user"].readable()) | ||
52 | { | ||
53 | tree["socket-user"] >> config.socket_user; | ||
54 | } | ||
55 | if(tree["socket-group"].readable()) | ||
56 | { | ||
57 | tree["socket-group"] >> config.socket_group; | ||
58 | } | ||
59 | if(tree["socket-permission"].readable()) | ||
60 | { | ||
61 | tree["socket-permission"] >> config.socket_permission; | ||
62 | } | ||
63 | if(tree["base-url"].readable()) | ||
64 | { | ||
65 | tree["base-url"] >> config.base_url; | ||
66 | } | ||
67 | if(tree["data-dir"].readable()) | ||
68 | { | ||
69 | tree["data-dir"] >> config.data_dir; | ||
70 | } | ||
71 | if(tree["openid-url-prefix"].readable()) | ||
72 | { | ||
73 | tree["openid-url-prefix"] >> config.openid_url_prefix; | ||
74 | } | ||
75 | if(tree["client-id"].readable()) | ||
76 | { | ||
77 | tree["client-id"] >> config.client_id; | ||
78 | } | ||
79 | if(tree["client-secret"].readable()) | ||
80 | { | ||
81 | tree["client-secret"] >> config.client_secret; | ||
82 | } | ||
83 | |||
84 | return mw::E<Configuration>{std::in_place, std::move(config)}; | ||
85 | } | ||