Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement gRPC for echo and version APIs #322

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/podinfo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/stefanprodan/podinfo/pkg/api"
"github.com/stefanprodan/podinfo/pkg/grpc"
"github.com/stefanprodan/podinfo/pkg/api/grpc"
"github.com/stefanprodan/podinfo/pkg/api/http"
"github.com/stefanprodan/podinfo/pkg/signals"
"github.com/stefanprodan/podinfo/pkg/version"
go_grpc "google.golang.org/grpc"
Expand Down Expand Up @@ -138,11 +138,13 @@ func main() {
var grpcServer *go_grpc.Server
if grpcCfg.Port > 0 {
grpcSrv, _ := grpc.NewServer(&grpcCfg, logger)
//grpcinfoSrv, _ := grpc.NewInfoServer(&grpcCfg)

grpcServer = grpcSrv.ListenAndServe()
}

// load HTTP server config
var srvCfg api.Config
var srvCfg http.Config
if err := viper.Unmarshal(&srvCfg); err != nil {
logger.Panic("config unmarshal failed", zap.Error(err))
}
Expand All @@ -155,7 +157,7 @@ func main() {
)

// start HTTP server
srv, _ := api.NewServer(&srvCfg, logger)
srv, _ := http.NewServer(&srvCfg, logger)
httpServer, httpsServer, healthy, ready := srv.ListenAndServe()

// graceful shutdown
Expand Down
20 changes: 20 additions & 0 deletions pkg/api/grpc/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package grpc

import (
"context"

"github.com/stefanprodan/podinfo/pkg/api/grpc/echo"
"go.uber.org/zap"
)

type echoServer struct {
echo.UnimplementedEchoServiceServer
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this call Unimplemented?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's something protoc-gen-grpc-go compiler now throws an error for in newer version must be embedded to have forward compatible implementations., it act as a placeholder or a default behavior for any methods that have not yet been implemented in your code

config *Config
logger *zap.Logger
}

func (s *echoServer) Echo (ctx context.Context, message *echo.Message) (*echo.Message, error){

s.logger.Info("Received message body from client:", zap.String("input body", message.Body))
return &echo.Message {Body: message.Body}, nil
}
146 changes: 146 additions & 0 deletions pkg/api/grpc/echo/echo.pb.go

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

14 changes: 14 additions & 0 deletions pkg/api/grpc/echo/echo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

option go_package = "./echo";

package echo;

message Message {
string body = 1;
}


service EchoService {
rpc Echo(Message) returns (Message) {}
}
109 changes: 109 additions & 0 deletions pkg/api/grpc/echo/echo_grpc.pb.go

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

Loading
Loading