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.

server.go 1.5 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package scanner
  2. import (
  3. "gitlink.org.cn/cloudream/common/pkg/mq"
  4. mymq "gitlink.org.cn/cloudream/storage-common/pkgs/mq"
  5. "gitlink.org.cn/cloudream/storage-common/pkgs/mq/config"
  6. )
  7. // Service 协调端接口
  8. type Service interface {
  9. EventService
  10. }
  11. type Server struct {
  12. service Service
  13. rabbitSvr mq.RabbitMQServer
  14. OnError func(err error)
  15. }
  16. func NewServer(svc Service, cfg *config.Config) (*Server, error) {
  17. srv := &Server{
  18. service: svc,
  19. }
  20. rabbitSvr, err := mq.NewRabbitMQServer(
  21. cfg.MakeConnectingURL(),
  22. mymq.SCANNER_QUEUE_NAME,
  23. func(msg *mq.Message) (*mq.Message, error) {
  24. return msgDispatcher.Handle(srv.service, msg)
  25. },
  26. )
  27. if err != nil {
  28. return nil, err
  29. }
  30. srv.rabbitSvr = *rabbitSvr
  31. return srv, nil
  32. }
  33. func (s *Server) Stop() {
  34. s.rabbitSvr.Close()
  35. }
  36. func (s *Server) Serve() error {
  37. return s.rabbitSvr.Serve()
  38. }
  39. var msgDispatcher mq.MessageDispatcher = mq.NewMessageDispatcher()
  40. // Register 将Service中的一个接口函数作为指定类型消息的处理函数
  41. // TODO 需要约束:Service实现了TSvc接口
  42. func Register[TSvc any, TReq any, TResp any](svcFn func(svc TSvc, msg *TReq) (*TResp, *mq.CodeMessage)) {
  43. mq.AddServiceFn(&msgDispatcher, svcFn)
  44. }
  45. // RegisterNoReply 将Service中的一个*没有返回值的*接口函数作为指定类型消息的处理函数
  46. // TODO 需要约束:Service实现了TSvc接口
  47. func RegisterNoReply[TSvc any, TReq any](svcFn func(svc TSvc, msg *TReq)) {
  48. mq.AddNoRespServiceFn(&msgDispatcher, svcFn)
  49. }

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