userver: /data/code/userver/libraries/easy/src/easy.cpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
easy.cpp
1#include <userver/easy.hpp>
2
3#include <fstream>
4#include <iostream>
5#include <unordered_map>
6
7#include <fmt/ranges.h>
8#include <boost/algorithm/string/replace.hpp>
9#include <boost/program_options.hpp>
10
11#include <fmt/format.h>
12#include <fmt/ranges.h>
13
14#include <userver/clients/dns/component.hpp>
15#include <userver/clients/http/component.hpp>
16#include <userver/components/component_context.hpp>
17#include <userver/components/minimal_server_component_list.hpp>
18#include <userver/components/run.hpp>
19#include <userver/server/handlers/http_handler_base.hpp>
20#include <userver/storages/postgres/component.hpp>
21#include <userver/testsuite/testsuite_support.hpp>
22#include <userver/utils/daemon_run.hpp>
23
24USERVER_NAMESPACE_BEGIN
25
26namespace easy {
27
28namespace {
29
30constexpr std::string_view kConfigBase = R"~(# yaml
31components_manager:
32 task_processors: # Task processor is an executor for coroutine tasks
33 main-task-processor: # Make a task processor for CPU-bound coroutine tasks.
34 worker_threads: 4 # Process tasks in 4 threads.
35
36 fs-task-processor: # Make a separate task processor for filesystem bound tasks.
37 worker_threads: 1
38
39 default_task_processor: main-task-processor # Task processor in which components start.
40
41 components: # Configuring components that were registered via component_list)~";
42
43constexpr std::string_view kConfigServerTemplate = R"~(
44 server:
45 listener: # configuring the main listening socket...
46 port: {} # ...to listen on this port and...
47 task_processor: main-task-processor # ...process incoming requests on this task processor.
48)~";
49
50constexpr std::string_view kConfigLoggingTemplate = R"~(
51 logging:
52 fs-task-processor: fs-task-processor
53 loggers:
54 default:
55 file_path: '@stderr'
56 level: {}
57 overflow_behavior: discard # Drop logs if the system is too busy to write them down.
58)~";
59
60constexpr std::string_view kConfigHandlerTemplate{
61 "path: {0} # Registering handler by URL '{0}'.\n"
62 "method: {1}\n"
63 "task_processor: main-task-processor # Run it on CPU bound task processor\n"};
64
65struct SharedPyaload {
66 std::unordered_map<std::string, HttpBase::Callback> http_functions;
67 std::optional<http::ContentType> default_content_type;
68 std::string db_schema;
69};
70
71SharedPyaload globals{};
72
73} // anonymous namespace
74
75namespace impl {
76
77DependenciesBase::~DependenciesBase() = default;
78
79} // namespace impl
80
81class HttpBase::Handle final : public server::handlers::HttpHandlerBase {
82public:
83 Handle(const components::ComponentConfig& config, const components::ComponentContext& context)
84 : HttpHandlerBase(config, context),
85 deps_{context.FindComponent<impl::DependenciesBase>()},
86 callback_{globals.http_functions.at(config.Name())} {}
87
88 std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&)
89 const override {
90 if (globals.default_content_type) {
91 request.GetHttpResponse().SetContentType(*globals.default_content_type);
92 }
93 return callback_(request, deps_);
94 }
95
96private:
97 const impl::DependenciesBase& deps_;
98 HttpBase::Callback& callback_;
99};
100
101HttpBase::HttpBase(int argc, const char* const argv[])
102 : argc_{argc},
103 argv_{argv},
104 static_config_{kConfigBase},
105 component_list_{components::MinimalServerComponentList()} {}
106
107HttpBase::~HttpBase() {
108 static_config_.append(fmt::format(kConfigServerTemplate, port_));
109 static_config_.append(fmt::format(kConfigLoggingTemplate, ToString(level_)));
110
111 namespace po = boost::program_options;
112 po::variables_map vm;
113 auto desc = utils::BaseRunOptions();
114 std::string config_dump;
115 std::string schema_dump;
116
117 // clang-format off
118 desc.add_options()
119 ("dump-config", po::value(&config_dump)->implicit_value(""), "path to dump the server config")
120 ("dump-db-schema", po::value(&schema_dump)->implicit_value(""), "path to dump the DB schema")
121 ("config,c", po::value<std::string>(), "path to server config")
122 ;
123 // clang-format on
124
125 po::store(po::parse_command_line(argc_, argv_, desc), vm);
126 po::notify(vm);
127
128 if (vm.count("help")) {
129 std::cerr << desc << '\n';
130 return;
131 }
132
133 if (vm.count("dump-config")) {
134 if (config_dump.empty()) {
135 std::cout << static_config_ << std::endl;
136 } else {
137 std::ofstream(config_dump) << static_config_;
138 }
139 return;
140 }
141
142 if (vm.count("dump-db-schema")) {
143 if (schema_dump.empty()) {
144 std::cout << schema_dump << std::endl;
145 } else {
146 std::ofstream(schema_dump) << globals.db_schema;
147 }
148 return;
149 }
150
151 if (argc_ <= 1) {
152 components::Run(components::InMemoryConfig{static_config_}, component_list_);
153 } else {
154 const auto ret = utils::DaemonMain(vm, component_list_);
155 if (ret != 0) {
156 std::exit(ret); // NOLINT(concurrency-mt-unsafe)
157 }
158 }
159}
160
161void HttpBase::DefaultContentType(http::ContentType content_type) { globals.default_content_type = content_type; }
162
163void HttpBase::Route(std::string_view path, Callback&& func, std::initializer_list<server::http::HttpMethod> methods) {
164 auto component_name = fmt::format("{}-{}", path, fmt::join(methods, ","));
165
166 globals.http_functions.emplace(component_name, std::move(func));
167 component_list_.Append<Handle>(component_name);
168 AddComponentConfig(component_name, fmt::format(kConfigHandlerTemplate, path, fmt::join(methods, ",")));
169}
170
171void HttpBase::AddComponentConfig(std::string_view component, std::string_view config) {
172 static_config_ += fmt::format("\n {}:", component);
173 if (config.empty()) {
174 static_config_ += " {}\n";
175 } else {
176 if (config.back() == '\n') {
177 config = std::string_view{config.data(), config.size() - 1};
178 }
179 static_config_ += boost::algorithm::replace_all_copy("\n" + std::string{config}, "\n", "\n ");
180 static_config_ += '\n';
181 }
182}
183
184void HttpBase::DbSchema(std::string_view schema) { globals.db_schema = schema; }
185
186const std::string& HttpBase::GetDbSchema() noexcept { return globals.db_schema; }
187
188void HttpBase::Port(std::uint16_t port) { port_ = port; }
189
190void HttpBase::LogLevel(logging::Level level) { level_ = level; }
191
192PgDep::PgDep(const components::ComponentContext& context)
193 : pg_cluster_(context.FindComponent<components::Postgres>("postgres").GetCluster()) {
194 const auto& db_schema = HttpBase::GetDbSchema();
195 if (!db_schema.empty()) {
196 pg_cluster_->Execute(storages::postgres::ClusterHostType::kMaster, db_schema);
197 }
198}
199
200void PgDep::RegisterOn(HttpBase& app) {
201 app.TryAddComponent<components::Postgres>(
202 "postgres",
203 "dbconnection#env: POSTGRESQL\n"
204 "dbconnection#fallback: 'postgresql://testsuite@localhost:15433/postgres'\n"
205 "blocking_task_processor: fs-task-processor\n"
206 "dns_resolver: async\n"
207 );
208
209 app.TryAddComponent<components::TestsuiteSupport>(components::TestsuiteSupport::kName, "");
210 app.TryAddComponent<clients::dns::Component>(
211 clients::dns::Component::kName, "fs-task-processor: fs-task-processor"
212 );
213}
214
215HttpDep::HttpDep(const components::ComponentContext& context)
216 : http_(context.FindComponent<components::HttpClient>().GetHttpClient()) {}
217
218void HttpDep::RegisterOn(easy::HttpBase& app) {
219 app.TryAddComponent<components::HttpClient>(
220 components::HttpClient::kName,
221 "pool-statistics-disable: false\n"
222 "thread-name-prefix: http-client\n"
223 "threads: 2\n"
224 "fs-task-processor: fs-task-processor\n"
225 );
226}
227
228} // namespace easy
229
230USERVER_NAMESPACE_END