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

Enable gRPC-web calls on providers gRPC port #154

Merged
merged 2 commits into from
Dec 8, 2022
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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ require (
github.com/hdevalence/ed25519consensus v0.0.0-20210204194344-59a8610d2b87 // indirect
github.com/iancoleman/strcase v0.2.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/improbable-eng/grpc-web v0.14.1 // indirect
github.com/improbable-eng/grpc-web v0.14.1
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
Expand Down Expand Up @@ -188,7 +188,7 @@ require (
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/exp v0.0.0-20220613132600-b0d781184e0d
golang.org/x/net v0.2.0 // indirect
golang.org/x/net v0.2.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/term v0.2.0 // indirect
Expand Down
30 changes: 27 additions & 3 deletions relayer/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"bytes"
context "context"
"encoding/json"
"errors"
"fmt"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"golang.org/x/net/http2"
"golang.org/x/net/http2/h2c"
"math/rand"
"net"
"net/http"
"os"
"os/signal"
"strconv"
Expand Down Expand Up @@ -1139,23 +1144,42 @@ func Server(
utils.LavaFormatFatal("provider failure setting up listener", err, &map[string]string{"listenAddr": listenAddr, "ChainID": ChainID})
}
s := grpc.NewServer()

wrappedServer := grpcweb.WrapServer(s)
handler := func(resp http.ResponseWriter, req *http.Request) {
// Set CORS headers
resp.Header().Set("Access-Control-Allow-Origin", "*")
resp.Header().Set("Access-Control-Allow-Headers", "Content-Type,x-grpc-web")

wrappedServer.ServeHTTP(resp, req)
}

httpServer := http.Server{
Handler: h2c.NewHandler(http.HandlerFunc(handler), &http2.Server{}),
}

go func() {
select {
case <-ctx.Done():
utils.LavaFormatInfo("Provider Server ctx.Done", nil)
case <-signalChan:
utils.LavaFormatInfo("Provider Server signalChan", nil)
}
cancel()
s.Stop()

shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()

if err := httpServer.Shutdown(shutdownCtx); err != nil {
utils.LavaFormatFatal("Provider failed to shutdown", err, &map[string]string{})
}
}()

Server := &relayServer{}

pairingtypes.RegisterRelayerServer(s, Server)

utils.LavaFormatInfo("Server listening", &map[string]string{"Address": lis.Addr().String()})
if err := s.Serve(lis); err != nil {
if err := httpServer.Serve(lis); !errors.Is(err, http.ErrServerClosed) {
utils.LavaFormatFatal("provider failed to serve", err, &map[string]string{"Address": lis.Addr().String(), "ChainID": ChainID})
}

Expand Down