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

31
api/notification/live.go Normal file
View file

@ -0,0 +1,31 @@
package notification
import (
"github.com/0xJacky/Nginx-UI/internal/notification"
"github.com/0xJacky/Nginx-UI/model"
"github.com/gin-gonic/gin"
"io"
)
func Live(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
evtChan := make(chan *model.Notification)
notification.SetClient(c, evtChan)
notify := c.Writer.CloseNotify()
go func() {
<-notify
notification.RemoveClient(c)
}()
for n := range evtChan {
c.Stream(func(w io.Writer) bool {
c.SSEvent("message", n)
return false
})
}
}

View file

@ -7,4 +7,6 @@ func InitRouter(r *gin.RouterGroup) {
r.GET("notifications/:id", Get)
r.DELETE("notifications/:id", Destroy)
r.DELETE("notifications", DestroyAll)
r.GET("notifications/live", Live)
}