gRPC gateway init (#7019)
* WIP: grpc server setup * add register grpc routes * updated go mod * updated grpc for all modules * added pb file for grpc gateway * udpated gw file * added a test for grpc route * fixed conflicts * added grpc server * grpc tests added * cleanup * Fix gateway forward issue * Add godoc * updated tests * fix lint * fix tests * fix tests * fix tests * fixed test * Add grpc headers * Fix error handling * Fix tests - hacky * Fix lint * remove debug logs * Fix review comments * move grpc tests into a separate file * Fix protobuf version * Update x/capability/module.go * Fix godoc * Fix review suggestions * Fix codec * Add query params test for gateway request * Fix gofmt Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
co-authored by
anilCSE
Federico Kunze
parent
1f7a2787aa
commit
78194e1cdd
+46
-3
@@ -7,8 +7,10 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogo/gateway"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/grpc-ecosystem/grpc-gateway/runtime"
|
||||
"github.com/rakyll/statik/fs"
|
||||
"github.com/tendermint/tendermint/libs/log"
|
||||
tmrpcserver "github.com/tendermint/tendermint/rpc/jsonrpc/server"
|
||||
@@ -16,6 +18,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/server/config"
|
||||
"github.com/cosmos/cosmos-sdk/telemetry"
|
||||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
|
||||
"github.com/cosmos/cosmos-sdk/types/rest"
|
||||
|
||||
// unnamed import of statik for swagger UI support
|
||||
@@ -24,19 +27,54 @@ import (
|
||||
|
||||
// Server defines the server's API interface.
|
||||
type Server struct {
|
||||
Router *mux.Router
|
||||
ClientCtx client.Context
|
||||
Router *mux.Router
|
||||
GRPCRouter *runtime.ServeMux
|
||||
ClientCtx client.Context
|
||||
|
||||
logger log.Logger
|
||||
metrics *telemetry.Metrics
|
||||
listener net.Listener
|
||||
}
|
||||
|
||||
// CustomGRPCHeaderMatcher for mapping request headers to
|
||||
// GRPC metadata.
|
||||
// HTTP headers that start with 'Grpc-Metadata-' are automatically mapped to
|
||||
// gRPC metadata after removing prefix 'Grpc-Metadata-'. We can use this
|
||||
// CustomGRPCHeaderMatcher if headers don't start with `Grpc-Metadata-`
|
||||
func CustomGRPCHeaderMatcher(key string) (string, bool) {
|
||||
switch strings.ToLower(key) {
|
||||
case grpctypes.GRPCBlockHeightHeader:
|
||||
return grpctypes.GRPCBlockHeightHeader, true
|
||||
default:
|
||||
return runtime.DefaultHeaderMatcher(key)
|
||||
}
|
||||
}
|
||||
|
||||
func New(clientCtx client.Context, logger log.Logger) *Server {
|
||||
// The default JSON marshaller used by the gRPC-Gateway is unable to marshal non-nullable non-scalar fields.
|
||||
// Using the gogo/gateway package with the gRPC-Gateway WithMarshaler option fixes the scalar field marshalling issue.
|
||||
marshalerOption := &gateway.JSONPb{
|
||||
EmitDefaults: true,
|
||||
Indent: " ",
|
||||
OrigName: true,
|
||||
}
|
||||
|
||||
return &Server{
|
||||
Router: mux.NewRouter(),
|
||||
ClientCtx: clientCtx,
|
||||
logger: logger,
|
||||
GRPCRouter: runtime.NewServeMux(
|
||||
// Custom marshaler option is required for gogo proto
|
||||
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshalerOption),
|
||||
|
||||
// This is necessary to get error details properly
|
||||
// marshalled in unary requests.
|
||||
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler),
|
||||
|
||||
// Custom header matcher for mapping request headers to
|
||||
// GRPC metadata
|
||||
runtime.WithIncomingHeaderMatcher(CustomGRPCHeaderMatcher),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,6 +108,7 @@ func (s *Server) Start(cfg config.Config) error {
|
||||
return err
|
||||
}
|
||||
|
||||
s.registerGRPCRoutes()
|
||||
s.listener = listener
|
||||
var h http.Handler = s.Router
|
||||
|
||||
@@ -93,7 +132,11 @@ func (s *Server) registerSwaggerUI() {
|
||||
}
|
||||
|
||||
staticServer := http.FileServer(statikFS)
|
||||
s.Router.PathPrefix("/").Handler(staticServer)
|
||||
s.Router.PathPrefix("/legacy").Handler(staticServer)
|
||||
}
|
||||
|
||||
func (s *Server) registerGRPCRoutes() {
|
||||
s.Router.PathPrefix("/").Handler(s.GRPCRouter)
|
||||
}
|
||||
|
||||
func (s *Server) registerMetrics() {
|
||||
|
||||
Reference in New Issue
Block a user