1 // Copyright 2023 The gRPC Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <iostream>
16 #include <memory>
17 #include <string>
18
19 #include "absl/flags/flag.h"
20 #include "absl/flags/parse.h"
21 #include "absl/strings/str_format.h"
22
23 #include <grpcpp/ext/admin_services.h>
24 #include <grpcpp/ext/proto_server_reflection_plugin.h>
25 #include <grpcpp/grpcpp.h>
26 #include <grpcpp/health_check_service_interface.h>
27
28 #ifdef BAZEL_BUILD
29 #include "examples/protos/helloworld.grpc.pb.h"
30 #else
31 #include "helloworld.grpc.pb.h"
32 #endif
33
34 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
35
36 using grpc::CallbackServerContext;
37 using grpc::Server;
38 using grpc::ServerBuilder;
39 using grpc::ServerUnaryReactor;
40 using grpc::Status;
41 using helloworld::Greeter;
42 using helloworld::HelloReply;
43 using helloworld::HelloRequest;
44
45 // Logic and data behind the server's behavior.
46 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)47 ServerUnaryReactor* SayHello(CallbackServerContext* context,
48 const HelloRequest* request,
49 HelloReply* reply) override {
50 std::string prefix("Hello ");
51 reply->set_message(prefix + request->name());
52
53 ServerUnaryReactor* reactor = context->DefaultReactor();
54 reactor->Finish(Status::OK);
55 return reactor;
56 }
57 };
58
RunServer(uint16_t port)59 void RunServer(uint16_t port) {
60 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
61 GreeterServiceImpl service;
62
63 grpc::EnableDefaultHealthCheckService(true);
64 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
65 ServerBuilder builder;
66 // Enable gRPC Admin Services
67 grpc::AddAdminServices(&builder);
68 // Listen on the given address without any authentication mechanism.
69 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
70 // Register "service" as the instance through which we'll communicate with
71 // clients. In this case it corresponds to an *synchronous* service.
72 builder.RegisterService(&service);
73 // Finally assemble the server.
74 std::unique_ptr<Server> server(builder.BuildAndStart());
75 std::cout << "Server listening on " << server_address << std::endl;
76
77 // Wait for the server to shutdown. Note that some other thread must be
78 // responsible for shutting down the server for this call to ever return.
79 server->Wait();
80 }
81
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83 absl::ParseCommandLine(argc, argv);
84 RunServer(absl::GetFlag(FLAGS_port));
85 return 0;
86 }
87