首页 文章

从.proto自动生成REST endpoints

提问于
浏览
0

我在编译.proto文件时遇到问题 . 希望从.proto文件生成REST endpoints . 下面是代码和错误:syntax =“proto3”;

package pb;

import "google/protobuf/empty.proto";
import "google/api/annotations.proto";

service UrlShortener {
  rpc Hello(HelloRequest) returns (HelloResponse);

  rpc Encrypt(EncryptRequest) returns (EncryptResponse);

  rpc Decrypt(DecryptRequest) returns (DecryptResponse) {
    option (google.api.http) = {
                get: "/{hash}"
            };
  }
}

message HelloRequest {
  string Name = 1;
}

message HelloResponse {
  string Message = 1;
}

message EncryptRequest {
  string OriginalUrl = 1;
}

message EncryptResponse {
  UrlMap ResponseMap = 1;
}

message DecryptRequest {
  string hash = 1;
}

错误:github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis:警告:目录不存在 . google / api / annotations.proto:找不到文件 . urlshortener.proto:未找到导入“google / api / annotations.proto”或有错误 .

请帮忙解决这个问题 .

我试过了:go get -u github.com/grpc-ecosystem/grpc-gateway但它没说:路径中没有可构建的源文件 .

2 回答

  • 0

    我认为你的定义中有多个错误

    您在定义的最开头缺少语法版本:

    syntax = "proto3";
    

    有一些未定义的类型

    service.proto:32:3: "UrlMap" is not defined.
    service.proto:12:40: "DecryptResponse" is not defined.
    

    您正在导入和未使用的empty.proto

    您可以使用googleapies

    {GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis
    

    然后运行:

    protoc -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis -I/usr/local/include -I. service.proto --go_out=plugins=grpc:.
    

    我做了以前的更改并编译,所以我有service.pb.go文件

    Edited:

    看看这个grpc网关,也许可以帮到你https://github.com/grpc-ecosystem/grpc-gateway

  • 0

    找到解决方案:问题是google / api / annotations已从早期路径grpc-ecosystem / grpc-gateway / third_party / googleapis移至https://github.com/grpc-ecosystem/grpc-gateway/tree/master/third_party/googleapis/google/api .

    运行以下内容解决了错误:go get -u github.com/grpc-ecosystem/grpc-gateway / ...

相关问题