feat: sse notify

This commit is contained in:
Jacky 2024-11-03 23:46:47 +08:00
parent b4add42039
commit e6e1876c54
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
11 changed files with 187 additions and 47 deletions

View file

@ -3,8 +3,29 @@ package notification
import (
"github.com/0xJacky/Nginx-UI/model"
"github.com/0xJacky/Nginx-UI/query"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy/logger"
"sync"
)
var (
clientMap = make(map[*gin.Context]chan *model.Notification)
mutex = &sync.RWMutex{}
)
func SetClient(c *gin.Context, evtChan chan *model.Notification) {
mutex.Lock()
defer mutex.Unlock()
clientMap[c] = evtChan
}
func RemoveClient(c *gin.Context) {
mutex.Lock()
defer mutex.Unlock()
close(clientMap[c])
delete(clientMap, c)
}
func Info(title string, details string) {
push(model.NotificationInfo, title, details)
}
@ -24,9 +45,24 @@ func Success(title string, details string) {
func push(nType model.NotificationType, title string, details string) {
n := query.Notification
_ = n.Create(&model.Notification{
data := &model.Notification{
Type: nType,
Title: title,
Details: details,
})
}
err := n.Create(data)
if err != nil {
logger.Error(err)
return
}
broadcast(data)
}
func broadcast(data *model.Notification) {
mutex.RLock()
defer mutex.RUnlock()
for _, evtChan := range clientMap {
evtChan <- data
}
}

View file

@ -44,6 +44,7 @@ type SyncResult struct {
Name string `json:"name"`
NewName string `json:"new_name,omitempty"`
Response gin.H `json:"response"`
Error string `json:"error"`
}
func NewSyncResult(node string, siteName string, resp *resty.Response) (s *SyncResult) {