tree.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/config/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. var (
  14. tokenURI = "http://easyst.bilibili.co/v1/token"
  15. dataURI = "http://easyst.bilibili.co/v1/node/apptree"
  16. authURI = "http://easyst.bilibili.co/v1/auth"
  17. nodeURI = "http://easyst.bilibili.co/v1/node/bilibili%s"
  18. appsURI = "http://easyst.bilibili.co/v1/node/role/app"
  19. prefix = []byte("bilibili.")
  20. )
  21. // Token get Token.
  22. func (d *Dao) Token(c context.Context, body string) (msg map[string]interface{}, err error) {
  23. var (
  24. req *http.Request
  25. )
  26. if req, err = http.NewRequest("POST", tokenURI, strings.NewReader(body)); err != nil {
  27. log.Error("Token url(%s) error(%v)", tokenURI, err)
  28. return
  29. }
  30. var res struct {
  31. Code int `json:"code"`
  32. Data map[string]interface{} `json:"data"`
  33. Message string `json:"message"`
  34. Status int `json:"status"`
  35. }
  36. if err = d.client.Do(c, req, &res); err != nil {
  37. log.Error("d.Token url(%s) res(%+v) err(%v)", tokenURI, res, err)
  38. return
  39. }
  40. if res.Code != 90000 {
  41. err = fmt.Errorf("error code :%d", res.Code)
  42. log.Error("Status url(%s) res(%v)", tokenURI, res)
  43. return
  44. }
  45. msg = res.Data
  46. return
  47. }
  48. // Auth get Token.
  49. func (d *Dao) Auth(c context.Context, cookie string) (msg map[string]interface{}, err error) {
  50. var (
  51. req *http.Request
  52. )
  53. if req, err = http.NewRequest("GET", authURI, nil); err != nil {
  54. log.Error("Token url(%s) error(%v)", tokenURI, err)
  55. return
  56. }
  57. req.Header.Set("Cookie", cookie)
  58. var res struct {
  59. Code int `json:"code"`
  60. Data map[string]interface{} `json:"data"`
  61. Message string `json:"message"`
  62. Status int `json:"status"`
  63. }
  64. if err = d.client.Do(c, req, &res); err != nil {
  65. log.Error("d.Token url(%s) res($s) err(%v)", tokenURI, res, err)
  66. return
  67. }
  68. if res.Code != 90000 {
  69. err = fmt.Errorf("error code :%d", res.Code)
  70. log.Error("Status url(%s) res(%v)", tokenURI, res)
  71. return
  72. }
  73. msg = res.Data
  74. return
  75. }
  76. // Tree get service tree.
  77. func (d *Dao) Tree(c context.Context, token string) (data interface{}, err error) {
  78. var (
  79. req *http.Request
  80. tmp map[string]interface{}
  81. ok bool
  82. )
  83. if req, err = http.NewRequest("GET", dataURI, nil); err != nil {
  84. log.Error("Status url(%s) error(%v)", dataURI, err)
  85. return
  86. }
  87. req.Header.Set("X-Authorization-Token", token)
  88. req.Header.Set("Content-Type", "application/json")
  89. var res struct {
  90. Code int `json:"code"`
  91. Data map[string]map[string]interface{} `json:"data"`
  92. Message string `json:"message"`
  93. Status int `json:"status"`
  94. }
  95. if err = d.client.Do(c, req, &res); err != nil {
  96. log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
  97. return
  98. }
  99. if res.Code != 90000 {
  100. err = fmt.Errorf("error code :%d", res.Code)
  101. log.Error("Status url(%s) res(%v)", dataURI, res)
  102. return
  103. }
  104. if tmp, ok = res.Data["bilibili"]; ok {
  105. data, ok = tmp["children"]
  106. }
  107. if !ok {
  108. err = ecode.NothingFound
  109. }
  110. return
  111. }
  112. // Role get service tree.
  113. func (d *Dao) Role(c context.Context, user, token string) (nodes *model.CacheData, err error) {
  114. var (
  115. req *http.Request
  116. )
  117. if req, err = http.NewRequest("GET", appsURI, nil); err != nil {
  118. log.Error("Status url(%s) error(%v)", dataURI, err)
  119. return
  120. }
  121. req.Header.Set("X-Authorization-Token", token)
  122. req.Header.Set("Content-Type", "application/json")
  123. var res struct {
  124. Code int `json:"code"`
  125. Data []*model.RoleNode `json:"data"`
  126. Message string `json:"message"`
  127. Status int `json:"status"`
  128. }
  129. if err = d.client.Do(c, req, &res); err != nil {
  130. log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
  131. return
  132. }
  133. if res.Code != 90000 {
  134. err = fmt.Errorf("error code :%d", res.Code)
  135. log.Error("Status url(%s) res(%v)", dataURI, res)
  136. return
  137. }
  138. nodes = &model.CacheData{Data: make(map[int64]*model.RoleNode)}
  139. nodes.CTime = time.Now()
  140. for _, node := range res.Data {
  141. if bytes.Equal(prefix, []byte(node.Path)[0:9]) {
  142. node.Path = string([]byte(node.Path)[9:])
  143. }
  144. nodes.Data[node.ID] = node
  145. }
  146. return
  147. }
  148. // NodeTree get service tree.
  149. func (d *Dao) NodeTree(c context.Context, token, bu, team string) (nodes []*model.Node, err error) {
  150. var (
  151. req *http.Request
  152. node string
  153. )
  154. if len(bu) != 0 {
  155. node = "." + bu
  156. }
  157. if len(team) != 0 {
  158. node = "." + team
  159. }
  160. if len(node) == 0 {
  161. nodes = append(nodes, &model.Node{Name: "main", Path: "main"})
  162. nodes = append(nodes, &model.Node{Name: "ai", Path: "ai"})
  163. return
  164. }
  165. if req, err = http.NewRequest("GET", fmt.Sprintf(nodeURI, node), nil); err != nil {
  166. log.Error("Status url(%s) error(%v)", dataURI, err)
  167. return
  168. }
  169. req.Header.Set("X-Authorization-Token", token)
  170. req.Header.Set("Content-Type", "application/json")
  171. var res struct {
  172. Code int `json:"code"`
  173. Data *model.Res `json:"data"`
  174. Message string `json:"message"`
  175. Status int `json:"status"`
  176. }
  177. if err = d.client.Do(c, req, &res); err != nil {
  178. log.Error("d.Status url(%s) res($s) err(%v)", dataURI, res, err)
  179. return
  180. }
  181. if res.Code != 90000 {
  182. err = fmt.Errorf("error code :%d", res.Code)
  183. log.Error("Status url(%s) error(%v)", dataURI, err)
  184. return
  185. }
  186. for _, tnode := range res.Data.Data {
  187. if bytes.Equal(prefix, []byte(tnode.Path)[0:9]) {
  188. tnode.Path = string([]byte(tnode.Path)[9:])
  189. }
  190. nodes = append(nodes, &model.Node{Name: tnode.Name, Path: tnode.Path})
  191. }
  192. return
  193. }