sync.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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/uozi-tech/cosy/logger"
  10. )
  11. // getSyncNodes returns the nodes that need to be synchronized by site name
  12. func getSyncNodes(name string) (nodes []*model.Environment) {
  13. configFilePath := nginx.GetConfPath("streams-available", name)
  14. s := query.Stream
  15. stream, err := s.Where(s.Path.Eq(configFilePath)).
  16. Preload(s.EnvGroup).First()
  17. if err != nil {
  18. logger.Error(err)
  19. return
  20. }
  21. syncNodeIds := stream.SyncNodeIDs
  22. // inherit sync node ids from site category
  23. if stream.EnvGroup != nil {
  24. syncNodeIds = append(syncNodeIds, stream.EnvGroup.SyncNodeIds...)
  25. }
  26. e := query.Environment
  27. nodes, err = e.Where(e.ID.In(syncNodeIds...)).Find()
  28. if err != nil {
  29. logger.Error(err)
  30. return
  31. }
  32. return
  33. }
  34. type SyncResult struct {
  35. StatusCode int `json:"status_code"`
  36. Node string `json:"node"`
  37. Name string `json:"name"`
  38. NewName string `json:"new_name,omitempty"`
  39. Response gin.H `json:"response"`
  40. Error string `json:"error"`
  41. }
  42. func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
  43. s = &SyncResult{
  44. StatusCode: resp.StatusCode(),
  45. Node: node,
  46. Name: siteName,
  47. }
  48. err := json.Unmarshal(resp.Body(), &s.Response)
  49. if err != nil {
  50. logger.Error(err)
  51. }
  52. return
  53. }
  54. func (s *SyncResult) SetNewName(name string) *SyncResult {
  55. s.NewName = name
  56. return s
  57. }