sync.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // getSyncData returns the nodes that need to be synchronized by stream name and the post-sync action
  12. func getSyncData(name string) (nodes []*model.Environment, postSyncAction string) {
  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 stream category
  23. if stream.EnvGroup != nil {
  24. syncNodeIds = append(syncNodeIds, stream.EnvGroup.SyncNodeIds...)
  25. postSyncAction = stream.EnvGroup.PostSyncAction
  26. }
  27. e := query.Environment
  28. nodes, err = e.Where(e.ID.In(syncNodeIds...)).Find()
  29. if err != nil {
  30. logger.Error(err)
  31. return
  32. }
  33. return
  34. }
  35. // getSyncNodes returns the nodes that need to be synchronized by stream name (for backward compatibility)
  36. func getSyncNodes(name string) (nodes []*model.Environment) {
  37. nodes, _ = getSyncData(name)
  38. return
  39. }
  40. type SyncResult struct {
  41. StatusCode int `json:"status_code"`
  42. Node string `json:"node"`
  43. Name string `json:"name"`
  44. NewName string `json:"new_name,omitempty"`
  45. Response gin.H `json:"response"`
  46. Error string `json:"error"`
  47. }
  48. func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
  49. s = &SyncResult{
  50. StatusCode: resp.StatusCode(),
  51. Node: node,
  52. Name: siteName,
  53. }
  54. err := json.Unmarshal(resp.Body(), &s.Response)
  55. if err != nil {
  56. logger.Error(err)
  57. }
  58. return
  59. }
  60. func (s *SyncResult) SetNewName(name string) *SyncResult {
  61. s.NewName = name
  62. return s
  63. }