-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
feat(simapp/v2): wire grpcgateway server (partial backport #22701) #22713
Merged
julienrbrt
merged 2 commits into
release/v0.52.x
from
mergify/bp/release/v0.52.x/pr-22701
Dec 2, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
package grpcgateway | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
gateway "github.com/cosmos/gogogateway" | ||
"github.com/cosmos/gogoproto/jsonpb" | ||
"github.com/grpc-ecosystem/grpc-gateway/runtime" | ||
|
||
"cosmossdk.io/core/server" | ||
"cosmossdk.io/core/transaction" | ||
"cosmossdk.io/log" | ||
serverv2 "cosmossdk.io/server/v2" | ||
Check failure on line 16 in server/v2/api/grpcgateway/server.go GitHub Actions / split-test-files
Check failure on line 16 in server/v2/api/grpcgateway/server.go GitHub Actions / dependency-review
Check failure on line 16 in server/v2/api/grpcgateway/server.go GitHub Actions / dependency-review
|
||
) | ||
|
||
var ( | ||
_ serverv2.ServerComponent[transaction.Tx] = (*Server[transaction.Tx])(nil) | ||
_ serverv2.HasConfig = (*Server[transaction.Tx])(nil) | ||
) | ||
|
||
const ServerName = "grpc-gateway" | ||
|
||
type Server[T transaction.Tx] struct { | ||
logger log.Logger | ||
config *Config | ||
cfgOptions []CfgOption | ||
|
||
server *http.Server | ||
GRPCGatewayRouter *runtime.ServeMux | ||
} | ||
|
||
// New creates a new gRPC-gateway server. | ||
func New[T transaction.Tx]( | ||
logger log.Logger, | ||
config server.ConfigMap, | ||
ir jsonpb.AnyResolver, | ||
cfgOptions ...CfgOption, | ||
) (*Server[T], error) { | ||
// 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 marshaling issue. | ||
marshalerOption := &gateway.JSONPb{ | ||
EmitDefaults: true, | ||
Indent: "", | ||
OrigName: true, | ||
AnyResolver: ir, | ||
} | ||
|
||
s := &Server[T]{ | ||
GRPCGatewayRouter: runtime.NewServeMux( | ||
// Custom marshaler option is required for gogo proto | ||
runtime.WithMarshalerOption(runtime.MIMEWildcard, marshalerOption), | ||
|
||
// This is necessary to get error details properly | ||
// marshaled in unary requests. | ||
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler), | ||
|
||
// Custom header matcher for mapping request headers to | ||
// GRPC metadata | ||
runtime.WithIncomingHeaderMatcher(CustomGRPCHeaderMatcher), | ||
), | ||
cfgOptions: cfgOptions, | ||
} | ||
|
||
serverCfg := s.Config().(*Config) | ||
if len(config) > 0 { | ||
if err := serverv2.UnmarshalSubConfig(config, s.Name(), &serverCfg); err != nil { | ||
return s, fmt.Errorf("failed to unmarshal config: %w", err) | ||
} | ||
} | ||
|
||
// TODO: register the gRPC-Gateway routes | ||
|
||
s.logger = logger.With(log.ModuleKey, s.Name()) | ||
s.config = serverCfg | ||
|
||
return s, nil | ||
} | ||
|
||
// NewWithConfigOptions creates a new gRPC-gateway server with the provided config options. | ||
func NewWithConfigOptions[T transaction.Tx](opts ...CfgOption) *Server[T] { | ||
return &Server[T]{ | ||
cfgOptions: opts, | ||
} | ||
} | ||
|
||
func (s *Server[T]) Name() string { | ||
return ServerName | ||
} | ||
|
||
func (s *Server[T]) Config() any { | ||
if s.config == nil || s.config.Address == "" { | ||
cfg := DefaultConfig() | ||
// overwrite the default config with the provided options | ||
for _, opt := range s.cfgOptions { | ||
opt(cfg) | ||
} | ||
|
||
return cfg | ||
} | ||
|
||
return s.config | ||
} | ||
|
||
func (s *Server[T]) Start(ctx context.Context) error { | ||
if !s.config.Enable { | ||
s.logger.Info(fmt.Sprintf("%s server is disabled via config", s.Name())) | ||
return nil | ||
} | ||
|
||
mux := http.NewServeMux() | ||
mux.Handle("/", s.GRPCGatewayRouter) | ||
|
||
s.server = &http.Server{ | ||
Addr: s.config.Address, | ||
Handler: mux, | ||
} | ||
|
||
s.logger.Info("starting gRPC-Gateway server...", "address", s.config.Address) | ||
if err := s.server.ListenAndServe(); err != nil && err != http.ErrServerClosed { | ||
return fmt.Errorf("failed to start gRPC-Gateway server: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *Server[T]) Stop(ctx context.Context) error { | ||
if !s.config.Enable { | ||
return nil | ||
} | ||
|
||
s.logger.Info("stopping gRPC-Gateway server...", "address", s.config.Address) | ||
return s.server.Shutdown(ctx) | ||
} | ||
|
||
// 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) { | ||
// GRPCBlockHeightHeader is the gRPC header for block height. | ||
const GRPCBlockHeightHeader = "x-cosmos-block-height" | ||
|
||
switch strings.ToLower(key) { | ||
case GRPCBlockHeightHeader: | ||
return GRPCBlockHeightHeader, true | ||
|
||
default: | ||
return runtime.DefaultHeaderMatcher(key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Iteration over map Warning