save.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package site
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "runtime"
  7. "sync"
  8. "github.com/0xJacky/Nginx-UI/internal/helper"
  9. "github.com/0xJacky/Nginx-UI/internal/nginx"
  10. "github.com/0xJacky/Nginx-UI/internal/notification"
  11. "github.com/0xJacky/Nginx-UI/model"
  12. "github.com/0xJacky/Nginx-UI/query"
  13. "github.com/go-resty/resty/v2"
  14. "github.com/uozi-tech/cosy/logger"
  15. )
  16. // Save saves a site configuration file
  17. func Save(name string, content string, overwrite bool, siteCategoryId uint64, syncNodeIds []uint64) (err error) {
  18. path := nginx.GetConfPath("sites-available", name)
  19. if !overwrite && helper.FileExists(path) {
  20. return ErrDstFileExists
  21. }
  22. err = os.WriteFile(path, []byte(content), 0644)
  23. if err != nil {
  24. return
  25. }
  26. enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
  27. if helper.FileExists(enabledConfigFilePath) {
  28. // Test nginx configuration
  29. output := nginx.TestConf()
  30. if nginx.GetLogLevel(output) > nginx.Warn {
  31. return fmt.Errorf("%s", output)
  32. }
  33. output = nginx.Reload()
  34. if nginx.GetLogLevel(output) > nginx.Warn {
  35. return fmt.Errorf("%s", output)
  36. }
  37. }
  38. s := query.Site
  39. _, err = s.Where(s.Path.Eq(path)).
  40. Select(s.SiteCategoryID, s.SyncNodeIDs).
  41. Updates(&model.Site{
  42. SiteCategoryID: siteCategoryId,
  43. SyncNodeIDs: syncNodeIds,
  44. })
  45. if err != nil {
  46. return
  47. }
  48. go syncSave(name, content)
  49. return
  50. }
  51. func syncSave(name string, content string) {
  52. nodes := getSyncNodes(name)
  53. wg := &sync.WaitGroup{}
  54. wg.Add(len(nodes))
  55. for _, node := range nodes {
  56. go func() {
  57. defer func() {
  58. if err := recover(); err != nil {
  59. buf := make([]byte, 1024)
  60. runtime.Stack(buf, false)
  61. logger.Error(err)
  62. }
  63. }()
  64. defer wg.Done()
  65. client := resty.New()
  66. client.SetBaseURL(node.URL)
  67. resp, err := client.R().
  68. SetHeader("X-Node-Secret", node.Token).
  69. SetBody(map[string]interface{}{
  70. "content": content,
  71. "overwrite": true,
  72. }).
  73. Post(fmt.Sprintf("/api/sites/%s", name))
  74. if err != nil {
  75. notification.Error("Save Remote Site Error", err.Error(), nil)
  76. return
  77. }
  78. if resp.StatusCode() != http.StatusOK {
  79. notification.Error("Save Remote Site Error", "Save site %{name} to %{node} failed", NewSyncResult(node.Name, name, resp))
  80. return
  81. }
  82. notification.Success("Save Remote Site Success", "Save site %{name} to %{node} successfully", NewSyncResult(node.Name, name, resp))
  83. // Check if the site is enabled, if so then enable it on the remote node
  84. enabledConfigFilePath := nginx.GetConfPath("sites-enabled", name)
  85. if helper.FileExists(enabledConfigFilePath) {
  86. syncEnable(name)
  87. }
  88. }()
  89. }
  90. wg.Wait()
  91. }