【发布时间】: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