processing.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package system
  2. import (
  3. "time"
  4. "io"
  5. "github.com/0xJacky/Nginx-UI/api"
  6. "github.com/0xJacky/Nginx-UI/internal/cache"
  7. "github.com/0xJacky/Nginx-UI/internal/cert"
  8. "github.com/gin-gonic/gin"
  9. )
  10. type ProcessingStatus struct {
  11. IndexScanning bool `json:"index_scanning"`
  12. AutoCertProcessing bool `json:"auto_cert_processing"`
  13. }
  14. // GetProcessingStatus is an SSE endpoint that sends real-time processing status updates
  15. func GetProcessingStatus(c *gin.Context) {
  16. api.SetSSEHeaders(c)
  17. notify := c.Writer.CloseNotify()
  18. indexScanning := cache.SubscribeScanningStatus()
  19. defer cache.UnsubscribeScanningStatus(indexScanning)
  20. autoCert := cert.SubscribeProcessingStatus()
  21. defer cert.UnsubscribeProcessingStatus(autoCert)
  22. // Track current status
  23. status := ProcessingStatus{
  24. IndexScanning: false,
  25. AutoCertProcessing: false,
  26. }
  27. sendStatus := func() {
  28. c.Stream(func(w io.Writer) bool {
  29. c.SSEvent("message", status)
  30. return false
  31. })
  32. }
  33. for {
  34. select {
  35. case indexStatus, ok := <-indexScanning:
  36. if !ok {
  37. return
  38. }
  39. status.IndexScanning = indexStatus
  40. sendStatus()
  41. case certStatus, ok := <-autoCert:
  42. if !ok {
  43. return
  44. }
  45. status.AutoCertProcessing = certStatus
  46. sendStatus()
  47. case <-time.After(30 * time.Second):
  48. c.Stream(func(w io.Writer) bool {
  49. c.SSEvent("heartbeat", "")
  50. return false
  51. })
  52. case <-notify:
  53. // Client disconnected
  54. return
  55. }
  56. }
  57. }