save.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package stream
  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, syncNodeIds []uint64) (err error) {
  18. path := nginx.GetConfPath("streams-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("streams-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.Stream
  39. _, err = s.Where(s.Path.Eq(path)).
  40. Select(s.SyncNodeIDs).
  41. Updates(&model.Site{
  42. SyncNodeIDs: syncNodeIds,
  43. })
  44. if err != nil {
  45. return
  46. }
  47. go syncSave(name, content)
  48. return
  49. }
  50. func syncSave(name string, content string) {
  51. nodes := getSyncNodes(name)
  52. wg := &sync.WaitGroup{}
  53. wg.Add(len(nodes))
  54. for _, node := range nodes {
  55. go func() {
  56. defer func() {
  57. if err := recover(); err != nil {
  58. buf := make([]byte, 1024)
  59. runtime.Stack(buf, false)
  60. logger.Error(err)
  61. }
  62. }()
  63. defer wg.Done()
  64. client := resty.New()
  65. client.SetBaseURL(node.URL)
  66. resp, err := client.R().
  67. SetHeader("X-Node-Secret", node.Token).
  68. SetBody(map[string]interface{}{
  69. "content": content,
  70. "overwrite": true,
  71. }).
  72. Post(fmt.Sprintf("/api/streams/%s", name))
  73. if err != nil {
  74. notification.Error("Save Remote Stream Error", err.Error(), nil)
  75. return
  76. }
  77. if resp.StatusCode() != http.StatusOK {
  78. notification.Error("Save Remote Stream Error", "Save stream %{name} to %{node} failed", NewSyncResult(node.Name, name, resp))
  79. return
  80. }
  81. notification.Success("Save Remote Stream Success", "Save stream %{name} to %{node} successfully", NewSyncResult(node.Name, name, resp))
  82. // Check if the site is enabled, if so then enable it on the remote node
  83. enabledConfigFilePath := nginx.GetConfPath("streams-enabled", name)
  84. if helper.FileExists(enabledConfigFilePath) {
  85. syncEnable(name)
  86. }
  87. }()
  88. }
  89. wg.Wait()
  90. }