local_xml_resolver.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Licensed under the Apache License, Version 2.0 (the "License");
  3. * you may not use this file except in compliance with the License.
  4. * You may obtain a copy of the License at
  5. *
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14. package endpoints
  15. import (
  16. "encoding/xml"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "os/exec"
  21. "path/filepath"
  22. "runtime"
  23. "strings"
  24. "sync"
  25. )
  26. var readXmlOnce sync.Once
  27. var v = Endpoints{}
  28. type LocalXmlResolver struct {
  29. }
  30. func (resolver *LocalXmlResolver) TryResolve(param *ResolveParam) (endpoint string, support bool, err error) {
  31. readXmlOnce.Do(func() {
  32. _, file, _, _ := runtime.Caller(0)
  33. filename := filepath.Join(file, "../endpoints.xml")
  34. data, err := ioutil.ReadFile(filename)
  35. if err != nil {
  36. support = false
  37. return
  38. }
  39. err = xml.Unmarshal(data, &v)
  40. if err != nil {
  41. support = false
  42. return
  43. }
  44. })
  45. for _, xmlEndpoint := range v.EndpointList {
  46. for _, xmlRegionId := range xmlEndpoint.RegionIds.Id {
  47. if xmlRegionId == param.RegionId {
  48. for _, xmlProduct := range xmlEndpoint.Products.ProductList {
  49. if xmlProduct.ProductName == param.Product {
  50. endpoint = xmlProduct.DomainName
  51. support = true
  52. return
  53. }
  54. }
  55. }
  56. }
  57. }
  58. support = false
  59. return
  60. }
  61. func GetCurrentPath() string {
  62. s, err := exec.LookPath(os.Args[0])
  63. if err != nil {
  64. fmt.Println(err.Error())
  65. }
  66. s = strings.Replace(s, "\\", "/", -1)
  67. s = strings.Replace(s, "\\\\", "/", -1)
  68. i := strings.LastIndex(s, "/")
  69. path := string(s[0 : i+1])
  70. return path
  71. }
  72. type Endpoints struct {
  73. XMLName xml.Name `xml:"Endpoints"`
  74. EndpointList []Endpoint `xml:"Endpoint"`
  75. }
  76. type Endpoint struct {
  77. XMLName xml.Name `xml:"Endpoint"`
  78. Name string `xml:"name,attr"`
  79. RegionIds RegionIds `xml:"RegionIds"`
  80. Products Products `xml:"Products"`
  81. }
  82. type RegionIds struct {
  83. Id []string `xml:"RegionId"`
  84. }
  85. type Products struct {
  86. ProductList []Product `xml:"Product"`
  87. }
  88. type RegionId struct {
  89. XMLName string `xml:"RegionId"`
  90. }
  91. type Product struct {
  92. ProductName string `xml:"ProductName"`
  93. DomainName string `xml:"DomainName"`
  94. }