feat(api): Add block height to response headers (#24428)

Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
Alexander Peters
2025-04-10 20:47:33 +00:00
committed by GitHub
co-authored by Alex | Interchain Labs
parent 1e92c9c7b7
commit f18c6ea274
3 changed files with 74 additions and 0 deletions
+13
View File
@@ -11,6 +11,7 @@ import (
tmrpcserver "github.com/cometbft/cometbft/rpc/jsonrpc/server"
gateway "github.com/cosmos/gogogateway"
"github.com/golang/protobuf/proto" //nolint:staticcheck // grpc-gateway uses deprecated golang/protobuf
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
@@ -84,11 +85,23 @@ func New(clientCtx client.Context, logger log.Logger, grpcSrv *grpc.Server) *Ser
// Custom header matcher for mapping request headers to
// GRPC metadata
runtime.WithIncomingHeaderMatcher(CustomGRPCHeaderMatcher),
// extension to set custom response headers
runtime.WithForwardResponseOption(customGRPCResponseHeaders),
),
GRPCSrv: grpcSrv,
}
}
func customGRPCResponseHeaders(ctx context.Context, w http.ResponseWriter, _ proto.Message) error {
if meta, ok := runtime.ServerMetadataFromContext(ctx); ok {
if values := meta.HeaderMD.Get(grpctypes.GRPCBlockHeightHeader); len(values) == 1 {
w.Header().Set(grpctypes.GRPCBlockHeightHeader, values[0])
}
}
return nil
}
// Start starts the API server. Internally, the API server leverages CometBFT's
// JSON RPC server. Configuration options are provided via config.APIConfig
// and are delegated to the CometBFT JSON RPC server.