1 /*
2 *
3 * Copyright 2021 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::Server;
46 using grpc::ServerBuilder;
47 using grpc::ServerContext;
48 using grpc::Status;
49 using helloworld::Greeter;
50 using helloworld::HelloReply;
51 using helloworld::HelloRequest;
52
53 // Logic and data behind the server's behavior.
54 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)55 Status SayHello(ServerContext* context, const HelloRequest* request,
56 HelloReply* reply) override {
57 std::string prefix("Hello ");
58 reply->set_message(prefix + request->name());
59 return Status::OK;
60 }
61 };
62
RunServer()63 void RunServer() {
64 grpc::EnableDefaultHealthCheckService(true);
65 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
66 int port = absl::GetFlag(FLAGS_port);
67 int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
68 grpc::XdsServerBuilder xds_builder;
69 ServerBuilder builder;
70 std::unique_ptr<Server> xds_enabled_server;
71 std::unique_ptr<Server> server;
72 GreeterServiceImpl service;
73 // Register "service" as the instance through which we'll communicate with
74 // clients. In this case it corresponds to an *synchronous* service.
75 xds_builder.RegisterService(&service);
76 if (absl::GetFlag(FLAGS_secure)) {
77 // Listen on the given address with XdsServerCredentials and a fallback of
78 // InsecureServerCredentials
79 xds_builder.AddListeningPort(
80 absl::StrCat("0.0.0.0:", port),
81 grpc::XdsServerCredentials(grpc::InsecureServerCredentials()));
82 xds_enabled_server = xds_builder.BuildAndStart();
83 LOG(INFO) << "Server starting on 0.0.0.0:" << port;
84 grpc::AddAdminServices(&builder);
85 // For the maintenance server, do not use any authentication mechanism.
86 builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
87 grpc::InsecureServerCredentials());
88 server = builder.BuildAndStart();
89 LOG(INFO) << "Maintenance server listening on 0.0.0.0:" << maintenance_port;
90 } else {
91 grpc::AddAdminServices(&xds_builder);
92 // Listen on the given address without any authentication mechanism.
93 builder.AddListeningPort(absl::StrCat("0.0.0.0:", port),
94 grpc::InsecureServerCredentials());
95 server = xds_builder.BuildAndStart();
96 LOG(INFO) << "Server listening on 0.0.0.0:" << port;
97 }
98
99 // Wait for the server to shutdown. Note that some other thread must be
100 // responsible for shutting down the server for this call to ever return.
101 server->Wait();
102 }
103
main(int argc,char ** argv)104 int main(int argc, char** argv) {
105 absl::ParseCommandLine(argc, argv);
106 RunServer();
107 return 0;
108 }
109