...
1 package descriptor
2
3 import (
4 "bytes"
5 "fmt"
6 "io/ioutil"
7 "strings"
8
9 "github.com/ghodss/yaml"
10 "github.com/golang/protobuf/jsonpb"
11 )
12
13 func loadGrpcAPIServiceFromYAML(yamlFileContents []byte, yamlSourceLogName string) (*GrpcAPIService, error) {
14 jsonContents, err := yaml.YAMLToJSON(yamlFileContents)
15 if err != nil {
16 return nil, fmt.Errorf("Failed to convert gRPC API Configuration from YAML in '%v' to JSON: %v", yamlSourceLogName, err)
17 }
18
19
20 unmarshaler := jsonpb.Unmarshaler{
21 AllowUnknownFields: true,
22 }
23
24 serviceConfiguration := GrpcAPIService{}
25 if err := unmarshaler.Unmarshal(bytes.NewReader(jsonContents), &serviceConfiguration); err != nil {
26 return nil, fmt.Errorf("Failed to parse gRPC API Configuration from YAML in '%v': %v", yamlSourceLogName, err)
27 }
28
29 return &serviceConfiguration, nil
30 }
31
32 func registerHTTPRulesFromGrpcAPIService(registry *Registry, service *GrpcAPIService, sourceLogName string) error {
33 if service.HTTP == nil {
34
35 return nil
36 }
37
38 for _, rule := range service.HTTP.GetRules() {
39 selector := "." + strings.Trim(rule.GetSelector(), " ")
40 if strings.ContainsAny(selector, "*, ") {
41 return fmt.Errorf("Selector '%v' in %v must specify a single service method without wildcards", rule.GetSelector(), sourceLogName)
42 }
43
44 registry.AddExternalHTTPRule(selector, rule)
45 }
46
47 return nil
48 }
49
50
51
52
53
54
55
56
57
58
59 func (r *Registry) LoadGrpcAPIServiceFromYAML(yamlFile string) error {
60 yamlFileContents, err := ioutil.ReadFile(yamlFile)
61 if err != nil {
62 return fmt.Errorf("Failed to read gRPC API Configuration description from '%v': %v", yamlFile, err)
63 }
64
65 service, err := loadGrpcAPIServiceFromYAML(yamlFileContents, yamlFile)
66 if err != nil {
67 return err
68 }
69
70 return registerHTTPRulesFromGrpcAPIService(r, service, yamlFile)
71 }
72
View as plain text