userver: /home/user/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/dns/component.hpp>
7#include <userver/clients/http/component.hpp>
8#include <userver/clients/http/component_list.hpp>
9#include <userver/components/component_context.hpp>
10#include <userver/components/minimal_server_component_list.hpp>
11#include <userver/server/handlers/http_handler_base.hpp>
12#include <userver/server/handlers/ping.hpp>
13#include <userver/server/handlers/tests_control.hpp>
14#include <userver/storages/postgres/component.hpp>
15#include <userver/testsuite/testsuite_support.hpp>
16#include <userver/utils/daemon_run.hpp>
17
18/// [ActionClient]
19class ActionClient : public components::ComponentBase {
20public:
21 static constexpr std::string_view kName = "action-client";
22
23 ActionClient(const components::ComponentConfig& config, const components::ComponentContext& context)
24 : ComponentBase{config, context},
25 service_url_(config["service-url"].As<std::string>()),
26 http_client_(context.FindComponent<components::HttpClient>().GetHttpClient())
27 {}
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)
55 : component_{config.FindComponent<ActionClient>()}
56 {}
57 auto CreateActionRequest(std::string action) const { return component_.CreateHttpRequest(std::move(action)); }
58
59 static void RegisterOn(easy::HttpBase& app) {
60 app.TryAddComponent<ActionClient>(ActionClient::kName, "service-url: http://some-service.example/v1/action");
61 easy::HttpDep::RegisterOn(app);
62 }
63
64private:
65 ActionClient& component_;
66};
67
68/// [main]
69using Deps = easy::Dependencies<ActionDep, easy::PgDep>;
70using DepsComponent = easy::DependenciesComponent<Deps>;
71
72class MyHandler final : public server::handlers::HttpHandlerBase {
73public:
74 MyHandler(const components::ComponentConfig& config, const components::ComponentContext& component_context)
75 : HttpHandlerBase(config, component_context),
76 deps_(component_context.FindComponent<DepsComponent>().GetDependencies())
77 {}
78
79 std::string HandleRequestThrow(const server::http::HttpRequest& request, server::request::RequestContext&)
80 const override {
81 const auto& action = request.GetArg("action");
82 deps_.pg().Execute(
84 "INSERT INTO events_table(action) VALUES($1)",
85 action
86 );
87 return deps_.CreateActionRequest(action)->body();
88 }
89
90private:
91 const Deps deps_;
92};
93
94int main(int argc, char* argv[]) {
95 auto component_list =
97 .Append<components::TestsuiteSupport>()
98 .AppendComponentList(clients::http::ComponentList())
99 .Append<clients::dns::Component>()
100 .Append<ActionClient>()
101 .Append<DepsComponent>()
103 .Append<MyHandler>("/log-POST");
104
105 return utils::DaemonMain(argc, argv, component_list);
106}
107/// [main]