enhance(site): notification of sync

This commit is contained in:
Jacky 2024-10-26 11:26:29 +08:00
parent 497d5cddea
commit 2ee057a857
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
21 changed files with 1336 additions and 184 deletions

View file

@ -12,13 +12,14 @@ import (
"net/http"
"os"
"runtime"
"sync"
)
// Delete deletes a site by removing the file in sites-available
func Delete(name string) (err error) {
availablePath := nginx.GetConfPath("sites-available", name)
syncDelete(name)
s := query.Site
_, err = s.Where(s.Path.Eq(availablePath)).Unscoped().Delete(&model.Site{})
if err != nil {
@ -43,17 +44,12 @@ func Delete(name string) (err error) {
return
}
go syncDelete(name)
return
}
func syncDelete(name string) {
nodes := getSyncNodes(name)
wg := &sync.WaitGroup{}
wg.Add(len(nodes))
for _, node := range nodes {
go func() {
defer func() {
@ -63,23 +59,20 @@ func syncDelete(name string) {
logger.Error(err)
}
}()
defer wg.Done()
client := resty.New()
client.SetBaseURL(node.URL)
resp, err := client.R().
SetHeader("X-Node-Secret", node.Token).
Delete(fmt.Sprintf("/api/sites/%s", name))
if err != nil {
notification.Error("Delete Remote Site Error", err.Error())
return
}
if resp.StatusCode() != http.StatusOK {
notification.Error("Delete Remote Site Error", string(resp.Body()))
notification.Error("Delete Remote Site Error", NewSyncResult(node.Name, name, resp).String())
return
}
notification.Success("Delete Remote Site Success", string(resp.Body()))
notification.Success("Delete Remote Site Success", NewSyncResult(node.Name, name, resp).String())
}()
}
wg.Wait()
}

View file

@ -63,16 +63,17 @@ func syncDisable(name string) {
client := resty.New()
client.SetBaseURL(node.URL)
resp, err := client.R().
SetHeader("X-Node-Secret", node.Token).
Post(fmt.Sprintf("/api/sites/%s/disable", name))
if err != nil {
notification.Error("Disable Remote Site Error", err.Error())
return
}
if resp.StatusCode() != http.StatusOK {
notification.Error("Disable Remote Site Error", string(resp.Body()))
notification.Error("Disable Remote Site Error", NewSyncResult(node.Name, name, resp).String())
return
}
notification.Success("Disable Remote Site Success", string(resp.Body()))
notification.Success("Disable Remote Site Success", NewSyncResult(node.Name, name, resp).String())
}()
}

View file

@ -69,16 +69,17 @@ func syncEnable(name string) {
client := resty.New()
client.SetBaseURL(node.URL)
resp, err := client.R().
SetHeader("X-Node-Secret", node.Token).
Post(fmt.Sprintf("/api/sites/%s/enable", name))
if err != nil {
notification.Error("Enable Remote Site Error", err.Error())
return
}
if resp.StatusCode() != http.StatusOK {
notification.Error("Enable Remote Site Error", string(resp.Body()))
notification.Error("Enable Remote Site Error", NewSyncResult(node.Name, name, resp).String())
return
}
notification.Success("Enable Remote Site Success", string(resp.Body()))
notification.Success("Enable Remote Site Success", NewSyncResult(node.Name, name, resp).String())
}()
}

View file

@ -83,19 +83,24 @@ func syncRename(oldName, newName string) {
client := resty.New()
client.SetBaseURL(node.URL)
resp, err := client.R().
SetBody(map[string]string{
"new_name": newName,
}).
SetHeader("X-Node-Secret", node.Token).
SetBody(map[string]string{
"new_name": newName,
}).
Post(fmt.Sprintf("/api/sites/%s/rename", oldName))
if err != nil {
notification.Error("Rename Remote Site Error", err.Error())
return
}
if resp.StatusCode() != http.StatusOK {
notification.Error("Rename Remote Site Error", string(resp.Body()))
notification.Error("Rename Remote Site Error",
NewSyncResult(node.Name, oldName, resp).
SetNewName(newName).String())
return
}
notification.Success("Rename Remote Site Success", string(resp.Body()))
notification.Success("Rename Remote Site Success",
NewSyncResult(node.Name, oldName, resp).
SetNewName(newName).String())
}()
}

View file

@ -46,10 +46,10 @@ func Save(name string, content string, overwrite bool, siteCategoryId uint64, sy
s := query.Site
_, err = s.Where(s.Path.Eq(path)).
Select(s.SiteCategoryID, s.SyncNodeIDs).
Updates(&model.Site{
SiteCategoryID: siteCategoryId,
SyncNodeIDs: syncNodeIds,
})
Updates(&model.Site{
SiteCategoryID: siteCategoryId,
SyncNodeIDs: syncNodeIds,
})
if err != nil {
return
}
@ -79,20 +79,21 @@ func syncSave(name string, content string) {
client := resty.New()
client.SetBaseURL(node.URL)
resp, err := client.R().
SetBody(map[string]interface{}{
"content": content,
"overwrite": true,
}).
SetHeader("X-Node-Secret", node.Token).
SetBody(map[string]interface{}{
"content": content,
"overwrite": true,
}).
Post(fmt.Sprintf("/api/sites/%s", name))
if err != nil {
notification.Error("Save Remote Site Error", err.Error())
return
}
if resp.StatusCode() != http.StatusOK {
notification.Error("Save Remote Site Error", string(resp.Body()))
notification.Error("Save Remote Site Error", NewSyncResult(node.Name, name, resp).String())
return
}
notification.Success("Save Remote Site Success", string(resp.Body()))
notification.Success("Save Remote Site Success", NewSyncResult(node.Name, name, resp).String())
}()
}

View file

@ -1,13 +1,17 @@
package site
import (
"encoding/json"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/samber/lo"
"github.com/uozi-tech/cosy/logger"
)
// getSyncNodes returns the nodes that need to be synchronized by site name
func getSyncNodes(name string) (nodes []*model.Environment) {
configFilePath := nginx.GetConfPath("sites-available", name)
s := query.Site
@ -33,3 +37,37 @@ func getSyncNodes(name string) (nodes []*model.Environment) {
}
return
}
type SyncResult struct {
StatusCode int `json:"status_code"`
Node string `json:"node"`
Name string `json:"name"`
NewName string `json:"new_name,omitempty"`
Response gin.H `json:"response"`
}
func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {
s = &SyncResult{
StatusCode: resp.StatusCode(),
Node: node,
Name: siteName,
}
err := json.Unmarshal(resp.Body(), &s.Response)
if err != nil {
logger.Error(err)
}
return
}
func (s *SyncResult) SetNewName(name string) *SyncResult {
s.NewName = name
return s
}
func (s *SyncResult) String() string {
b, err := json.Marshal(s)
if err != nil {
logger.Error(err)
}
return string(b)
}