enhance: nginx log

This commit is contained in:
Jacky 2025-04-02 18:56:15 +08:00
parent 2e284c5aa1
commit 56f4e5b87f
No known key found for this signature in database
GPG key ID: 215C21B10DF38B4D
24 changed files with 1685 additions and 347 deletions

50
api/nginx_log/sse.go Normal file
View 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
}
}
}