1 // Copyright 2022 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 "examples/protos/helloworld.grpc.pb.h"
20
21 #include <grpcpp/ext/proto_server_reflection_plugin.h>
22 #include <grpcpp/grpcpp.h>
23 #include <grpcpp/health_check_service_interface.h>
24
25 using grpc::Server;
26 using grpc::ServerBuilder;
27 using grpc::ServerContext;
28 using grpc::Status;
29 using helloworld::Greeter;
30 using helloworld::HelloReply;
31 using helloworld::HelloRequest;
32
33 // Logic and data behind the server's behavior.
34 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)35 Status SayHello(ServerContext* context, const HelloRequest* request,
36 HelloReply* reply) override {
37 std::string prefix("Hello ");
38 reply->set_message(prefix + request->name());
39 return Status::OK;
40 }
41 };
42
RunServer()43 void RunServer() {
44 std::string server_address("unix:/tmp/server");
45 GreeterServiceImpl service;
46
47 grpc::EnableDefaultHealthCheckService(true);
48 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
49 ServerBuilder builder;
50 // Listen on the given address without any authentication mechanism.
51 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
52 // Register "service" as the instance through which we'll communicate with
53 // clients. In this case it corresponds to an *synchronous* service.
54 builder.RegisterService(&service);
55 // Finally assemble the server.
56 std::unique_ptr<Server> server(builder.BuildAndStart());
57 std::cout << "Server listening on " << server_address << std::endl;
58
59 // Wait for the server to shutdown. Note that some other thread must be
60 // responsible for shutting down the server for this call to ever return.
61 server->Wait();
62 }
63
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65 RunServer();
66
67 return 0;
68 }
69