service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * @Author: LinkLeong link@icewhale.com
  3. * @Date: 2022-07-12 09:48:56
  4. * @LastEditors: LinkLeong
  5. * @LastEditTime: 2022-09-02 22:10:05
  6. * @FilePath: /CasaOS/service/service.go
  7. * @Description:
  8. * @Website: https://www.casaos.io
  9. * Copyright (c) 2022 by icewhale, All Rights Reserved.
  10. */
  11. package service
  12. import (
  13. "github.com/IceWhaleTech/CasaOS-Common/external"
  14. "github.com/IceWhaleTech/CasaOS/codegen/message_bus"
  15. "github.com/IceWhaleTech/CasaOS/pkg/config"
  16. "github.com/gorilla/websocket"
  17. "github.com/patrickmn/go-cache"
  18. "gorm.io/gorm"
  19. )
  20. var Cache *cache.Cache
  21. var (
  22. MyService Repository
  23. )
  24. var (
  25. WebSocketConns []*websocket.Conn
  26. SocketRun bool
  27. )
  28. type Repository interface {
  29. Casa() CasaService
  30. Connections() ConnectionsService
  31. Gateway() external.ManagementService
  32. Health() HealthService
  33. Notify() NotifyServer
  34. Rely() RelyService
  35. Shares() SharesService
  36. System() SystemService
  37. Storage() StorageService
  38. MessageBus() *message_bus.ClientWithResponses
  39. Peer() PeerService
  40. Other() OtherService
  41. }
  42. func NewService(db *gorm.DB, RuntimePath string) Repository {
  43. gatewayManagement, err := external.NewManagementService(RuntimePath)
  44. if err != nil && len(RuntimePath) > 0 {
  45. panic(err)
  46. }
  47. return &store{
  48. casa: NewCasaService(),
  49. connections: NewConnectionsService(db),
  50. gateway: gatewayManagement,
  51. notify: NewNotifyService(db),
  52. rely: NewRelyService(db),
  53. system: NewSystemService(),
  54. health: NewHealthService(),
  55. shares: NewSharesService(db),
  56. storage: NewStorageService(),
  57. other: NewOtherService(),
  58. peer: NewPeerService(db),
  59. }
  60. }
  61. type store struct {
  62. peer PeerService
  63. db *gorm.DB
  64. casa CasaService
  65. notify NotifyServer
  66. rely RelyService
  67. system SystemService
  68. shares SharesService
  69. connections ConnectionsService
  70. gateway external.ManagementService
  71. storage StorageService
  72. health HealthService
  73. other OtherService
  74. }
  75. func (c *store) Storage() StorageService {
  76. return c.storage
  77. }
  78. func (c *store) Peer() PeerService {
  79. return c.peer
  80. }
  81. func (c *store) Other() OtherService {
  82. return c.other
  83. }
  84. func (c *store) Gateway() external.ManagementService {
  85. return c.gateway
  86. }
  87. func (s *store) Connections() ConnectionsService {
  88. return s.connections
  89. }
  90. func (s *store) Shares() SharesService {
  91. return s.shares
  92. }
  93. func (c *store) Rely() RelyService {
  94. return c.rely
  95. }
  96. func (c *store) System() SystemService {
  97. return c.system
  98. }
  99. func (c *store) Notify() NotifyServer {
  100. return c.notify
  101. }
  102. func (c *store) Casa() CasaService {
  103. return c.casa
  104. }
  105. func (c *store) Health() HealthService {
  106. return c.health
  107. }
  108. func (c *store) MessageBus() *message_bus.ClientWithResponses {
  109. client, _ := message_bus.NewClientWithResponses("", func(c *message_bus.Client) error {
  110. // error will never be returned, as we always want to return a client, even with wrong address,
  111. // in order to avoid panic.
  112. //
  113. // If we don't avoid panic, message bus becomes a hard dependency, which is not what we want.
  114. messageBusAddress, err := external.GetMessageBusAddress(config.CommonInfo.RuntimePath)
  115. if err != nil {
  116. c.Server = "message bus address not found"
  117. return nil
  118. }
  119. c.Server = messageBusAddress
  120. return nil
  121. })
  122. return client
  123. }