From 81580a97bbe5411e8576409a2b681a34a070a165 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Fri, 20 Feb 2015 03:47:37 -0800 Subject: [PATCH 1/4] Adds a helloworld sample in Go. --- go/README.md | 51 +++++++++++++++ go/greeter_client/main.go | 70 +++++++++++++++++++++ go/greeter_server/main.go | 65 ++++++++++++++++++++ go/helloworld/helloworld.pb.go | 109 +++++++++++++++++++++++++++++++++ 4 files changed, 295 insertions(+) create mode 100644 go/README.md create mode 100644 go/greeter_client/main.go create mode 100644 go/greeter_server/main.go create mode 100644 go/helloworld/helloworld.pb.go diff --git a/go/README.md b/go/README.md new file mode 100644 index 00000000000..df81915c9b7 --- /dev/null +++ b/go/README.md @@ -0,0 +1,51 @@ +gRPC Go Hello World +=================== + +PREREQUISITES +------------- + +- This requires Go 1.4.x +- Requires that [GOPATH is set](https://golang.org/doc/code.html#GOPATH) +```sh +$ go help gopath +$ # ensure the PATH contains $GOPATH/bin or $GOBIN +$ export PATH=PATH: +``` + +INSTALL +------- + +```sh +$ export GOPATH= +$ go install -u github.com/grpc-common/go/greeter_client +$ go install -u github.com/grpc-common/go/greeter_server +``` + +OPTIONAL - Rebuild the generated code +------------------------------------- + +1 First [install protoc](https://github.com/google/protobuf/blob/master/INSTALL.txt) + - For now, this needs to be installed from source + - This is will change once proto3 is officially released +2 Install the protoc Go plugin. +```sh +$ go install -a github.com/golang/protobuf/protoc-gen-go +$ # ensure the PATH contains $GOPATH/bin or $GOBIN +$ export PATH=PATH: +$ +$ # from ths dir; invoke protoc +$ protoc -I ../protos ../protos/helloworld.proto --go_out=plugins=grpc:. +``` + +TRY IT! +------- + +- Run the server +```sh +$ greeter_server & +``` + +- Run the client +```sh +$ greeter_client +``` diff --git a/go/greeter_client/main.go b/go/greeter_client/main.go new file mode 100644 index 00000000000..abe33c0e2ad --- /dev/null +++ b/go/greeter_client/main.go @@ -0,0 +1,70 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +package main + +import ( + "log" + "os" + + pb "github.com/grpc-common/go/helloworld" + "golang.org/x/net/context" + "google.golang.org/grpc" +) + +const ( + address = "localhost:50051" + defaultName = "world" +) + +func main() { + // Set up a connection to the server. + var opts []grpc.DialOption + conn, err := grpc.Dial(address, opts...) + if err != nil { + log.Fatalf("did not connect: %v", err) + } + defer conn.Close() + + // Contact the server and print out its response. + name := defaultName + if len(os.Args) > 1 { + name = os.Args[1] + } + c := pb.NewGreeterClient(conn) + r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name}) + if err != nil { + log.Fatalf("could not greet: %v", err) + } + log.Printf("Greeting: %s", r.Message) +} diff --git a/go/greeter_server/main.go b/go/greeter_server/main.go new file mode 100644 index 00000000000..643524bde76 --- /dev/null +++ b/go/greeter_server/main.go @@ -0,0 +1,65 @@ +/* + * + * Copyright 2015, Google Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Google Inc. nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +package main + +import ( + "log" + "net" + + pb "github.com/grpc-common/go/helloworld" + "golang.org/x/net/context" + "google.golang.org/grpc" +) + +const ( + port = ":50051" +) + +// server is used to implement hellowrld.GreeterServer. +type server struct{} + +// SayHello implements helloworld.GreeterServer +func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) { + return &pb.HelloReply{Message: "Hello " + in.Name}, nil +} + +func main() { + l, err := net.Listen("tcp", port) + if err != nil { + log.Fatalf("failed to listen: %v", err) + } + s := grpc.NewServer() + pb.RegisterGreeterServer(s, &server{}) + s.Serve(l) +} diff --git a/go/helloworld/helloworld.pb.go b/go/helloworld/helloworld.pb.go new file mode 100644 index 00000000000..399adda5eaf --- /dev/null +++ b/go/helloworld/helloworld.pb.go @@ -0,0 +1,109 @@ +// Code generated by protoc-gen-go. +// source: helloworld.proto +// DO NOT EDIT! + +/* +Package helloworld is a generated protocol buffer package. + +It is generated from these files: + helloworld.proto + +It has these top-level messages: + HelloRequest + HelloReply +*/ +package helloworld + +import proto "github.com/golang/protobuf/proto" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal + +// The request message containing the user's name. +type HelloRequest struct { + Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"` +} + +func (m *HelloRequest) Reset() { *m = HelloRequest{} } +func (m *HelloRequest) String() string { return proto.CompactTextString(m) } +func (*HelloRequest) ProtoMessage() {} + +// The response message containing the greetings +type HelloReply struct { + Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"` +} + +func (m *HelloReply) Reset() { *m = HelloReply{} } +func (m *HelloReply) String() string { return proto.CompactTextString(m) } +func (*HelloReply) ProtoMessage() {} + +func init() { +} + +// Client API for Greeter service + +type GreeterClient interface { + // Sends a greeting + SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) +} + +type greeterClient struct { + cc *grpc.ClientConn +} + +func NewGreeterClient(cc *grpc.ClientConn) GreeterClient { + return &greeterClient{cc} +} + +func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) { + out := new(HelloReply) + err := grpc.Invoke(ctx, "/helloworld.Greeter/sayHello", in, out, c.cc, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Greeter service + +type GreeterServer interface { + // Sends a greeting + SayHello(context.Context, *HelloRequest) (*HelloReply, error) +} + +func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) { + s.RegisterService(&_Greeter_serviceDesc, srv) +} + +func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, buf []byte) (proto.Message, error) { + in := new(HelloRequest) + if err := proto.Unmarshal(buf, in); err != nil { + return nil, err + } + out, err := srv.(GreeterServer).SayHello(ctx, in) + if err != nil { + return nil, err + } + return out, nil +} + +var _Greeter_serviceDesc = grpc.ServiceDesc{ + ServiceName: "helloworld.Greeter", + HandlerType: (*GreeterServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "sayHello", + Handler: _Greeter_SayHello_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, +} From ea8eb3c7030da5a4396eba646b5f5e6291413030 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Fri, 20 Feb 2015 07:48:04 -0800 Subject: [PATCH 2/4] Pushes the optional code-gen section to the bottom --- go/README.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/go/README.md b/go/README.md index df81915c9b7..2bcb5ab0a91 100644 --- a/go/README.md +++ b/go/README.md @@ -21,8 +21,21 @@ $ go install -u github.com/grpc-common/go/greeter_client $ go install -u github.com/grpc-common/go/greeter_server ``` -OPTIONAL - Rebuild the generated code -------------------------------------- +TRY IT! +------- + +- Run the server +```sh +$ greeter_server & +``` + +- Run the client +```sh +$ greeter_client +``` + +OPTIONAL - Rebuilding the generated code +---------------------------------------- 1 First [install protoc](https://github.com/google/protobuf/blob/master/INSTALL.txt) - For now, this needs to be installed from source @@ -36,16 +49,3 @@ $ $ # from ths dir; invoke protoc $ protoc -I ../protos ../protos/helloworld.proto --go_out=plugins=grpc:. ``` - -TRY IT! -------- - -- Run the server -```sh -$ greeter_server & -``` - -- Run the client -```sh -$ greeter_client -``` From 4ee7225fbf09b1944dd2a4a31f35dae6f3929047 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Fri, 20 Feb 2015 08:14:34 -0800 Subject: [PATCH 3/4] Changed the documentation title --- go/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/README.md b/go/README.md index 2bcb5ab0a91..d464b45d364 100644 --- a/go/README.md +++ b/go/README.md @@ -1,5 +1,5 @@ -gRPC Go Hello World -=================== +gRPC in 3 minutes (Go) +====================== PREREQUISITES ------------- From 8c0464dcf64507d028601abe97e003720c656922 Mon Sep 17 00:00:00 2001 From: Tim Emiola Date: Fri, 20 Feb 2015 14:19:31 -0800 Subject: [PATCH 4/4] Fixes var name --- go/greeter_server/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go/greeter_server/main.go b/go/greeter_server/main.go index 643524bde76..231f5c607ba 100644 --- a/go/greeter_server/main.go +++ b/go/greeter_server/main.go @@ -55,11 +55,11 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe } func main() { - l, err := net.Listen("tcp", port) + lis, err := net.Listen("tcp", port) if err != nil { log.Fatalf("failed to listen: %v", err) } s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) - s.Serve(l) + s.Serve(lis) }