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

Feature grpc version echo api #321

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
2bd6392
Testing with echo
JayKaku Sep 24, 2023
ed977d9
Merge branch 'master' of https://github.com/stefanprodan/podinfo
JayKaku Sep 24, 2023
2f41d6c
Started the GRPC APIs with Version API
Prashant-Dwivedi-08-01 Oct 1, 2023
e03c890
Complete another set of APIs
Prashant-Dwivedi-08-01 Oct 1, 2023
e9fc595
Merge pull request #1 from JayKaku/dev_prash_grpc_api
Prashant-Dwivedi-08-01 Oct 3, 2023
c09d68a
test
JayKaku Oct 16, 2023
d4d1df7
grpc api partial
JayKaku Oct 25, 2023
944fbf2
Updated the logger
Prashant-Dwivedi-08-01 Oct 26, 2023
78f7a69
delay n env grpc api added
JayKaku Oct 26, 2023
61b5cdd
Merge branch 'master' of https://github.com/JayKaku/podinfo
Prashant-Dwivedi-08-01 Oct 26, 2023
d49f09a
Updated the status API
Prashant-Dwivedi-08-01 Oct 26, 2023
de13843
header api added
JayKaku Oct 27, 2023
93ad600
updated header, info and echo
JayKaku Nov 3, 2023
f7efffb
info test, mockgrpcserver added
JayKaku Nov 3, 2023
10eecb6
added tests for half of grpc apis
JayKaku Nov 4, 2023
eff0843
Added the test cases for GRPC apis
Prashant-Dwivedi-08-01 Nov 10, 2023
1742d0c
Merge pull request #2 from JayKaku/dev-prash-grpc-tests
JayKaku Nov 16, 2023
d366557
added more information for error status in grpc
JayKaku Nov 16, 2023
721d0e4
Merge branch 'master' of https://github.com/JayKaku/podinfo
JayKaku Nov 16, 2023
59f64f8
patched env test to "PATH" for linux users
JayKaku Nov 16, 2023
282ec7d
package api to http, new folder structure [tested]
JayKaku Nov 17, 2023
9bb9cfe
error handling for grpc/info
JayKaku Nov 17, 2023
406ef7b
Merge branch 'stefanprodan:master' into master
JayKaku Nov 18, 2023
1351240
cleaned up comments
JayKaku Nov 18, 2023
6b68325
Merge branch 'master' of https://github.com/JayKaku/podinfo
JayKaku Nov 18, 2023
c6df291
verion and echo api
JayKaku Nov 18, 2023
6408efa
restructured api to api/http, api/grpc, pkg http
JayKaku Nov 20, 2023
35d3248
remoced tash
JayKaku Nov 20, 2023
8011621
Merge branch 'feature/grpc-version-echo-apis' into feature-gRPC-versi…
JayKaku Nov 20, 2023
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
17 changes: 17 additions & 0 deletions pkg/api/grpc/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package grpc

import (
"context"
"log"

"github.com/stefanprodan/podinfo/pkg/api/grpc/echo"
)

type echoServer struct {
echo.UnimplementedEchoServiceServer
}

func (s *echoServer) Echo (ctx context.Context, message *echo.Message) (*echo.Message, error){
log.Printf("Received message body from client: %s", message.Body)
Copy link
Owner

Choose a reason for hiding this comment

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

We need to use the server logger instead of default one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright

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.

69 changes: 69 additions & 0 deletions pkg/api/grpc/echo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package grpc

import (
"context"
"log"
"net"
"regexp"
"testing"

"github.com/stefanprodan/podinfo/pkg/api/grpc/echo"
"google.golang.org/grpc"
"google.golang.org/grpc/status"
"google.golang.org/grpc/test/bufconn"
)

func TestGrpcEcho(t *testing.T) {

// Server initialization
// bufconn => uses in-memory connection instead of system network I/O
lis := bufconn.Listen(1024*1024)
t.Cleanup(func() {
lis.Close()
})

srv := grpc.NewServer()
t.Cleanup(func() {
srv.Stop()
})

echo.RegisterEchoServiceServer(srv, &echoServer{})

go func(){
if err := srv.Serve(lis); err != nil {
log.Fatalf("srv.Serve %v", err)
}
}()

// - Test
dialer := func(context.Context, string) (net.Conn, error){
return lis.Dial()
}

ctx := context.Background()

conn, err := grpc.DialContext(ctx, "", grpc.WithContextDialer(dialer), grpc.WithInsecure())
t.Cleanup(func() {
conn.Close()
})

if err != nil {
t.Fatalf("grpc.DialContext %v", err)
}

client := echo.NewEchoServiceClient(conn)
res , err := client.Echo(context.Background(), &echo.Message{Body:"test123-test"})

// Check the status code is what we expect.
if _, ok := status.FromError(err); !ok {
t.Errorf("Echo returned type %T, want %T", err, status.Error)
}

// Check the response body is what we expect.
expected := ".*body.*test123-test.*"
r := regexp.MustCompile(expected)
if !r.MatchString(res.String()) {
t.Fatalf("Returned unexpected body:\ngot \n%v \nwant \n%s",
res, expected)
}
}
Loading