tree.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "go-common/app/service/main/msm/model"
  9. "go-common/library/conf/env"
  10. "go-common/library/log"
  11. )
  12. const (
  13. treeUsername = "msm"
  14. treeAuthURL = "/v1/token"
  15. allAppAuthURL = "%s/v1/node/app/secretinfo/%s"
  16. )
  17. func (d *Dao) treeToken(c context.Context) (token string, err error) {
  18. var (
  19. jsonBytes []byte
  20. url = d.treeHost + treeAuthURL
  21. )
  22. body := &struct {
  23. Username string `json:"user_name"`
  24. PlatformID string `json:"platform_id"`
  25. }{
  26. Username: treeUsername,
  27. PlatformID: d.platformID,
  28. }
  29. if jsonBytes, err = json.Marshal(body); err != nil {
  30. log.Error("json.Marshal(body) error(%v)", err)
  31. return
  32. }
  33. req, err := http.NewRequest("POST", url, strings.NewReader(string(jsonBytes)))
  34. if err != nil {
  35. log.Error("http.NewRequest failed", err)
  36. return
  37. }
  38. req.Header.Set("Content-Type", "application/json")
  39. res := &struct {
  40. Code int64 `json:"code"`
  41. Data struct {
  42. Token string `json:"token"`
  43. Username string `json:"user_name"`
  44. Secret string `json:"secret"`
  45. Expired int64 `json:"expired"`
  46. } `json:"data"`
  47. Message string `json:"message"`
  48. Status int `json:"status"`
  49. }{}
  50. if err = d.client.Do(c, req, res); err != nil {
  51. log.Error("service-tree client Do failed", err)
  52. return
  53. }
  54. if res.Code != 90000 {
  55. log.Error("service-tree client Do failed", err)
  56. return
  57. }
  58. token = res.Data.Token
  59. return
  60. }
  61. // TreeAppInfo TreeAppInfo.
  62. func (d *Dao) TreeAppInfo(c context.Context) (appInfo map[int64]*model.AppToken, err error) {
  63. var (
  64. token string
  65. url = fmt.Sprintf(allAppAuthURL, d.treeHost, env.DeployEnv)
  66. )
  67. appInfo = make(map[int64]*model.AppToken)
  68. if token, err = d.treeToken(c); err != nil {
  69. return
  70. }
  71. req, err := http.NewRequest("GET", url, nil)
  72. if err != nil {
  73. log.Error("http.NewRequest failed", err)
  74. return
  75. }
  76. req.Header.Set("Content-Type", "application/json")
  77. req.Header.Set("X-Authorization-Token", token)
  78. res := &struct {
  79. Code int `json:"code"`
  80. Data []*model.AppToken `json:"data"`
  81. Message string `json:"message"`
  82. Status int `json:"status"`
  83. }{}
  84. if err = d.client.Do(c, req, res); err != nil {
  85. log.Error("service-tree client Do failed", err)
  86. return
  87. }
  88. if res.Code != 90000 {
  89. log.Error("service-tree client Do failed", err)
  90. return
  91. }
  92. for _, auth := range res.Data {
  93. appInfo[auth.AppTreeID] = auth
  94. }
  95. return
  96. }