Skip to content

Commit

Permalink
add --empty-package-service
Browse files Browse the repository at this point in the history
  • Loading branch information
ktr0731 committed Feb 5, 2020
1 parent b935ffa commit dac7ffe
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 7 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions api/emptypackage.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
syntax = "proto3";

service EmptyPackageService {
rpc Unary (SimpleRequest) returns (SimpleResponse) {}
}

message SimpleRequest {
string name = 1;
}

message SimpleResponse {
string message = 1;
}
193 changes: 193 additions & 0 deletions api/emptypackage/emptypackage.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()

Expand Down
19 changes: 19 additions & 0 deletions server/emptypackage.go
Original file line number Diff line number Diff line change
@@ -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
}
21 changes: 14 additions & 7 deletions server/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -81,3 +82,9 @@ func WithCompressor(c string) Option {
o.compressor = c
}
}

func WithEmptyPackageService() Option {
return func(o *opt) {
o.emptyPackageService = true
}
}
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dac7ffe

Please sign in to comment.