From dac7ffe572f15d8f0a3d45b5228d5ac6e6819cc1 Mon Sep 17 00:00:00 2001 From: ktr0731 Date: Wed, 5 Feb 2020 18:20:15 +0900 Subject: [PATCH] add --empty-package-service --- Makefile | 1 + api/emptypackage.proto | 13 ++ api/emptypackage/emptypackage.pb.go | 193 ++++++++++++++++++++++++++++ main.go | 4 + server/emptypackage.go | 19 +++ server/option.go | 21 ++- server/server.go | 4 + 7 files changed, 248 insertions(+), 7 deletions(-) create mode 100644 api/emptypackage.proto create mode 100644 api/emptypackage/emptypackage.pb.go create mode 100644 server/emptypackage.go diff --git a/Makefile b/Makefile index e7d3adf..0a33347 100644 --- a/Makefile +++ b/Makefile @@ -3,3 +3,4 @@ SHELL := /bin/bash .PHONY: proto proto: protoc --proto_path=$(GOPATH)/src --proto_path api --go_out=plugins=grpc:api api/api.proto + protoc --proto_path=$(GOPATH)/src --proto_path api --go_out=plugins=grpc:api/emptypackage api/emptypackage.proto diff --git a/api/emptypackage.proto b/api/emptypackage.proto new file mode 100644 index 0000000..5488d8e --- /dev/null +++ b/api/emptypackage.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +service EmptyPackageService { + rpc Unary (SimpleRequest) returns (SimpleResponse) {} +} + +message SimpleRequest { + string name = 1; +} + +message SimpleResponse { + string message = 1; +} diff --git a/api/emptypackage/emptypackage.pb.go b/api/emptypackage/emptypackage.pb.go new file mode 100644 index 0000000..85b2dd8 --- /dev/null +++ b/api/emptypackage/emptypackage.pb.go @@ -0,0 +1,193 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: emptypackage.proto + +package emptypackage + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type SimpleRequest struct { + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SimpleRequest) Reset() { *m = SimpleRequest{} } +func (m *SimpleRequest) String() string { return proto.CompactTextString(m) } +func (*SimpleRequest) ProtoMessage() {} +func (*SimpleRequest) Descriptor() ([]byte, []int) { + return fileDescriptor_emptypackage_1cead27ff50271bc, []int{0} +} +func (m *SimpleRequest) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SimpleRequest.Unmarshal(m, b) +} +func (m *SimpleRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SimpleRequest.Marshal(b, m, deterministic) +} +func (dst *SimpleRequest) XXX_Merge(src proto.Message) { + xxx_messageInfo_SimpleRequest.Merge(dst, src) +} +func (m *SimpleRequest) XXX_Size() int { + return xxx_messageInfo_SimpleRequest.Size(m) +} +func (m *SimpleRequest) XXX_DiscardUnknown() { + xxx_messageInfo_SimpleRequest.DiscardUnknown(m) +} + +var xxx_messageInfo_SimpleRequest proto.InternalMessageInfo + +func (m *SimpleRequest) GetName() string { + if m != nil { + return m.Name + } + return "" +} + +type SimpleResponse struct { + Message string `protobuf:"bytes,1,opt,name=message,proto3" json:"message,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *SimpleResponse) Reset() { *m = SimpleResponse{} } +func (m *SimpleResponse) String() string { return proto.CompactTextString(m) } +func (*SimpleResponse) ProtoMessage() {} +func (*SimpleResponse) Descriptor() ([]byte, []int) { + return fileDescriptor_emptypackage_1cead27ff50271bc, []int{1} +} +func (m *SimpleResponse) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_SimpleResponse.Unmarshal(m, b) +} +func (m *SimpleResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_SimpleResponse.Marshal(b, m, deterministic) +} +func (dst *SimpleResponse) XXX_Merge(src proto.Message) { + xxx_messageInfo_SimpleResponse.Merge(dst, src) +} +func (m *SimpleResponse) XXX_Size() int { + return xxx_messageInfo_SimpleResponse.Size(m) +} +func (m *SimpleResponse) XXX_DiscardUnknown() { + xxx_messageInfo_SimpleResponse.DiscardUnknown(m) +} + +var xxx_messageInfo_SimpleResponse proto.InternalMessageInfo + +func (m *SimpleResponse) GetMessage() string { + if m != nil { + return m.Message + } + return "" +} + +func init() { + proto.RegisterType((*SimpleRequest)(nil), "SimpleRequest") + proto.RegisterType((*SimpleResponse)(nil), "SimpleResponse") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// EmptyPackageServiceClient is the client API for EmptyPackageService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type EmptyPackageServiceClient interface { + Unary(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) +} + +type emptyPackageServiceClient struct { + cc *grpc.ClientConn +} + +func NewEmptyPackageServiceClient(cc *grpc.ClientConn) EmptyPackageServiceClient { + return &emptyPackageServiceClient{cc} +} + +func (c *emptyPackageServiceClient) Unary(ctx context.Context, in *SimpleRequest, opts ...grpc.CallOption) (*SimpleResponse, error) { + out := new(SimpleResponse) + err := c.cc.Invoke(ctx, "/EmptyPackageService/Unary", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// EmptyPackageServiceServer is the server API for EmptyPackageService service. +type EmptyPackageServiceServer interface { + Unary(context.Context, *SimpleRequest) (*SimpleResponse, error) +} + +func RegisterEmptyPackageServiceServer(s *grpc.Server, srv EmptyPackageServiceServer) { + s.RegisterService(&_EmptyPackageService_serviceDesc, srv) +} + +func _EmptyPackageService_Unary_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(SimpleRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(EmptyPackageServiceServer).Unary(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/EmptyPackageService/Unary", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(EmptyPackageServiceServer).Unary(ctx, req.(*SimpleRequest)) + } + return interceptor(ctx, in, info, handler) +} + +var _EmptyPackageService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "EmptyPackageService", + HandlerType: (*EmptyPackageServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Unary", + Handler: _EmptyPackageService_Unary_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "emptypackage.proto", +} + +func init() { proto.RegisterFile("emptypackage.proto", fileDescriptor_emptypackage_1cead27ff50271bc) } + +var fileDescriptor_emptypackage_1cead27ff50271bc = []byte{ + // 148 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0xcd, 0x2d, 0x28, + 0xa9, 0x2c, 0x48, 0x4c, 0xce, 0x4e, 0x4c, 0x4f, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x57, 0x52, + 0xe6, 0xe2, 0x0d, 0xce, 0xcc, 0x2d, 0xc8, 0x49, 0x0d, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, 0x2e, 0x11, + 0x12, 0xe2, 0x62, 0xc9, 0x4b, 0xcc, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x02, 0xb3, + 0x95, 0xb4, 0xb8, 0xf8, 0x60, 0x8a, 0x8a, 0x0b, 0xf2, 0xf3, 0x8a, 0x53, 0x85, 0x24, 0xb8, 0xd8, + 0x73, 0x53, 0x8b, 0x8b, 0x13, 0xd3, 0x61, 0x0a, 0x61, 0x5c, 0x23, 0x47, 0x2e, 0x61, 0x57, 0x90, + 0x35, 0x01, 0x10, 0x6b, 0x82, 0x53, 0x8b, 0xca, 0x32, 0x93, 0x53, 0x85, 0xb4, 0xb8, 0x58, 0x43, + 0xf3, 0x12, 0x8b, 0x2a, 0x85, 0xf8, 0xf4, 0x50, 0xec, 0x93, 0xe2, 0xd7, 0x43, 0x35, 0x5a, 0x89, + 0x21, 0x89, 0x0d, 0xec, 0x34, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x87, 0x70, 0x9e, 0x4b, + 0xb0, 0x00, 0x00, 0x00, +} diff --git a/main.go b/main.go index 7348014..87c5ab7 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ func main() { cert := pflag.String("cert", "", "cert") certKey := pflag.String("cert-key", "", "cert key") rootCACert := pflag.String("root-ca-cert", "", "cert key") + emptyPackageService := pflag.Bool("empty-package-service", false, "register empty package service") pflag.Parse() var opts []server.Option @@ -41,6 +42,9 @@ func main() { if *rootCACert != "" { opts = append(opts, server.WithRootCACert(*rootCACert)) } + if *emptyPackageService { + opts = append(opts, server.WithEmptyPackageService()) + } defer server.New(opts...).Serve().Stop() diff --git a/server/emptypackage.go b/server/emptypackage.go new file mode 100644 index 0000000..d241f00 --- /dev/null +++ b/server/emptypackage.go @@ -0,0 +1,19 @@ +package server + +import ( + "context" + "fmt" + "log" + + "github.com/ktr0731/grpc-test/api/emptypackage" +) + +type EmptyPackageService struct { + logger *log.Logger +} + +func (s *EmptyPackageService) Unary(ctx context.Context, req *emptypackage.SimpleRequest) (*emptypackage.SimpleResponse, error) { + return &emptypackage.SimpleResponse{ + Message: fmt.Sprintf("hello, %s", req.GetName()), + }, nil +} diff --git a/server/option.go b/server/option.go index 630e07f..837dc33 100644 --- a/server/option.go +++ b/server/option.go @@ -8,18 +8,19 @@ type opt struct { protocol Protocol addr string - tls bool - cert, certKey string - rootCACert string - reflection bool - verbose bool - compressor string + tls bool + cert, certKey string + rootCACert string + reflection bool + verbose bool + compressor string + emptyPackageService bool } type Option func(*opt) var defaultOption = opt{ - addr: ":50051", + addr: "0.0.0.0:50051", } type Protocol int @@ -81,3 +82,9 @@ func WithCompressor(c string) Option { o.compressor = c } } + +func WithEmptyPackageService() Option { + return func(o *opt) { + o.emptyPackageService = true + } +} diff --git a/server/server.go b/server/server.go index 7a9c995..9d5bc27 100644 --- a/server/server.go +++ b/server/server.go @@ -14,6 +14,7 @@ import ( "github.com/improbable-eng/grpc-web/go/grpcweb" "github.com/ktr0731/dept/logger" "github.com/ktr0731/grpc-test/api" + "github.com/ktr0731/grpc-test/api/emptypackage" "github.com/rakyll/statik/fs" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -80,6 +81,9 @@ func New(opts ...Option) *Server { s := grpc.NewServer(grpcOpts...) api.RegisterExampleServer(s, &ExampleService{logger: logger}) + if opt.emptyPackageService { + emptypackage.RegisterEmptyPackageServiceServer(s, &EmptyPackageService{logger: logger}) + } if opt.reflection { reflection.Register(s)