Skip to content
Myr

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

7 files+290 added−2 removed

added:examples/grpc/gin/main.go+46 added
1package main23import (4 "fmt"5 "log"6 "net/http"78 "github.com/gin-gonic/gin"9 pb "github.com/gin-gonic/gin/examples/grpc/pb"10 "google.golang.org/grpc"11)1213func 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)2122 // Set up a http setver.23 r := gin.Default()24 r.GET("/rest/n/:name", func(c *gin.Context) {25 name := c.Param("name")2627 // 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 }3637 c.JSON(http.StatusOK, gin.H{38 "result": fmt.Sprint(res.Message),39 })40 })4142 // 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
1package main23import (4 "log"5 "net"67 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)1213// server is used to implement helloworld.GreeterServer.14type server struct{}1516// SayHello implements helloworld.GreeterServer17func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {18 return &pb.HelloReply{Message: "Hello " + in.Name}, nil19}2021func 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{})2829 // 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!45/*6Package helloworld is a generated protocol buffer package.78It is generated from these files:9 helloworld.proto1011It has these top-level messages:12 HelloRequest13 HelloReply14*/15package helloworld1617import proto "github.com/golang/protobuf/proto"18import fmt "fmt"19import math "math"2021import (22 context "golang.org/x/net/context"23 grpc "google.golang.org/grpc"24)2526// Reference imports to suppress errors if they are not otherwise used.27var _ = proto.Marshal28var _ = fmt.Errorf29var _ = math.Inf3031// 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.35const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package3637// The request message containing the user's name.38type HelloRequest struct {39 Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`40}4142func (m *HelloRequest) Reset() { *m = HelloRequest{} }43func (m *HelloRequest) String() string { return proto.CompactTextString(m) }44func (*HelloRequest) ProtoMessage() {}45func (*HelloRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }4647// The response message containing the greetings48type HelloReply struct {49 Message string `protobuf:"bytes,1,opt,name=message" json:"message,omitempty"`50}5152func (m *HelloReply) Reset() { *m = HelloReply{} }53func (m *HelloReply) String() string { return proto.CompactTextString(m) }54func (*HelloReply) ProtoMessage() {}55func (*HelloReply) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }5657func init() {58 proto.RegisterType((*HelloRequest)(nil), "helloworld.HelloRequest")59 proto.RegisterType((*HelloReply)(nil), "helloworld.HelloReply")60}6162// Reference imports to suppress errors if they are not otherwise used.63var _ context.Context64var _ grpc.ClientConn6566// This is a compile-time assertion to ensure that this generated file67// is compatible with the grpc package it is being compiled against.68const _ = grpc.SupportPackageIsVersion46970// Client API for Greeter service7172type GreeterClient interface {73 // Sends a greeting74 SayHello(ctx context.Context, in *HelloRequest, opts ...grpc.CallOption) (*HelloReply, error)75}7677type greeterClient struct {78 cc *grpc.ClientConn79}8081func NewGreeterClient(cc *grpc.ClientConn) GreeterClient {82 return &greeterClient{cc}83}8485func (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}9394// Server API for Greeter service9596type GreeterServer interface {97 // Sends a greeting98 SayHello(context.Context, *HelloRequest) (*HelloReply, error)99}100101func RegisterGreeterServer(s *grpc.Server, srv GreeterServer) {102 s.RegisterService(&_Greeter_serviceDesc, srv)103}104105func _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}122123var _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}135136func init() { proto.RegisterFile("helloworld.proto", fileDescriptor0) }137138var 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.1415syntax = "proto3";1617option java_multiple_files = true;18option java_package = "io.grpc.examples.helloworld";19option java_outer_classname = "HelloWorldProto";2021package helloworld;2223// The greeting service definition.24service Greeter {25 // Sends a greeting26 rpc SayHello (HelloRequest) returns (HelloReply) {}27}2829// The request message containing the user's name.30message HelloRequest {31 string name = 1;32}3334// The response message containing the greetings35message HelloReply {36 string message = 1;37}
added:examples/grpc/README.md+19 added
modified:coverage.sh+1 added-1 removed
modified:Makefile+2 added-1 removed
1GOFMT ?= gofmt "-s"2PACKAGES ?= $(shell go list ./... | grep -v /vendor/)3VETPACKAGES ?= $(shell go list ./... | grep -v /vendor/ | grep -v /examples/)4GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")56all: install7
18 unmodified lines
26 exit 1; \27 fi;2829vet:29 go vet $(PACKAGES)30 go vet $(VETPACKAGES)3132deps:33 @hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \34 go get -u github.com/kardianos/govendor; \
28 unmodified lines