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