【问题标题】:Build proto3 gRPC API with overloaded endpoints使用重载端点构建 proto3 gRPC API
【发布时间】:2021-05-24 14:46:36
【问题描述】:

我对 API 构建相当陌生,所以这可能是一个比我最初提出的更广泛的问题。 我在 Golang 中创建一个 API(使用 protobuf 3 和 gRPC),它有两个相似的端点:

  • GET /project/genres
  • GET /project/{id}

问题是,当我运行curl localhost:8080/project/genres 时,模式匹配会导致/project/{id} 端点被以genres 作为id 调用。有没有一些简单的方法可以解决这个问题,还是我必须在服务器代码中构建一些东西才能根据类型调用正确的函数?

我还尝试翻转这些定义的顺序,以防模式匹配有一些我不知道的操作顺序,但这并没有什么不同。

这是我的 proto 文件中的定义:

message EmptyRequest { }
message ProjectRequest {
  string id = 1;
}

message GenreResponse {
  int32 id = 1;
  string name = 2;
}

message ProjectResponse {
  int32 id = 1;
  string name = 2;
}

service ProjectService {
  rpc GetProject(ProjectRequest) returns (ProjectResponse) {
    option (google.api.http) = {
      get: "/v1/project/{id}"
    };
  }
  rpc GetGenres(EmptyRequest) returns (GenreResponse) {
    option (google.api.http) = {
      get: "/v1/project/genres"
    };
  }
}

【问题讨论】:

    标签: go protocol-buffers grpc


    【解决方案1】:

    我能够在 url 路径模板中指定一个检查来解决这个问题:

     rpc GetGenres(EmptyRequest) returns (GenreResponse) {
        option (google.api.http) = {
        get: "/v1/project/{id=genres}"
      };
    }
    

    这似乎解决了问题。我不知道是否有其他解决方案,或者这是否是正确的方法,但如果有更好的解决方案,我很乐意接受其他答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2021-08-08
      • 2021-11-19
      • 1970-01-01
      相关资源
      最近更新 更多