sync.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // getSyncNodes returns the nodes that need to be synchronized by site name
  13. func getSyncNodes(name string) (nodes []*model.Environment) {
  14. configFilePath := nginx.GetConfPath("sites-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. }
  43. func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
  44. s = &SyncResult{
  45. StatusCode: resp.StatusCode(),
  46. Node: node,
  47. Name: siteName,
  48. }
  49. err := json.Unmarshal(resp.Body(), &s.Response)
  50. if err != nil {
  51. logger.Error(err)
  52. }
  53. return
  54. }
  55. func (s *SyncResult) SetNewName(name string) *SyncResult {
  56. s.NewName = name
  57. return s
  58. }
  59. func (s *SyncResult) String() string {
  60. b, err := json.Marshal(s)
  61. if err != nil {
  62. logger.Error(err)
  63. }
  64. return string(b)
  65. }