Skip to content

Commit

Permalink
Merge pull request #10 from Galactica-corp/feature/h2c-grpc-server
Browse files Browse the repository at this point in the history
Add h2c gRPC server wrapper in order to handle HTTP/2 headers. Add gRPC client playground to debug connection.
  • Loading branch information
yakud authored May 29, 2024
2 parents bab0de7 + 2019e1c commit 3a5d1ac
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
55 changes: 55 additions & 0 deletions cmd/grpc-client-playground/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 4 additions & 2 deletions internal/query/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}

Expand Down

0 comments on commit 3a5d1ac

Please sign in to comment.