add grpc example (#1401)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
use grpc helloworld example.
authored by 田欧 · committed by Bo-Yi Wu · parent bf85b32 · Browse files · 7 files·+290 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+290 added−2 removed
- other:+290 added−2 removed
Provenance
- Commit
737d2fb- Tree
99d063c- Author
田欧 <thinkerou@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
bf85b32
7 files changed+290 added−2 removed
added:examples/grpc/gin/main.go+46 added
1+package main2+3+import (4+ "fmt"5+ "log"6+ "net/http"7+8+ "github.com/gin-gonic/gin"9+ pb "github.com/gin-gonic/gin/examples/grpc/pb"10+ "google.golang.org/grpc"11+)12+13+func main() {14+ // Set up a connection to the server.15+ conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())16+ if err != nil {17+ log.Fatalf("did not connect: %v", err)18+ }19+ defer conn.Close()20+ client := pb.NewGreeterClient(conn)21+22+ // Set up a http setver.23+ r := gin.Default()24+ r.GET("/rest/n/:name", func(c *gin.Context) {25+ name := c.Param("name")26+27+ // Contact the server and print out its response.28+ req := &pb.HelloRequest{Name: name}29+ res, err := client.SayHello(g, req)30+ if err != nil {31+ c.JSON(http.StatusInternalServerError, gin.H{32+ "error": err.Error(),33+ })34+ return35+ }36+37+ c.JSON(http.StatusOK, gin.H{38+ "result": fmt.Sprint(res.Message),39+ })40+ })41+42+ // Run http server43+ if err := r.Run(":8052"); err != nil {44+ log.Fatalf("could not run server: %v", err)45+ }46+}added:examples/grpc/grpc/server.go+34 added
1+package main2+3+import (4+ "log"5+ "net"6+7+ pb "github.com/gin-gonic/gin/examples/grpc/pb"8+ "golang.org/x/net/context"9+ "google.golang.org/grpc"10+ "google.golang.org/grpc/reflection"11+)12+13+// server is used to implement helloworld.GreeterServer.14+type server struct{}15+16+// SayHello implements helloworld.GreeterServer17+func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {18+ return &pb.HelloReply{Message: "Hello " + in.Name}, nil19+}20+21+func main() {22+ lis, err := net.Listen("tcp", ":50051")23+ if err != nil {24+ log.Fatalf("failed to listen: %v", err)25+ }26+ s := grpc.NewServer()27+ pb.RegisterGreeterServer(s, &server{})28+29+ // Register reflection service on gRPC server.30+ reflection.Register(s)31+ if err := s.Serve(lis); err != nil {32+ log.Fatalf("failed to serve: %v", err)33+ }34+}added:examples/grpc/pb/helloworld.pb.go+151 added
1+// Code generated by protoc-gen-go.2+// source: helloworld.proto3+// DO NOT EDIT!4+5+/*6+Package helloworld is a generated protocol buffer package.7+8+It is generated from these files:9+ helloworld.proto10+11+It has these top-level messages:12+ HelloRequest13+ HelloReply14+*/15+package helloworld16+17+import proto "github.com/golang/protobuf/proto"18+import fmt "fmt"19+import math "math"20+21+import (22+ context "golang.org/x/net/context"23+ grpc "google.golang.org/grpc"24+)25+26+// Reference imports to suppress errors if they are not otherwise used.27+var _ = proto.Marshal28+var _ = fmt.Errorf29+var _ = math.Inf30+31+// This is a compile-time assertion to ensure that this generated file32+// is compatible with the proto package it is being compiled against.33+// A compilation error at this line likely means your copy of the34+// proto package needs to be updated.35+const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package36+37+// The request message containing the user's name.38+type HelloRequest struct {39+ Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`40+}41+42+func (m *HelloRequest) Reset() { *m = HelloRequest{} }43+func (m *HelloRequest) String() string { return proto.CompactTextString(m) }44+func (*HelloRequest) ProtoMessage() {}45+func (*HelloRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }46+47+// The response message containing the greetings48+type HelloReply struct {49+ Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`50+}51+52+func (m *HelloReply) Reset() { *m = HelloReply{} }53+func (m *HelloReply) String() string { return proto.CompactTextString(m) }54+func (*HelloReply) ProtoMessage() {}55+func (*HelloReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }56+57+func init() {58+ proto.RegisterType((*HelloRequest)(nil), "helloworld.HelloRequest")59+ proto.RegisterType((*HelloReply)(nil), "helloworld.HelloReply")60+}61+62+// Reference imports to suppress errors if they are not otherwise used.63+var _ context.Context64+var _ grpc.ClientConn65+66+// This is a compile-time assertion to ensure that this generated file67+// is compatible with the grpc package it is being compiled against.68+const _ = grpc.SupportPackageIsVersion469+70+// Client API for Greeter service71+72+type GreeterClient interface {73+ // Sends a greeting74+ SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)75+}76+77+type greeterClient struct {78+ cc *grpc.ClientConn79+}80+81+func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {82+ return &greeterClient{cc}83+}84+85+func (c *greeterClient) SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error) {86+ out := new(HelloReply)87+ err := grpc.Invoke(ctx, "/helloworld.Greeter/SayHello", in, out, c.cc, opts...)88+ if err != nil {89+ return nil, err90+ }91+ return out, nil92+}93+94+// Server API for Greeter service95+96+type GreeterServer interface {97+ // Sends a greeting98+ SayHello(context.Context, *HelloRequest) (*HelloReply, error)99+}100+101+func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {102+ s.RegisterService(&_Greeter_serviceDesc, srv)103+}104+105+func _Greeter_SayHello_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {106+ in := new(HelloRequest)107+ if err := dec(in); err != nil {108+ return nil, err109+ }110+ if interceptor == nil {111+ return srv.(GreeterServer).SayHello(ctx, in)112+ }113+ info := &grpc.UnaryServerInfo{114+ Server: srv,115+ FullMethod: "/helloworld.Greeter/SayHello",116+ }117+ handler := func(ctx context.Context, req interface{}) (interface{}, error) {118+ return srv.(GreeterServer).SayHello(ctx, req.(*HelloRequest))119+ }120+ return interceptor(ctx, in, info, handler)121+}122+123+var _Greeter_serviceDesc = grpc.ServiceDesc{124+ ServiceName: "helloworld.Greeter",125+ HandlerType: (*GreeterServer)(nil),126+ Methods: []grpc.MethodDesc{127+ {128+ MethodName: "SayHello",129+ Handler: _Greeter_SayHello_Handler,130+ },131+ },132+ Streams: []grpc.StreamDesc{},133+ Metadata: "helloworld.proto",134+}135+136+func init() { proto.RegisterFile("helloworld.proto", fileDescriptor0) }137+138+var fileDescriptor0 = []byte{139+ // 174 bytes of a gzipped FileDescriptorProto140+ 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0xc8, 0x48, 0xcd, 0xc9,141+ 0xc9, 0x2f, 0xcf, 0x2f, 0xca, 0x49, 0xd1, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x42, 0x88,142+ 0x28, 0x29, 0x71, 0xf1, 0x78, 0x80, 0x78, 0x41, 0xa9, 0x85, 0xa5, 0xa9, 0xc5, 0x25, 0x42, 0x42,143+ 0x5c, 0x2c, 0x79, 0x89, 0xb9, 0xa9, 0x12, 0x8c, 0x0a, 0x8c, 0x1a, 0x9c, 0x41, 0x60, 0xb6, 0x92,144+ 0x1a, 0x17, 0x17, 0x54, 0x4d, 0x41, 0x4e, 0xa5, 0x90, 0x04, 0x17, 0x7b, 0x6e, 0x6a, 0x71, 0x71,145+ 0x62, 0x3a, 0x4c, 0x11, 0x8c, 0x6b, 0xe4, 0xc9, 0xc5, 0xee, 0x5e, 0x94, 0x9a, 0x5a, 0x92, 0x5a,146+ 0x24, 0x64, 0xc7, 0xc5, 0x11, 0x9c, 0x58, 0x09, 0xd6, 0x25, 0x24, 0xa1, 0x87, 0xe4, 0x02, 0x64,147+ 0xcb, 0xa4, 0xc4, 0xb0, 0xc8, 0x00, 0xad, 0x50, 0x62, 0x70, 0x32, 0xe0, 0x92, 0xce, 0xcc, 0xd7,148+ 0x4b, 0x2f, 0x2a, 0x48, 0xd6, 0x4b, 0xad, 0x48, 0xcc, 0x2d, 0xc8, 0x49, 0x2d, 0x46, 0x52, 0xeb,149+ 0xc4, 0x0f, 0x56, 0x1c, 0x0e, 0x62, 0x07, 0x80, 0xbc, 0x14, 0xc0, 0x98, 0xc4, 0x06, 0xf6, 0x9b,150+ 0x31, 0x20, 0x00, 0x00, 0xff, 0xff, 0x0f, 0xb7, 0xcd, 0xf2, 0xef, 0x00, 0x00, 0x00,151+}added:examples/grpc/pb/helloworld.proto+37 added
1+// Copyright 2015 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 at6+//7+// http://www.apache.org/licenses/LICENSE-2.08+//9+// Unless required by applicable law or agreed to in writing, software10+// 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 and13+// limitations under the License.14+15+syntax = "proto3";16+17+option java_multiple_files = true;18+option java_package = "io.grpc.examples.helloworld";19+option java_outer_classname = "HelloWorldProto";20+21+package helloworld;22+23+// The greeting service definition.24+service Greeter {25+ // Sends a greeting26+ rpc SayHello (HelloRequest) returns (HelloReply) {}27+}28+29+// The request message containing the user's name.30+message HelloRequest {31+ string name = 1;32+}33+34+// The response message containing the greetings35+message HelloReply {36+ string message = 1;37+}added:examples/grpc/README.md+19 added
12345678910111213141516171819## How to run this example
1. run grpc server
```sh$ go run grpc/server.go```
2. run gin server
```sh$ go run gin/main.go```
3. use curl command to test it
```sh$ curl -v 'http://localhost:8052/rest/n/thinkerou'```modified:coverage.sh+1 added-1 removed
2 unmodified lines3456778910112 unmodified lines2 unmodified linesset -e
echo "mode: count" > coverage.out
for d in $(go list ./... | grep -E 'gin$|binding$|render$'); dofor d in $(go list ./... | grep -E 'gin$|binding$|render$' | grep -v 'examples'); do go test -v -covermode=count -coverprofile=profile.out $d if [ -f profile.out ]; then cat profile.out | grep -v "mode:" >> coverage.out rm profile.out2 unmodified linesmodified:Makefile+2 added-1 removed
1 GOFMT ?= gofmt "-s"2 PACKAGES ?= $(shell go list ./... | grep -v /vendor/)3+VETPACKAGES ?= $(shell go list ./... | grep -v /vendor/ | grep -v /examples/)4 GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")5 6 all: install7 18 unmodified lines26 exit 1; \27 fi;28 29 vet:29- go vet $(PACKAGES)30+ go vet $(VETPACKAGES)31 32 deps:33 @hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \34 go get -u github.com/kardianos/govendor; \28 unmodified lines