mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
refactor: system processsing status pub/sub
This commit is contained in:
parent
b5ee43b2b0
commit
9ee84dd138
50 changed files with 2239 additions and 2306 deletions
|
@ -1,8 +0,0 @@
|
|||
package index
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// InitRouter registers all the index related routes
|
||||
func InitRouter(r *gin.RouterGroup) {
|
||||
r.GET("index/status", GetIndexStatus)
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package index
|
||||
|
||||
import (
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cache"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetIndexStatus is an SSE endpoint that sends real-time index status updates
|
||||
func GetIndexStatus(c *gin.Context) {
|
||||
api.SetSSEHeaders(c)
|
||||
notify := c.Writer.CloseNotify()
|
||||
|
||||
// Subscribe to scanner status changes
|
||||
statusChan := cache.SubscribeScanningStatus()
|
||||
|
||||
// Ensure we unsubscribe when the handler exits
|
||||
defer cache.UnsubscribeScanningStatus(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
|
||||
}
|
||||
}
|
||||
}
|
66
api/system/processing.go
Normal file
66
api/system/processing.go
Normal file
|
@ -0,0 +1,66 @@
|
|||
package system
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"io"
|
||||
|
||||
"github.com/0xJacky/Nginx-UI/api"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cache"
|
||||
"github.com/0xJacky/Nginx-UI/internal/cert"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ProcessingStatus struct {
|
||||
IndexScanning bool `json:"index_scanning"`
|
||||
AutoCertProcessing bool `json:"auto_cert_processing"`
|
||||
}
|
||||
|
||||
// GetProcessingStatus is an SSE endpoint that sends real-time processing status updates
|
||||
func GetProcessingStatus(c *gin.Context) {
|
||||
api.SetSSEHeaders(c)
|
||||
notify := c.Writer.CloseNotify()
|
||||
|
||||
indexScanning := cache.SubscribeScanningStatus()
|
||||
defer cache.UnsubscribeScanningStatus(indexScanning)
|
||||
autoCert := cert.SubscribeProcessingStatus()
|
||||
defer cert.UnsubscribeProcessingStatus(autoCert)
|
||||
|
||||
// Track current status
|
||||
status := ProcessingStatus{
|
||||
IndexScanning: false,
|
||||
AutoCertProcessing: false,
|
||||
}
|
||||
|
||||
sendStatus := func() {
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("message", status)
|
||||
return false
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
select {
|
||||
case indexStatus, ok := <-indexScanning:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
status.IndexScanning = indexStatus
|
||||
sendStatus()
|
||||
case certStatus, ok := <-autoCert:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
status.AutoCertProcessing = certStatus
|
||||
sendStatus()
|
||||
case <-time.After(30 * time.Second):
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
c.SSEvent("heartbeat", "")
|
||||
return false
|
||||
})
|
||||
case <-notify:
|
||||
// Client disconnected
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ func InitPrivateRouter(r *gin.RouterGroup) {
|
|||
r.GET("upgrade/current", GetCurrentVersion)
|
||||
|
||||
r.GET("system/backup", CreateBackup)
|
||||
r.GET("system/processing", GetProcessingStatus)
|
||||
}
|
||||
|
||||
func InitSelfCheckRouter(r *gin.RouterGroup) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue