sync.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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)).First()
  16. if err != nil {
  17. logger.Error(err)
  18. return
  19. }
  20. syncNodeIds := stream.SyncNodeIDs
  21. e := query.Environment
  22. nodes, err = e.Where(e.ID.In(syncNodeIds...)).Find()
  23. if err != nil {
  24. logger.Error(err)
  25. return
  26. }
  27. return
  28. }
  29. type SyncResult struct {
  30. StatusCode int `json:"status_code"`
  31. Node string `json:"node"`
  32. Name string `json:"name"`
  33. NewName string `json:"new_name,omitempty"`
  34. Response gin.H `json:"response"`
  35. Error string `json:"error"`
  36. }
  37. func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
  38. s = &SyncResult{
  39. StatusCode: resp.StatusCode(),
  40. Node: node,
  41. Name: siteName,
  42. }
  43. err := json.Unmarshal(resp.Body(), &s.Response)
  44. if err != nil {
  45. logger.Error(err)
  46. }
  47. return
  48. }
  49. func (s *SyncResult) SetNewName(name string) *SyncResult {
  50. s.NewName = name
  51. return s
  52. }