mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-12 02:45:49 +02:00
enhance: nginx log
This commit is contained in:
parent
2e284c5aa1
commit
56f4e5b87f
24 changed files with 1685 additions and 347 deletions
50
api/nginx_log/sse.go
Normal file
50
api/nginx_log/sse.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package nginx_log
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cache"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetNginxLogsLive is an SSE endpoint that sends real-time log scanning status updates
|
||||
func GetNginxLogsLive(c *gin.Context) {
|
||||
api.SetSSEHeaders(c)
|
||||
notify := c.Writer.CloseNotify()
|
||||
|
||||
// Subscribe to scanner status changes
|
||||
statusChan := cache.SubscribeStatusChanges()
|
||||
|
||||
// Ensure we unsubscribe when the handler exits
|
||||
defer cache.UnsubscribeStatusChanges(statusChan)
|
||||
|
||||
// Main event loop
|
||||
for {
|
||||
select {
|
||||
case status, ok := <-statusChan:
|
||||
// If channel closed, exit
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Send status update
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("message", gin.H{
|
||||
"scanning": status,
|
||||
})
|
||||
return false
|
||||
})
|
||||
case <-time.After(30 * time.Second):
|
||||
// Send heartbeat to keep connection alive
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("heartbeat", "")
|
||||
return false
|
||||
})
|
||||
case <-notify:
|
||||
// Client disconnected
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue