You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sysevent.go 1.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package http
  2. import (
  3. "net/http"
  4. "github.com/gin-gonic/gin"
  5. "gitlink.org.cn/cloudream/common/pkgs/logger"
  6. "gitlink.org.cn/cloudream/common/utils/serder"
  7. "gitlink.org.cn/cloudream/jcs-pub/client/internal/http/types"
  8. cliapi "gitlink.org.cn/cloudream/jcs-pub/client/sdk/api/v1"
  9. "gitlink.org.cn/cloudream/jcs-pub/common/ecode"
  10. "gitlink.org.cn/cloudream/jcs-pub/common/pkgs/sysevent"
  11. )
  12. type SysEventService struct {
  13. *Server
  14. }
  15. func (s *Server) SysEvent() *SysEventService {
  16. return &SysEventService{s}
  17. }
  18. func (s *SysEventService) Watch(ctx *gin.Context) {
  19. log := logger.WithField("HTTP", "SysEvent.Watch")
  20. _, err := types.ShouldBindJSONEx[cliapi.SysEventWatch](ctx)
  21. if err != nil {
  22. log.Warnf("binding body: %s", err.Error())
  23. ctx.JSON(http.StatusBadRequest, types.Failed(ecode.BadArgument, "%v", err))
  24. return
  25. }
  26. if s.svc.EvtWatcher == nil {
  27. ctx.JSON(http.StatusOK, types.Failed(ecode.OperationFailed, "event watcher not configured"))
  28. return
  29. }
  30. ctx.Writer.Header().Set("Content-Type", "text/event-stream")
  31. ctx.Writer.Header().Set("Cache-Control", "no-cache")
  32. ctx.Writer.Header().Set("Connection", "keep-alive")
  33. wtchr := s.svc.EvtWatcher.AddWatcherFn(func(event sysevent.SysEvent) {
  34. data, err := serder.ObjectToJSONEx(event)
  35. if err != nil {
  36. log.Warnf("serializing event: %s", err.Error())
  37. return
  38. }
  39. ctx.SSEvent("message", string(data))
  40. ctx.Writer.Flush()
  41. })
  42. <-ctx.Request.Context().Done()
  43. s.svc.EvtWatcher.RemoveWatcher(wtchr)
  44. log.Debugf("event watcher stopped")
  45. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。