Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | // Command protoc-gen-grpc-gateway is a plugin for Google protocol buffer |
| 2 | // compiler to generate a reverse-proxy, which converts incoming RESTful |
| 3 | // HTTP/1 requests gRPC invocation. |
| 4 | // You rarely need to run this program directly. Instead, put this program |
| 5 | // into your $PATH with a name "protoc-gen-grpc-gateway" and run |
| 6 | // protoc --grpc-gateway_out=output_directory path/to/input.proto |
| 7 | // |
| 8 | // See README.md for more details. |
| 9 | package main |
| 10 | |
| 11 | import ( |
| 12 | "flag" |
| 13 | "fmt" |
| 14 | "os" |
| 15 | "strings" |
| 16 | |
| 17 | "github.com/golang/glog" |
| 18 | "github.com/golang/protobuf/proto" |
| 19 | plugin "github.com/golang/protobuf/protoc-gen-go/plugin" |
| 20 | "github.com/grpc-ecosystem/grpc-gateway/codegenerator" |
| 21 | "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/descriptor" |
| 22 | "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway/gengateway" |
| 23 | ) |
| 24 | |
| 25 | var ( |
| 26 | importPrefix = flag.String("import_prefix", "", "prefix to be added to go package paths for imported proto files") |
| 27 | importPath = flag.String("import_path", "", "used as the package if no input files declare go_package. If it contains slashes, everything up to the rightmost slash is ignored.") |
| 28 | registerFuncSuffix = flag.String("register_func_suffix", "Handler", "used to construct names of generated Register*<Suffix> methods.") |
| 29 | useRequestContext = flag.Bool("request_context", true, "determine whether to use http.Request's context or not") |
| 30 | allowDeleteBody = flag.Bool("allow_delete_body", false, "unless set, HTTP DELETE methods may not have a body") |
| 31 | grpcAPIConfiguration = flag.String("grpc_api_configuration", "", "path to gRPC API Configuration in YAML format") |
| 32 | pathType = flag.String("paths", "", "specifies how the paths of generated files are structured") |
| 33 | allowRepeatedFieldsInBody = flag.Bool("allow_repeated_fields_in_body", false, "allows to use repeated field in `body` and `response_body` field of `google.api.http` annotation option") |
| 34 | repeatedPathParamSeparator = flag.String("repeated_path_param_separator", "csv", "configures how repeated fields should be split. Allowed values are `csv`, `pipes`, `ssv` and `tsv`.") |
| 35 | allowPatchFeature = flag.Bool("allow_patch_feature", true, "determines whether to use PATCH feature involving update masks (using google.protobuf.FieldMask).") |
| 36 | allowColonFinalSegments = flag.Bool("allow_colon_final_segments", false, "determines whether colons are permitted in the final segment of a path") |
| 37 | versionFlag = flag.Bool("version", false, "print the current verison") |
| 38 | ) |
| 39 | |
| 40 | // Variables set by goreleaser at build time |
| 41 | var ( |
| 42 | version = "dev" |
| 43 | commit = "unknown" |
| 44 | date = "unknown" |
| 45 | ) |
| 46 | |
| 47 | func main() { |
| 48 | flag.Parse() |
| 49 | defer glog.Flush() |
| 50 | |
| 51 | if *versionFlag { |
| 52 | fmt.Printf("Version %v, commit %v, built at %v\n", version, commit, date) |
| 53 | os.Exit(0) |
| 54 | } |
| 55 | |
| 56 | reg := descriptor.NewRegistry() |
| 57 | |
| 58 | glog.V(1).Info("Parsing code generator request") |
| 59 | req, err := codegenerator.ParseRequest(os.Stdin) |
| 60 | if err != nil { |
| 61 | glog.Fatal(err) |
| 62 | } |
| 63 | glog.V(1).Info("Parsed code generator request") |
| 64 | if req.Parameter != nil { |
| 65 | for _, p := range strings.Split(req.GetParameter(), ",") { |
| 66 | spec := strings.SplitN(p, "=", 2) |
| 67 | if len(spec) == 1 { |
| 68 | if err := flag.CommandLine.Set(spec[0], ""); err != nil { |
| 69 | glog.Fatalf("Cannot set flag %s", p) |
| 70 | } |
| 71 | continue |
| 72 | } |
| 73 | name, value := spec[0], spec[1] |
| 74 | if strings.HasPrefix(name, "M") { |
| 75 | reg.AddPkgMap(name[1:], value) |
| 76 | continue |
| 77 | } |
| 78 | if err := flag.CommandLine.Set(name, value); err != nil { |
| 79 | glog.Fatalf("Cannot set flag %s", p) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | g := gengateway.New(reg, *useRequestContext, *registerFuncSuffix, *pathType, *allowPatchFeature) |
| 85 | |
| 86 | if *grpcAPIConfiguration != "" { |
| 87 | if err := reg.LoadGrpcAPIServiceFromYAML(*grpcAPIConfiguration); err != nil { |
| 88 | emitError(err) |
| 89 | return |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | reg.SetPrefix(*importPrefix) |
| 94 | reg.SetImportPath(*importPath) |
| 95 | reg.SetAllowDeleteBody(*allowDeleteBody) |
| 96 | reg.SetAllowRepeatedFieldsInBody(*allowRepeatedFieldsInBody) |
| 97 | reg.SetAllowColonFinalSegments(*allowColonFinalSegments) |
| 98 | if err := reg.SetRepeatedPathParamSeparator(*repeatedPathParamSeparator); err != nil { |
| 99 | emitError(err) |
| 100 | return |
| 101 | } |
| 102 | if err := reg.Load(req); err != nil { |
| 103 | emitError(err) |
| 104 | return |
| 105 | } |
| 106 | |
| 107 | var targets []*descriptor.File |
| 108 | for _, target := range req.FileToGenerate { |
| 109 | f, err := reg.LookupFile(target) |
| 110 | if err != nil { |
| 111 | glog.Fatal(err) |
| 112 | } |
| 113 | targets = append(targets, f) |
| 114 | } |
| 115 | |
| 116 | out, err := g.Generate(targets) |
| 117 | glog.V(1).Info("Processed code generator request") |
| 118 | if err != nil { |
| 119 | emitError(err) |
| 120 | return |
| 121 | } |
| 122 | emitFiles(out) |
| 123 | } |
| 124 | |
| 125 | func emitFiles(out []*plugin.CodeGeneratorResponse_File) { |
| 126 | emitResp(&plugin.CodeGeneratorResponse{File: out}) |
| 127 | } |
| 128 | |
| 129 | func emitError(err error) { |
| 130 | emitResp(&plugin.CodeGeneratorResponse{Error: proto.String(err.Error())}) |
| 131 | } |
| 132 | |
| 133 | func emitResp(resp *plugin.CodeGeneratorResponse) { |
| 134 | buf, err := proto.Marshal(resp) |
| 135 | if err != nil { |
| 136 | glog.Fatal(err) |
| 137 | } |
| 138 | if _, err := os.Stdout.Write(buf); err != nil { |
| 139 | glog.Fatal(err) |
| 140 | } |
| 141 | } |