userver: samples/grpc_service/src/greeter_service.cpp
Loading...
Searching...
No Matches
samples/grpc_service/src/greeter_service.cpp
#include <greeter_service.hpp>
#include <string_view>
#include <utility>
#include <fmt/format.h>
#include <samples/greeter_client.usrv.pb.hpp>
#include <samples/greeter_service.usrv.pb.hpp>
namespace samples {
GreeterService::GreeterService(std::string prefix)
: prefix_(std::move(prefix)) {}
void GreeterService::SayHello(api::GreeterServiceBase::SayHelloCall& call,
api::GreetingRequest&& request) {
// Authentication checking could have gone here.
// Or even better, use a global gRPC authentication middleware.
api::GreetingResponse response;
response.set_greeting(fmt::format("{}, {}!", prefix_, request.name()));
// Complete the RPC by sending the response. The service should complete
// each request by calling `Finish` or `FinishWithError`, otherwise the
// client will receive an Internal Error (500) response.
call.Finish(response);
}
void GreeterService::SayHelloResponseStream(
api::GreeterServiceBase::SayHelloResponseStreamCall& call,
api::GreetingRequest&& request) {
std::string message = fmt::format("{}, {}", prefix_, request.name());
api::GreetingResponse response;
constexpr auto kCountSend = 5;
for (auto i = 0; i < kCountSend; ++i) {
message.push_back('!');
response.set_greeting(grpc::string(message));
call.Write(response);
}
call.Finish();
}
void GreeterService::SayHelloRequestStream(
api::GreeterServiceBase::SayHelloRequestStreamCall& call) {
std::string message{};
api::GreetingRequest request;
while (call.Read(request)) {
message.append(request.name());
}
api::GreetingResponse response;
response.set_greeting(fmt::format("{}, {}", prefix_, message));
call.Finish(response);
}
void GreeterService::SayHelloStreams(
api::GreeterServiceBase::SayHelloStreamsCall& call) {
std::string message;
api::GreetingRequest request;
api::GreetingResponse response;
while (call.Read(request)) {
message.append(request.name());
response.set_greeting(fmt::format("{}, {}", prefix_, message));
call.Write(response);
}
call.Finish();
}
GreeterServiceComponent::GreeterServiceComponent(
service_(config["greeting-prefix"].As<std::string>()) {
RegisterService(service_);
}
yaml_config::Schema GreeterServiceComponent::GetStaticConfigSchema() {
return yaml_config::MergeSchemas<ugrpc::server::ServiceComponentBase>(R"(
type: object
description: gRPC sample greater service component
additionalProperties: false
properties:
greeting-prefix:
type: string
description: greeting prefix
)");
}
} // namespace samples