sync.go 1.9 KB

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