1#include <userver/easy.hpp>
5#include <unordered_map>
9#include <boost/algorithm/string/replace.hpp>
10#include <boost/program_options.hpp>
12#include <userver/clients/dns/component.hpp>
13#include <userver/clients/http/component.hpp>
14#include <userver/clients/http/middlewares/pipeline_component.hpp>
15#include <userver/components/component_context.hpp>
16#include <userver/components/minimal_server_component_list.hpp>
17#include <userver/components/run.hpp>
18#include <userver/server/handlers/http_handler_base.hpp>
19#include <userver/storages/postgres/component.hpp>
20#include <userver/testsuite/testsuite_support.hpp>
21#include <userver/utils/daemon_run.hpp>
23USERVER_NAMESPACE_BEGIN
29constexpr std::string_view kConfigBase = R"~(# yaml
30components_manager:
31 task_processors: # Task processor is an executor for coroutine tasks
32 main-task-processor: # Make a task processor for CPU-bound coroutine tasks.
33 worker_threads: 4 # Process tasks in 4 threads.
34
35 fs-task-processor: # Make a separate task processor for filesystem bound tasks.
36 worker_threads: 1
37
38 default_task_processor: main-task-processor # Task processor in which components start.
39
40 components: # Configuring components that were registered via component_list)~";
42constexpr std::string_view kConfigServerTemplate = R"~(
43 server:
44 listener: # configuring the main listening socket...
45 port: {} # ...to listen on this port and...
46 task_processor: main-task-processor # ...process incoming requests on this task processor.
47)~";
49constexpr std::string_view kConfigLoggingTemplate = R"~(
50 logging:
51 fs-task-processor: fs-task-processor
52 loggers:
53 default:
54 file_path: '@stderr'
55 level: {}
56 overflow_behavior: discard # Drop logs if the system is too busy to write them down.
57)~";
59constexpr std::string_view kConfigHandlerTemplate{
60 "path: {0} # Registering handler by URL '{0}'.\n"
62 "task_processor: main-task-processor # Run it on CPU bound task processor\n"
66 std::unordered_map<std::string, HttpBase::Callback> http_functions;
67 std::optional<http::
ContentType> default_content_type;
68 std::string db_schema;
71SharedPyaload globals{};
77DependenciesBase::~DependenciesBase() =
default;
86 callback_{globals.http_functions.at(config
.Name())}
89 std::string HandleRequestThrow(
const server::
http::HttpRequest& request, server::
request::RequestContext&)
91 if (globals.default_content_type) {
94 return callback_(request, deps_);
98 const impl::DependenciesBase& deps_;
99 HttpBase::Callback& callback_;
102HttpBase::HttpBase(
int argc,
const char*
const argv[])
105 static_config_{kConfigBase},
109HttpBase::~HttpBase() {
110 static_config_.append(fmt::format(kConfigServerTemplate, port_));
111 static_config_.append(fmt::format(kConfigLoggingTemplate,
ToString(level_
)));
113 namespace po = boost::program_options;
114 po::variables_map vm;
116 std::string config_dump;
117 std::string schema_dump;
121 (
"dump-config", po::value(&config_dump)->implicit_value(
""),
"path to dump the server config")
122 (
"dump-db-schema", po::value(&schema_dump)->implicit_value(
""),
"path to dump the DB schema")
123 (
"config,c", po::value<std::string>(),
"path to server config")
127 po::store(po::parse_command_line(argc_, argv_, desc), vm);
130 if (vm.count(
"help")) {
131 std::cerr << desc <<
'\n';
135 if (vm.count(
"dump-config")) {
136 if (config_dump.empty()) {
137 std::cout << static_config_ << std::endl;
139 std::ofstream(config_dump) << static_config_;
144 if (vm.count(
"dump-db-schema")) {
145 if (schema_dump.empty()) {
146 std::cout << schema_dump << std::endl;
148 std::ofstream(schema_dump) << globals.db_schema;
165void HttpBase::
Route(std::string_view path, Callback&& func, std::initializer_list<server::
http::
HttpMethod> methods) {
166 auto component_name = fmt::format(
"{}-{}", path, fmt::join(methods,
","));
168 globals.http_functions.emplace(component_name, std::move(func));
170 AddComponentConfig(component_name, fmt::format(kConfigHandlerTemplate, path, fmt::join(methods,
",")));
173void HttpBase::AddComponentConfig(std::string_view name, std::string_view config) {
174 static_config_ += fmt::format(
"\n {}:", name);
175 if (config.empty()) {
176 static_config_ +=
" {}\n";
178 if (config.back() ==
'\n') {
179 config = std::string_view{config.data(), config.size() - 1};
181 static_config_ += boost::algorithm::replace_all_copy(
"\n" + std::string{config},
"\n",
"\n ");
182 static_config_ +=
'\n';
186void HttpBase::
DbSchema(std::string_view schema) { globals.db_schema = schema; }
188const std::string& HttpBase::
GetDbSchema()
noexcept {
return globals.db_schema; }
190void HttpBase::
Port(std::uint16_t port) { port_ = port; }
198 if (!db_schema.empty()) {
203void PgDep::RegisterOn(HttpBase& app) {
206 "dbconnection#env: POSTGRESQL\n"
207 "dbconnection#fallback: 'postgresql://testsuite@localhost:15433/postgres'\n"
208 "blocking_task_processor: fs-task-processor\n"
209 "dns_resolver: async\n"
224 "pool-statistics-disable: false\n"
225 "thread-name-prefix: http-client\n"
227 "fs-task-processor: fs-task-processor\n"
230 clients
::http
::MiddlewarePipelineComponent
>(clients
::http
::MiddlewarePipelineComponent
::kName, "");