-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_builder.cpp
More file actions
183 lines (155 loc) · 5.15 KB
/
Copy pathapp_builder.cpp
File metadata and controls
183 lines (155 loc) · 5.15 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Created by flexf on 13.03.2020.
//
#include "app_settings.h"
#include "log.h"
#include <flask4cpp/app_builder.h>
#include <fmt/format.h>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/log/utility/setup/console.hpp>
#include <boost/log/utility/setup/filter_parser.hpp>
#include <unordered_set>
namespace flask4cpp
{
AppBuilder::AppBuilder() = default;
AppBuilder::~AppBuilder() = default;
AppBuilder& AppBuilder::ConfigureDefault()
{
EnsureSettingsCreated();
ListenerParams params = {};
params.name = "default";
params.address = "0.0.0.0";
params.port = 80;
m_settings->listeners.emplace_back(std::move(params));
return *this;
}
AppBuilder& AppBuilder::ListenOn(const nonstd::string_view& name, const nonstd::string_view& address, uint16_t port)
{
EnsureSettingsCreated();
ListenerParams params = {};
params.name.assign(name.begin(), name.end());
params.address.assign(address.begin(), address.end());
params.port = port;
m_settings->listeners.emplace_back(std::move(params));
return *this;
}
AppBuilder& AppBuilder::ListenOnSsl(const nonstd::string_view& name,
const nonstd::string_view& address,
uint16_t port,
const nonstd::string_view& certPath,
const nonstd::string_view& keyPath)
{
EnsureSettingsCreated();
SSLListenerParams params = {};
params.name.assign(name.begin(), name.end());
params.address.assign(address.begin(), address.end());
params.port = port;
params.certPath.assign(certPath.begin(), certPath.end());
params.keyPath.assign(keyPath.begin(), keyPath.end());
m_settings->listeners.emplace_back(std::move(params));
return *this;
}
AppBuilder& AppBuilder::SetLogLevel(AppBuilder::LogLevel level)
{
EnsureSettingsCreated();
auto& logSetts = m_settings->logSettings;
using namespace boost::log;
switch (level)
{
case LogLevel::Trace:
logSetts.severity = trivial::trace;
break;
case LogLevel::Debug:
logSetts.severity = trivial::debug;
break;
case LogLevel::Info:
logSetts.severity = trivial::info;
break;
case LogLevel::Warning:
logSetts.severity = trivial::warning;
break;
case LogLevel::Error:
logSetts.severity = trivial::error;
break;
case LogLevel::Fatal:
logSetts.severity = trivial::fatal;
break;
}
return *this;
}
AppBuilder& AppBuilder::SetNumThreads(uint16_t count)
{
EnsureSettingsCreated();
m_settings->threadNum = count;
return *this;
}
namespace
{
auto InitializeLogger(const LogSettings& setts)
{
auto log = std::make_unique<LoggerType>();
if (!setts.enableConsole)
{
auto console = boost::log::add_console_log();
console->set_filter(boost::log::expressions::attr<int>("Severity") >= 10);
}
return log;
}
struct EndpointAddressBuilder
{
std::pair<std::string, std::string> operator()(const ListenerParams& l) const
{
return std::make_pair(l.name, fmt::format("http://{}:{}", l.address, l.port));
}
std::pair<std::string, std::string> operator()(const SSLListenerParams& l) const
{
return std::make_pair(l.name, fmt::format("https://{}:{}", l.address, l.port));
}
};
} // namespace
nonstd::expected<App, AppBuilder::BuildErrors> AppBuilder::CreateApp()
{
if (!m_settings)
return nonstd::make_unexpected(BuildErrors::NotInitialized);
m_settings->logger = std::move(InitializeLogger(m_settings->logSettings));
auto log = [this] { return m_settings->logger.get(); };
LOG_DEBUG() << "Start verifying configuration";
std::unordered_set<std::string> endpoints;
for (auto& l : m_settings->listeners)
{
#if __cplusplus >= 201703L
const auto& [endpoint_name, endpoint] = nonstd::visit(EndpointAddressBuilder(), l);
#else
auto endpoint_info = nonstd::visit(EndpointAddressBuilder(), l);
auto& endpoint_name = endpoint_info.first;
auto& endpoint = endpoint_info.second;
#endif
LOG_DEBUG() << "Found endpoint: " << endpoint;
if (endpoints.count(endpoint))
{
LOG_FATAL() << "Duplicate listener bindings to: " << endpoint << ". Second binding is: " << endpoint_name;
return nonstd::make_unexpected(BuildErrors::ConfigurationError);
}
#ifndef FLASK4CPP_SSL_ENABLED
if (boost::algorithm::starts_with(endpoint, "https://") || boost::algorithm::starts_with(endpoint, "wss://"))
{
LOG_FATAL() << "Unsupported protocol for endpoint: " << endpoint;
return nonstd::make_unexpected(BuildErrors::NoSSLSupport);
}
#endif
endpoints.insert(endpoint);
}
return App(m_settings.release());
}
void AppBuilder::EnsureSettingsCreated()
{
if (!m_settings)
{
m_settings = std::make_unique<AppSettings>();
#ifndef NDEBUG
m_settings->logSettings.severity = boost::log::trivial::debug;
m_settings->logSettings.enableConsole = true;
#endif
}
}
} // namespace flask4cpp