1 // Copyright 2024 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 <grpcpp/grpcpp.h>
16
17 #include <iostream>
18 #include <memory>
19 #include <string>
20
21 #include "absl/flags/flag.h"
22 #include "absl/flags/parse.h"
23 #include "absl/strings/str_format.h"
24 #include "helper.h"
25
26 #ifdef BAZEL_BUILD
27 #include "examples/protos/helloworld.grpc.pb.h"
28 #else
29 #include "helloworld.grpc.pb.h"
30 #endif
31
32 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
33
34 using grpc::CallbackServerContext;
35 using grpc::Server;
36 using grpc::ServerBuilder;
37 using grpc::ServerUnaryReactor;
38 using grpc::Status;
39 using helloworld::Greeter;
40 using helloworld::HelloReply;
41 using helloworld::HelloRequest;
42
43 // Logic and data behind the server's behavior.
44 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)45 ServerUnaryReactor* SayHello(CallbackServerContext* context,
46 const HelloRequest* request,
47 HelloReply* reply) override {
48 std::string prefix("Hello ");
49 reply->set_message(prefix + request->name());
50
51 ServerUnaryReactor* reactor = context->DefaultReactor();
52 reactor->Finish(Status::OK);
53 return reactor;
54 }
55 };
56
57 #ifdef BAZEL_BUILD
58 constexpr char kServerCertPath[] =
59 "examples/cpp/auth/credentials/localhost.crt";
60 constexpr char kServerKeyPath[] = "examples/cpp/auth/credentials/localhost.key";
61 #else
62 constexpr char kServerCertPath[] = "credentials/localhost.crt";
63 constexpr char kServerKeyPath[] = "credentials/localhost.key";
64 #endif
65
RunServer(uint16_t port)66 void RunServer(uint16_t port) {
67 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
68 GreeterServiceImpl service;
69 ServerBuilder builder;
70 // Load SSL credentials and build a SSL credential options
71 grpc::SslServerCredentialsOptions::PemKeyCertPair key_cert_pair = {
72 LoadStringFromFile(kServerKeyPath), LoadStringFromFile(kServerCertPath)};
73 grpc::SslServerCredentialsOptions ssl_options;
74 ssl_options.pem_key_cert_pairs.emplace_back(key_cert_pair);
75 // Listen on the given address with SSL credentials
76 builder.AddListeningPort(server_address,
77 grpc::SslServerCredentials(ssl_options));
78 // Register "service" as the instance through which we'll communicate with
79 // clients. In this case it corresponds to an *synchronous* service.
80 builder.RegisterService(&service);
81 // Finally assemble the server.
82 std::unique_ptr<Server> server(builder.BuildAndStart());
83 std::cout << "Server listening on " << server_address << std::endl;
84
85 // Wait for the server to shutdown. Note that some other thread must be
86 // responsible for shutting down the server for this call to ever return.
87 server->Wait();
88 }
89
main(int argc,char ** argv)90 int main(int argc, char** argv) {
91 absl::ParseCommandLine(argc, argv);
92 RunServer(absl::GetFlag(FLAGS_port));
93 return 0;
94 }
95