Skills2026年5月3日·1 分钟阅读

grpc-gateway — RESTful JSON API Proxy for gRPC Services

A protoc plugin and reverse proxy that generates a RESTful HTTP/JSON API gateway from gRPC service definitions, letting clients use REST while backends speak gRPC.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
grpc-gateway Overview
通用 CLI 安装命令
npx tokrepo install ac909af5-46a7-11f1-9bc6-00163e2b0d79

Introduction

grpc-gateway bridges the gap between gRPC microservices and RESTful clients. By reading gRPC service definitions annotated with HTTP mapping rules, it generates a reverse proxy server that accepts JSON/HTTP requests, translates them into gRPC calls, and returns JSON responses. This lets teams adopt gRPC internally while exposing familiar REST APIs externally.

What grpc-gateway Does

  • Generates a reverse proxy from annotated Protocol Buffer service definitions
  • Translates HTTP/JSON requests to gRPC and responses back to JSON
  • Produces OpenAPI (Swagger) specifications automatically from your protos
  • Supports path parameters, query parameters, and request body mappings
  • Handles streaming RPCs with server-sent events or chunked transfer

Architecture Overview

The gateway works as a protoc plugin that reads google.api.http annotations in your .proto files. At build time, it generates Go code implementing an HTTP handler that marshals JSON to protobuf, calls the gRPC backend, and marshals the protobuf response back to JSON. At runtime, this handler runs as a standard Go HTTP server (often alongside the gRPC server in the same binary) or as a standalone reverse proxy process.

Self-Hosting & Configuration

  • Install protoc plugins: protoc-gen-grpc-gateway and protoc-gen-openapiv2
  • Add google/api/annotations.proto to your proto imports
  • Annotate RPCs with option (google.api.http) = { get: "/v1/resource/{id}" };
  • Run protoc to generate the gateway Go code
  • Register the generated handler on an HTTP mux and start the server

Key Features

  • Single source of truth: proto files define both gRPC and REST interfaces
  • Automatic OpenAPI spec generation for documentation and client code generation
  • Customizable JSON marshaling with support for field masks and enum strings
  • Request routing supports path parameters, query strings, and nested message bodies
  • Middleware support via standard Go HTTP handler chaining

Comparison with Similar Tools

  • Envoy gRPC-JSON transcoding — proxy-level transcoding without code generation; grpc-gateway embeds in your Go binary
  • gRPC-Web — browser gRPC client over HTTP/1.1; grpc-gateway provides full REST with custom URL paths
  • Connect (connectrpc) — unified protocol supporting gRPC and REST; grpc-gateway works with existing gRPC services
  • Twirp — simple RPC framework with JSON support; grpc-gateway preserves full gRPC compatibility
  • Kong/APISIX gRPC plugins — API gateway transcoding; grpc-gateway generates a dedicated Go server

FAQ

Q: Do I need a separate process for the gateway? A: No. You can run the gateway handler in the same Go binary as your gRPC server on a different port.

Q: Does it support gRPC streaming? A: Server-side streaming is supported. Client-side and bidirectional streaming have limited HTTP mapping support.

Q: Can I customize the JSON output format? A: Yes. The runtime package offers options for enum representation, field naming, and empty field emission.

Q: How do I add authentication? A: Use standard HTTP middleware (JWT validation, API keys) in front of the gateway handler.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产