userver: /data/code/userver/libraries/easy/samples/6_pg_service_template_no_http_with/src/main.cpp Source File
Loading...
Searching...
No Matches
main.cpp
1#include <userver/utest/using_namespace_userver.hpp> // Note: this is for the purposes of samples only
2
3#include <userver/easy.hpp>
4#include <userver/yaml_config/merge_schemas.hpp>
5
6#include <userver/clients/http/component.hpp>
7#include <userver/components/component_context.hpp>
8
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>
18
19/// [ActionClient]
20class ActionClient : public components::ComponentBase {
21public:
22 static constexpr std::string_view kName = "action-client";
23
24 ActionClient(const components::ComponentConfig& config, const components::ComponentContext& context)
25 : ComponentBase{config, context},
26 service_url_(config["service-url"].As<std::string>()),
27 http_client_(context.FindComponent<components::HttpClient>().GetHttpClient()) {}
28
29 auto CreateHttpRequest(std::string action) const {
30 return http_client_.CreateRequest().url(service_url_).post().data(std::move(action)).perform();
31 }
32
33 static yaml_config::Schema GetStaticConfigSchema() {
34 return yaml_config::MergeSchemas<components::ComponentBase>(R"(
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 )");
43 }
44
45private:
46 const std::string service_url_;
47 clients::http::Client& http_client_;
48};
49/// [ActionClient]
50
51/// [ActionDep]
52class ActionDep {
53public:
54 explicit ActionDep(const components::ComponentContext& config) : component_{config.FindComponent<ActionClient>()} {}
55 auto CreateActionRequest(std::string action) const { return component_.CreateHttpRequest(std::move(action)); }
56
57 static void RegisterOn(easy::HttpBase& app) {
58 app.TryAddComponent<ActionClient>(ActionClient::kName, "service-url: http://some-service.example/v1/action");
59 easy::HttpDep::RegisterOn(app);
60 }
61
62private:
63 ActionClient& component_;
64};
65
66/// [main]
67using Deps = easy::Dependencies<ActionDep, easy::PgDep>;
68using DepsComponent = easy::DependenciesComponent<Deps>;
69
70class MyHandler final : public server::handlers::HttpHandlerBase {
71public:
72 MyHandler(const components::ComponentConfig& config, const components::ComponentContext& component_context)
73 : HttpHandlerBase(config, component_context),
74 deps_(component_context.FindComponent<DepsComponent>().GetDependencies()) {}
75
76 std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&)
77 const override {
78 const auto& action = request.GetArg("action");
79 deps_.pg().Execute(
80 storages::postgres::ClusterHostType::kMaster, "INSERT INTO events_table(action) VALUES($1)", action
81 );
82 return deps_.CreateActionRequest(action)->body();
83 }
84
85private:
86 const Deps deps_;
87};
88
89int main(int argc, char* argv[]) {
91 .Append<components::TestsuiteSupport>()
92 .Append<components::HttpClient>()
93 .Append<clients::dns::Component>()
94 .Append<ActionClient>()
95 .Append<DepsComponent>()
96 .Append<components::Postgres>("postgres")
97 .Append<MyHandler>("/log-POST");
98
99 return utils::DaemonMain(argc, argv, component_list);
100}
101/// [main]