modulepackage
0.0.3
Repository: https://github.com/janrs-io/jgrpc-response.git
Documentation: pkg.go.dev
# README
介绍
基于 grpc-gateway
的,统一处理由 grpc
返回给 http
的数据。包括以下几点:
http
统一返回的json
格式: 由code/msg/data
组成- 按照谷歌标准将
grpc
错误码转成http
的错误码 - 请求成功后,处理后返回给前端只有一个
response
使用
引入包
go get "github.com/janrs-io/[email protected]"
在 grpc-gateway
启动 http
服务的中间件添加以下方法:
mux := runtime.NewServeMux(
runtime.WithErrorHandler(Jgrpc_response.HttpErrorHandler),
runtime.WithForwardResponseOption(Jgrpc_response.HttpSuccessResponseModifier),
runtime.WithMarshalerOption("*", &Jgrpc_response.CustomMarshaller{}),
)
当 rpc
按照以上格式返回后,添加了 http
处理中间件会自动处理 rpc
返回的数据然后返回给前端。
# Functions
HttpErrorHandler Http service error handler.
HTTPStatusFromCode converts a gRPC error code into the corresponding HTTP response status.
HttpSuccessResponseModifier Request successful return data format.