mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 10:25:52 +02:00
feat: sse notify
This commit is contained in:
parent
b4add42039
commit
e6e1876c54
11 changed files with 187 additions and 47 deletions
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue