https://github.com/janrs-io/jgrpc-response
基于 grpc-gateway 的,统一处理由 grpc 返回给 http 的数据
https://github.com/janrs-io/jgrpc-response
grpc grpc-gateway grpc-middleware microservice
Last synced: about 2 months ago
JSON representation
基于 grpc-gateway 的,统一处理由 grpc 返回给 http 的数据
- Host: GitHub
- URL: https://github.com/janrs-io/jgrpc-response
- Owner: janrs-io
- Created: 2023-04-14T12:43:07.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-06-07T10:00:29.000Z (about 3 years ago)
- Last Synced: 2025-03-11T14:19:01.791Z (over 1 year ago)
- Topics: grpc, grpc-gateway, grpc-middleware, microservice
- Language: Go
- Homepage: https://github.com/janrs-io/Jgrpc-response
- Size: 17.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 介绍
基于 `grpc-gateway` 的,统一处理由 `grpc` 返回给 `http` 的数据。包括以下几点:
- `http` 统一返回的 `json` 格式: 由 `code/msg/data` 组成
- 按照谷歌标准将 `grpc` 错误码转成 `http` 的错误码
- 请求成功后,处理后返回给前端只有一个 `response`
# 使用
引入包
```shell
go get "github.com/janrs-io/Jgrpc-response@v0.0.3"
```
在 `grpc-gateway` 启动 `http` 服务的中间件添加以下方法:
```shell
mux := runtime.NewServeMux(
runtime.WithErrorHandler(Jgrpc_response.HttpErrorHandler),
runtime.WithForwardResponseOption(Jgrpc_response.HttpSuccessResponseModifier),
runtime.WithMarshalerOption("*", &Jgrpc_response.CustomMarshaller{}),
)
```
**当 `rpc` 按照以上格式返回后,添加了 `http` 处理中间件会自动处理 `rpc` 返回的数据然后返回给前端。**