code.vegaprotocol.io/vega@v0.79.0/protos/embed.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package protos 17 18 import ( 19 "embed" 20 "strings" 21 22 "gopkg.in/yaml.v2" 23 ) 24 25 // We are going to use the path defined in the rules to ensure that only valid paths 26 // are recorded by the metrics. Rather than keeping a separate list in code that needs 27 // to be updated. We can embed the bindings files as the paths will need to be maintained 28 // here anyway. 29 30 //go:embed sources/vega/grpc-rest-bindings.yml sources/data-node/grpc-rest-bindings.yml 31 var FS embed.FS 32 33 type Rule struct { 34 Selector string `yaml:"selector"` 35 Post *string `yaml:"post"` 36 Get *string `yaml:"get"` 37 Body *string `yaml:"body"` 38 } 39 40 type Rules struct { 41 Rules []Rule `yaml:"rules"` 42 } 43 44 type Bindings struct { 45 HTTP Rules `yaml:"http"` 46 } 47 48 func CoreBindings() (*Bindings, error) { 49 b, err := FS.ReadFile("sources/vega/grpc-rest-bindings.yml") 50 if err != nil { 51 return nil, err 52 } 53 54 var bindings Bindings 55 err = yaml.Unmarshal(b, &bindings) 56 if err != nil { 57 return nil, err 58 } 59 60 return &bindings, nil 61 } 62 63 func DataNodeBindings() (*Bindings, error) { 64 b, err := FS.ReadFile("sources/data-node/grpc-rest-bindings.yml") 65 if err != nil { 66 return nil, err 67 } 68 69 var bindings Bindings 70 err = yaml.Unmarshal(b, &bindings) 71 if err != nil { 72 return nil, err 73 } 74 75 return &bindings, nil 76 } 77 78 func (b *Bindings) HasRoute(method, path string) bool { 79 method = strings.ToLower(method) 80 for _, rule := range b.HTTP.Rules { 81 var route string 82 switch method { 83 case "get": 84 if rule.Get == nil { 85 continue 86 } 87 route = *rule.Get 88 case "post": 89 if rule.Post == nil { 90 continue 91 } 92 route = *rule.Post 93 default: 94 return false 95 } 96 97 if strings.Contains(route, "/{") { 98 route = strings.Split(route, "/{")[0] 99 } 100 101 if strings.Contains(path, route) { 102 return true 103 } 104 } 105 106 return false 107 }