1#include <userver/utest/using_namespace_userver.hpp>
3#include <userver/easy.hpp>
4#include <userver/yaml_config/merge_schemas.hpp>
6#include <userver/clients/http/component.hpp>
7#include <userver/components/component_context.hpp>
9#include <userver/clients/dns/component.hpp>
10#include <userver/clients/http/component.hpp>
11#include <userver/components/minimal_server_component_list.hpp>
12#include <userver/server/handlers/http_handler_base.hpp>
13#include <userver/server/handlers/ping.hpp>
14#include <userver/server/handlers/tests_control.hpp>
15#include <userver/storages/postgres/component.hpp>
16#include <userver/testsuite/testsuite_support.hpp>
17#include <userver/utils/daemon_run.hpp>
22 static constexpr std::string_view kName =
"action-client";
24 ActionClient(
const components::ComponentConfig& config,
const components::ComponentContext& context)
26 service_url_(config[
"service-url"].As<std::string>()),
27 http_client_(context.FindComponent<
components::HttpClient>().GetHttpClient()) {}
29 auto CreateHttpRequest(std::string action)
const {
30 return http_client_.CreateRequest().url(service_url_).post().data(std::move(action)).perform();
33 static yaml_config::Schema GetStaticConfigSchema() {
35 type: object
36 description: My dependencies schema
37 additionalProperties: false
38 properties:
39 service-url:
40 type: string
41 description: URL of the service to send the actions to
42 )");
46 const std::string service_url_;
47 clients::http::Client& http_client_;
54 explicit ActionDep(
const components::ComponentContext& config) : component_{config.FindComponent<ActionClient>()} {}
55 auto CreateActionRequest(std::string action)
const {
return component_.CreateHttpRequest(std::move(action)); }
57 static void RegisterOn(
easy::HttpBase& app) {
58 app.TryAddComponent<ActionClient>(ActionClient::kName,
"service-url: http://some-service.example/v1/action");
63 ActionClient& component_;
72 MyHandler(
const components::ComponentConfig& config,
const components::ComponentContext& component_context)
74 deps_(component_context.FindComponent<DepsComponent>().GetDependencies()) {}
76 std::string HandleRequestThrow(
const server::
http::HttpRequest& request, server::
request::RequestContext&)
78 const auto& action = request.GetArg(
"action");
80 storages::postgres::ClusterHostType::kMaster,
"INSERT INTO events_table(action) VALUES($1)", action
82 return deps_.CreateActionRequest(action)->body();
89int main(
int argc,
char* argv[]) {
93 .Append<clients::
dns::Component>()
94 .Append<ActionClient>()
95 .Append<DepsComponent>()
97 .Append<MyHandler>(
"/log-POST");
99 return utils::DaemonMain(argc, argv, component_list);