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 <iostream>
20 #include <memory>
21 #include <string>
22
23 #include "absl/flags/flag.h"
24 #include "absl/flags/parse.h"
25 #include "absl/strings/str_cat.h"
26
27 #include <grpcpp/ext/admin_services.h>
28 #include <grpcpp/ext/proto_server_reflection_plugin.h>
29 #include <grpcpp/grpcpp.h>
30 #include <grpcpp/health_check_service_interface.h>
31 #include <grpcpp/xds_server_builder.h>
32
33 #ifdef BAZEL_BUILD
34 #include "examples/protos/helloworld.grpc.pb.h"
35 #else
36 #include "helloworld.grpc.pb.h"
37 #endif
38
39 ABSL_FLAG(int32_t, port, 50051, "Server port for service.");
40 ABSL_FLAG(int32_t, maintenance_port, 50052,
41 "Server port for maintenance if --secure is used.");
42 ABSL_FLAG(bool, secure, true, "Secure mode");
43
44 using grpc::Server;
45 using grpc::ServerBuilder;
46 using grpc::ServerContext;
47 using grpc::Status;
48 using helloworld::Greeter;
49 using helloworld::HelloReply;
50 using helloworld::HelloRequest;
51
52 // Logic and data behind the server's behavior.
53 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)54 Status SayHello(ServerContext* context, const HelloRequest* request,
55 HelloReply* reply) override {
56 std::string prefix("Hello ");
57 reply->set_message(prefix + request->name());
58 return Status::OK;
59 }
60 };
61
RunServer()62 void RunServer() {
63 grpc::EnableDefaultHealthCheckService(true);
64 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
65 int port = absl::GetFlag(FLAGS_port);
66 int maintenance_port = absl::GetFlag(FLAGS_maintenance_port);
67 grpc::XdsServerBuilder xds_builder;
68 ServerBuilder builder;
69 std::unique_ptr<Server> xds_enabled_server;
70 std::unique_ptr<Server> server;
71 GreeterServiceImpl service;
72 // Register "service" as the instance through which we'll communicate with
73 // clients. In this case it corresponds to an *synchronous* service.
74 xds_builder.RegisterService(&service);
75 if (absl::GetFlag(FLAGS_secure)) {
76 // Listen on the given address with XdsServerCredentials and a fallback of
77 // InsecureServerCredentials
78 xds_builder.AddListeningPort(
79 absl::StrCat("0.0.0.0:", port),
80 grpc::XdsServerCredentials(grpc::InsecureServerCredentials()));
81 xds_enabled_server = xds_builder.BuildAndStart();
82 gpr_log(GPR_INFO, "Server starting on 0.0.0.0:%d", port);
83 grpc::AddAdminServices(&builder);
84 // For the maintenance server, do not use any authentication mechanism.
85 builder.AddListeningPort(absl::StrCat("0.0.0.0:", maintenance_port),
86 grpc::InsecureServerCredentials());
87 server = builder.BuildAndStart();
88 gpr_log(GPR_INFO, "Maintenance server listening on 0.0.0.0:%d",
89 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 gpr_log(GPR_INFO, "Server listening on 0.0.0.0:%d", 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