processing.go 1.5 KB

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