sync.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package stream
  2. import (
  3. "encoding/json"
  4. "github.com/0xJacky/Nginx-UI/internal/nginx"
  5. "github.com/0xJacky/Nginx-UI/model"
  6. "github.com/0xJacky/Nginx-UI/query"
  7. "github.com/gin-gonic/gin"
  8. "github.com/go-resty/resty/v2"
  9. "github.com/samber/lo"
  10. "github.com/uozi-tech/cosy/logger"
  11. )
  12. // getSyncNodes returns the nodes that need to be synchronized by site name
  13. func getSyncNodes(name string) (nodes []*model.Environment) {
  14. configFilePath := nginx.GetConfPath("streams-available", name)
  15. s := query.Site
  16. site, err := s.Where(s.Path.Eq(configFilePath)).
  17. Preload(s.SiteCategory).First()
  18. if err != nil {
  19. logger.Error(err)
  20. return
  21. }
  22. syncNodeIds := site.SyncNodeIDs
  23. // inherit sync node ids from site category
  24. if site.SiteCategory != nil {
  25. syncNodeIds = append(syncNodeIds, site.SiteCategory.SyncNodeIds...)
  26. }
  27. syncNodeIds = lo.Uniq(syncNodeIds)
  28. e := query.Environment
  29. nodes, err = e.Where(e.ID.In(syncNodeIds...)).Find()
  30. if err != nil {
  31. logger.Error(err)
  32. return
  33. }
  34. return
  35. }
  36. type SyncResult struct {
  37. StatusCode int `json:"status_code"`
  38. Node string `json:"node"`
  39. Name string `json:"name"`
  40. NewName string `json:"new_name,omitempty"`
  41. Response gin.H `json:"response"`
  42. Error string `json:"error"`
  43. }
  44. func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
  45. s = &SyncResult{
  46. StatusCode: resp.StatusCode(),
  47. Node: node,
  48. Name: siteName,
  49. }
  50. err := json.Unmarshal(resp.Body(), &s.Response)
  51. if err != nil {
  52. logger.Error(err)
  53. }
  54. return
  55. }
  56. func (s *SyncResult) SetNewName(name string) *SyncResult {
  57. s.NewName = name
  58. return s
  59. }
  60. func (s *SyncResult) String() string {
  61. b, err := json.Marshal(s)
  62. if err != nil {
  63. logger.Error(err)
  64. }
  65. return string(b)
  66. }