【发布时间】:2021-01-25 10:45:23
【问题描述】:
我正在使用 go-kit 创建一个 RPC 端点。我正在创建这样的端点
httptransport.NewServer(
endPoint.MakeGetBlogEndPoint(blogService),
transport.DecodeGetBlogRequest,
transport.EncodeGetBlogResponse
下面是我的 DecodeGetBlogRequest 函数
func DecodeGetBlogRequest(c context.Context, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
return nil, err
}
req := endPoint.GetBlogRequest{
ID: id,
}
return req, nil
}
我想要做的是验证这个函数中的 HTTP 请求,如果发现无效,只从这里发送一个带有有效错误代码的响应,而不将它传递给服务层。即如果 ID 不是有效数字,则从此处返回 400 Bad Request 响应。
但是由于我在这个函数中没有 ResponseWriter 引用,所以我不知道该怎么做。
我正在关注 go-kit 文档中的这个示例 https://gokit.io/examples/stringsvc.html
请求/有效负载仅应在传输层中验证并且仅在请求/有效负载有效时才应调用服务层的假设是否有效?如果是,在这个例子中怎么做?
【问题讨论】:
标签: go rpc endpoint transport-layer-protocol