1#include <userver/easy.hpp>
5#include <unordered_map>
7#include <boost/algorithm/string/replace.hpp>
8#include <boost/program_options.hpp>
10#include <userver/clients/dns/component.hpp>
11#include <userver/clients/http/component.hpp>
12#include <userver/components/component_context.hpp>
13#include <userver/components/minimal_server_component_list.hpp>
14#include <userver/components/run.hpp>
15#include <userver/server/handlers/http_handler_base.hpp>
16#include <userver/storages/postgres/component.hpp>
17#include <userver/testsuite/testsuite_support.hpp>
18#include <userver/utils/daemon_run.hpp>
20USERVER_NAMESPACE_BEGIN
26constexpr std::string_view kConfigBase = R"~(# yaml
27components_manager:
28 task_processors: # Task processor is an executor for coroutine tasks
29 main-task-processor: # Make a task processor for CPU-bound coroutine tasks.
30 worker_threads: 4 # Process tasks in 4 threads.
31
32 fs-task-processor: # Make a separate task processor for filesystem bound tasks.
33 worker_threads: 1
34
35 default_task_processor: main-task-processor # Task processor in which components start.
36
37 components: # Configuring components that were registered via component_list)~";
39constexpr std::string_view kConfigServerTemplate = R"~(
40 server:
41 listener: # configuring the main listening socket...
42 port: {} # ...to listen on this port and...
43 task_processor: main-task-processor # ...process incoming requests on this task processor.
44)~";
46constexpr std::string_view kConfigLoggingTemplate = R"~(
47 logging:
48 fs-task-processor: fs-task-processor
49 loggers:
50 default:
51 file_path: '@stderr'
52 level: {}
53 overflow_behavior: discard # Drop logs if the system is too busy to write them down.
54)~";
56constexpr std::string_view kConfigHandlerTemplate{
57 "path: {0} # Registering handler by URL '{0}'.\n"
59 "task_processor: main-task-processor # Run it on CPU bound task processor\n"};
62 std::unordered_map<std::string, HttpBase::Callback> http_functions;
64 std::string db_schema;
67SharedPyaload globals{};
73DependenciesBase::~DependenciesBase() =
default;
77class HttpBase::Handle
final :
public server::handlers::
HttpHandlerBase {
81 deps_{context.FindComponent<impl::DependenciesBase>()},
82 callback_{globals.http_functions.at(config.Name())} {}
84 std::string HandleRequestThrow(
const server::
http::HttpRequest& request, server::
request::RequestContext&)
86 if (globals.default_content_type) {
89 return callback_(request, deps_);
93 const impl::DependenciesBase& deps_;
94 HttpBase::Callback& callback_;
97HttpBase::HttpBase(
int argc,
const char*
const argv[])
100 static_config_{kConfigBase},
101 component_list_{components::MinimalServerComponentList()} {}
103HttpBase::~HttpBase() {
104 static_config_.append(fmt::format(kConfigServerTemplate, port_));
105 static_config_.append(fmt::format(kConfigLoggingTemplate, ToString(level_)));
107 namespace po = boost::program_options;
108 po::variables_map vm;
110 std::string config_dump;
111 std::string schema_dump;
115 (
"dump-config", po::value(&config_dump)->implicit_value(
""),
"path to dump the server config")
116 (
"dump-db-schema", po::value(&schema_dump)->implicit_value(
""),
"path to dump the DB schema")
117 (
"config,c", po::value<std::string>(),
"path to server config")
121 po::store(po::parse_command_line(argc_, argv_, desc), vm);
124 if (vm.count(
"help")) {
125 std::cerr << desc <<
'\n';
129 if (vm.count(
"dump-config")) {
130 if (config_dump.empty()) {
131 std::cout << static_config_ << std::endl;
133 std::ofstream(config_dump) << static_config_;
138 if (vm.count(
"dump-db-schema")) {
139 if (schema_dump.empty()) {
140 std::cout << schema_dump << std::endl;
142 std::ofstream(schema_dump) << globals.db_schema;
148 components::Run(components::InMemoryConfig{static_config_}, component_list_);
150 const auto ret = utils::DaemonMain(vm, component_list_);
159void HttpBase::
Route(std::string_view path, Callback&& func, std::initializer_list<server::http::HttpMethod> methods) {
160 auto component_name = fmt::format(
"{}-{}", path, fmt::join(methods,
","));
162 globals.http_functions.emplace(component_name, std::move(func));
163 component_list_.Append<Handle>(component_name);
164 AddComponentConfig(component_name, fmt::format(kConfigHandlerTemplate, path, fmt::join(methods,
",")));
167void HttpBase::AddComponentConfig(std::string_view component, std::string_view config) {
168 static_config_ += fmt::format(
"\n {}:", component);
169 if (config.empty()) {
170 static_config_ +=
" {}\n";
172 if (config.back() ==
'\n') {
173 config = std::string_view{config.data(), config.size() - 1};
175 static_config_ += boost::algorithm::replace_all_copy(
"\n" + std::string{config},
"\n",
"\n ");
176 static_config_ +=
'\n';
180void HttpBase::
DbSchema(std::string_view schema) { globals.db_schema = schema; }
182const std::string& HttpBase::
GetDbSchema()
noexcept {
return globals.db_schema; }
184void HttpBase::
Port(std::uint16_t port) { port_ = port; }
189 : pg_cluster_(context.FindComponent<components::Postgres>(
"postgres").GetCluster()) {
191 if (!db_schema.empty()) {
192 pg_cluster_->Execute(storages::postgres::ClusterHostType::kMaster, db_schema);
196void PgDep::RegisterOn(HttpBase& app) {
199 "dbconnection#env: POSTGRESQL\n"
200 "dbconnection#fallback: 'postgresql://testsuite@localhost:15433/postgres'\n"
201 "blocking_task_processor: fs-task-processor\n"
202 "dns_resolver: async\n"
205 app.TryAddComponent<components::TestsuiteSupport>(components::TestsuiteSupport::kName,
"");
206 app.TryAddComponent<clients::dns::Component>(
207 clients::dns::Component::kName,
"fs-task-processor: fs-task-processor"
212 : http_(context.FindComponent<
components::HttpClient>().GetHttpClient()) {}
215 app.TryAddComponent<components::HttpClient>(
216 components::HttpClient::kName,
217 "pool-statistics-disable: false\n"
218 "thread-name-prefix: http-client\n"
220 "fs-task-processor: fs-task-processor\n"