Skip to content

Commit

Permalink
some renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
technicallyty committed Jan 15, 2025
1 parent aac283b commit 30be39c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
8 changes: 4 additions & 4 deletions server/v2/api/grpcgateway/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ func DefaultConfig() *Config {
}

type Config struct {
// Enable defines if the gRPC-gateway should be enabled.
Enable bool `mapstructure:"enable" toml:"enable" comment:"Enable defines if the gRPC-gateway should be enabled."`
// Enable defines if the gRPC-Gateway should be enabled.
Enable bool `mapstructure:"enable" toml:"enable" comment:"Enable defines if the gRPC-Gateway should be enabled."`

// Address defines the address the gRPC-gateway server binds to.
Address string `mapstructure:"address" toml:"address" comment:"Address defines the address the gRPC-gateway server binds to."`
// Address defines the address the gRPC-Gateway server binds to.
Address string `mapstructure:"address" toml:"address" comment:"Address defines the address the gRPC-Gateway server binds to."`
}

type CfgOption func(*Config)
Expand Down
2 changes: 1 addition & 1 deletion server/v2/api/grpcgateway/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
//
// Header `x-cosmos-block-height` allows you to specify a height for the query.
//
// Requests that do not have a dynamic handler registered will be routed to the canonical gRPC gateway mux.
// Requests that do not have a dynamic handler registered will be routed to the canonical gRPC-Gateway mux.
package grpcgateway
35 changes: 18 additions & 17 deletions server/v2/api/grpcgateway/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ type queryMetadata struct {
wildcardKeyNames []string
}

// registerGatewayToMux registers handlers for grpc gateway annotations to the httpMux.
func registerGatewayToMux[T transaction.Tx](logger log.Logger, httpMux *http.ServeMux, gateway *runtime.ServeMux, am appmanager.AppManager[T]) error {
// mountHTTPRoutes registers handlers for from proto HTTP annotations to the http.ServeMux, using runtime.ServeMux as a fallback/
// last ditch effort router.
func mountHTTPRoutes[T transaction.Tx](logger log.Logger, httpMux *http.ServeMux, fallbackRouter *runtime.ServeMux, am appmanager.AppManager[T]) error {
annotationMapping, err := newHTTPAnnotationMapping()
if err != nil {
return err
Expand All @@ -55,15 +56,15 @@ func registerGatewayToMux[T transaction.Tx](logger log.Logger, httpMux *http.Ser
if err != nil {
return err
}
registerMethods[T](logger, httpMux, am, gateway, annotationToMetadata)
registerMethods[T](logger, httpMux, am, fallbackRouter, annotationToMetadata)
return nil
}

// registerMethods registers the endpoints specified in the annotation mapping to the mux.
func registerMethods[T transaction.Tx](logger log.Logger, mux *http.ServeMux, am appmanager.AppManager[T], gateway *runtime.ServeMux, annotationToMetadata map[string]queryMetadata) {
// registerMethods registers the endpoints specified in the annotation mapping to the http.ServeMux.
func registerMethods[T transaction.Tx](logger log.Logger, mux *http.ServeMux, am appmanager.AppManager[T], fallbackRouter *runtime.ServeMux, annotationToMetadata map[string]queryMetadata) {
// register the fallback handler. this will run if the mux isn't able to get a match from the registrations below.
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
gateway.ServeHTTP(w, r)
fallbackRouter.ServeHTTP(w, r)
})

// register in deterministic order. we do this because of the problem mentioned below, and different nodes could
Expand All @@ -82,7 +83,7 @@ func registerMethods[T transaction.Tx](logger log.Logger, mux *http.ServeMux, am
}()
mux.Handle(u, &protoHandler[T]{
msg: qMD.msg,
gateway: gateway,
fallbackRouter: fallbackRouter,
appManager: am,
wildcardKeyNames: qMD.wildcardKeyNames,
})
Expand All @@ -96,14 +97,14 @@ type protoHandler[T transaction.Tx] struct {
msg gogoproto.Message
// wildcardKeyNames are the wildcard key names, if any, specified in the http annotation. (i.e. /foo/bar/{baz})
wildcardKeyNames []string
// gateway is the canonical gateway ServeMux to use as a fallback if the query does not have a handler in AppManager.
gateway *runtime.ServeMux
// fallbackRouter is the canonical gRPC gateway runtime.ServeMux, used as a fallback if the query does not have a handler in AppManager.
fallbackRouter *runtime.ServeMux
// appManager is used to route queries.
appManager appmanager.AppManager[T]
}

func (p *protoHandler[T]) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
in, out := runtime.MarshalerForRequest(p.gateway, request)
in, out := runtime.MarshalerForRequest(p.fallbackRouter, request)

// we clone here as handlers are concurrent and using p.msg would trample.
msg := gogoproto.Clone(p.msg)
Expand All @@ -117,7 +118,7 @@ func (p *protoHandler[T]) ServeHTTP(writer http.ResponseWriter, request *http.Re
inputMsg, err := p.populateMessage(request, in, msg, params)
if err != nil {
// the errors returned from the message creation return status errors. no need to make one here.
runtime.HTTPError(request.Context(), p.gateway, out, writer, request, err)
runtime.HTTPError(request.Context(), p.fallbackRouter, out, writer, request, err)
return
}

Expand All @@ -128,24 +129,24 @@ func (p *protoHandler[T]) ServeHTTP(writer http.ResponseWriter, request *http.Re
if heightStr != "" && heightStr != "latest" {
height, err = strconv.ParseUint(heightStr, 10, 64)
if err != nil {
runtime.HTTPError(request.Context(), p.gateway, out, writer, request, status.Errorf(codes.InvalidArgument, "invalid height in header: %s", heightStr))
runtime.HTTPError(request.Context(), p.fallbackRouter, out, writer, request, status.Errorf(codes.InvalidArgument, "invalid height in header: %s", heightStr))
return
}
}

responseMsg, err := p.appManager.Query(request.Context(), height, inputMsg)
if err != nil {
// if we couldn't find a handler for this request, just fall back to the gateway mux.
// if we couldn't find a handler for this request, just fall back to the fallbackRouter.
if strings.Contains(err.Error(), "no handler") {
p.gateway.ServeHTTP(writer, request)
p.fallbackRouter.ServeHTTP(writer, request)
} else {
// for all other errors, we just return the error.
runtime.HTTPError(request.Context(), p.gateway, out, writer, request, err)
runtime.HTTPError(request.Context(), p.fallbackRouter, out, writer, request, err)
}
return
}

runtime.ForwardResponseMessage(request.Context(), p.gateway, out, writer, request, responseMsg)
runtime.ForwardResponseMessage(request.Context(), p.fallbackRouter, out, writer, request, responseMsg)
}

func (p *protoHandler[T]) populateMessage(req *http.Request, marshaler runtime.Marshaler, input gogoproto.Message, pathParams map[string]string) (gogoproto.Message, error) {
Expand Down Expand Up @@ -176,7 +177,7 @@ func (p *protoHandler[T]) populateMessage(req *http.Request, marshaler runtime.M
}

// this block of code ensures that the body can be re-read. this is needed as if the query fails in the
// app's query handler, we need to pass the request back to the canonical gateway, which needs to be able to
// app's query handler, we need to pass the request back to the fallbackRouter, which needs to be able to
// read the body again.
bodyBytes, err := io.ReadAll(req.Body)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/v2/api/grpcgateway/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Server[T transaction.Tx] struct {
GRPCGatewayRouter *runtime.ServeMux
}

// New creates a new gRPC-gateway server.
// New creates a new gRPC-Gateway server.
func New[T transaction.Tx](
logger log.Logger,
config server.ConfigMap,
Expand Down Expand Up @@ -76,7 +76,7 @@ func New[T transaction.Tx](
s.logger = logger.With(log.ModuleKey, s.Name())
s.config = serverCfg
mux := http.NewServeMux()
err := registerGatewayToMux[T](logger, mux, s.GRPCGatewayRouter, appManager)
err := mountHTTPRoutes[T](logger, mux, s.GRPCGatewayRouter, appManager)
if err != nil {
return nil, fmt.Errorf("failed to register gRPC gateway annotations: %w", err)
}
Expand Down

0 comments on commit 30be39c

Please sign in to comment.