From 2019e1cb76efa3082159b1add477e7ea8d0896df Mon Sep 17 00:00:00 2001 From: yakud Date: Wed, 29 May 2024 17:34:28 +0300 Subject: [PATCH] Add h2c gRPC server wrapper in order to handle HTTP/2 headers. Add gRPC client playground to debug connection. --- cmd/grpc-client-playground/main.go | 55 ++++++++++++++++++++++++++++++ internal/query/server.go | 6 ++-- 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 cmd/grpc-client-playground/main.go diff --git a/cmd/grpc-client-playground/main.go b/cmd/grpc-client-playground/main.go new file mode 100644 index 0000000..c671889 --- /dev/null +++ b/cmd/grpc-client-playground/main.go @@ -0,0 +1,55 @@ +/* + * Copyright 2024 Galactica Network + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "context" + "fmt" + + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + + "github.com/Galactica-corp/merkle-proof-service/gen/galactica/merkle" +) + +func main() { + // GRPC client for merkle proof service + //url := "grpc-merkle-proof-service.galactica.com:443" + url := "localhost:50651" + + // Create a new connection + conn, err := grpc.Dial(url, grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + panic(err) + } + + // Close the connection + defer conn.Close() + + // Create a new client + client := merkle.NewQueryClient(conn) + resp, err := client.GetEmptyLeafProof(context.Background(), &merkle.GetEmptyLeafProofRequest{ + Registry: "0xbc196948e8c1Bc416aEaCf309a63DCEFfdf0cE31", + }) + + if err != nil { + panic(err) + } + + // Print the response + fmt.Println(resp) +} diff --git a/internal/query/server.go b/internal/query/server.go index 2d99d56..5d1f60c 100644 --- a/internal/query/server.go +++ b/internal/query/server.go @@ -27,6 +27,8 @@ import ( "github.com/cometbft/cometbft/libs/log" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/swaggest/swgui/v5cdn" + "golang.org/x/net/http2" + "golang.org/x/net/http2/h2c" "google.golang.org/grpc" "google.golang.org/protobuf/proto" @@ -65,7 +67,6 @@ func NewServer(registryService *zkregistry.Service, logger log.Logger) *Server { // RunGRPC starts the gRPC server func (s *Server) RunGRPC(ctx context.Context, address string) error { - // validate address if _, _, err := net.SplitHostPort(address); err != nil { return fmt.Errorf("invalid address: %v", err) } @@ -91,7 +92,8 @@ func (s *Server) RunGRPC(ctx context.Context, address string) error { s.logger.Info("gRPC server started", "address", address) - if err := grpcServer.Serve(lis); err != nil { + handler := h2c.NewHandler(grpcServer, &http2.Server{}) + if err := http.Serve(lis, handler); err != nil { return fmt.Errorf("failed to serve: %v", err) }