/
service.go
111 lines (91 loc) · 2.76 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package service
import (
"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/IceWhaleTech/CasaOS-LocalStorage/codegen/message_bus"
"github.com/IceWhaleTech/CasaOS-LocalStorage/pkg/config"
v2 "github.com/IceWhaleTech/CasaOS-LocalStorage/service/v2"
"github.com/IceWhaleTech/CasaOS-LocalStorage/service/v2/wrapper"
"github.com/patrickmn/go-cache"
"gorm.io/gorm"
)
var Cache *cache.Cache
var MyService Services
type Services interface {
Disk() DiskService
USB() USBService
LocalStorage() *v2.LocalStorageService
Gateway() external.ManagementService
Notify() NotifyServer
NotifySystem() external.NotifyService
Shares() external.ShareService
MessageBus() *message_bus.ClientWithResponses
Storage() StorageService
}
func NewService(db *gorm.DB) Services {
gatewayManagement, err := external.NewManagementService(config.CommonInfo.RuntimePath)
if err != nil {
panic(err)
}
notifySystem := external.NewNotifyService(config.CommonInfo.RuntimePath)
sharesService := external.NewShareService(config.CommonInfo.RuntimePath)
return &store{
usb: NewUSBService(),
disk: NewDiskService(db),
localStorage: v2.NewLocalStorageService(db, wrapper.NewMountInfo()),
gateway: gatewayManagement,
notify: NewNotifyService(),
notifySystem: notifySystem,
shares: sharesService,
storage: NewStorageService(),
}
}
type store struct {
usb USBService
disk DiskService
localStorage *v2.LocalStorageService
gateway external.ManagementService
notify NotifyServer
notifySystem external.NotifyService
shares external.ShareService
storage StorageService
}
func (c *store) NotifySystem() external.NotifyService {
return c.notifySystem
}
func (c *store) Storage() StorageService {
return c.storage
}
func (c *store) Gateway() external.ManagementService {
return c.gateway
}
func (c *store) USB() USBService {
return c.usb
}
func (c *store) Disk() DiskService {
return c.disk
}
func (c *store) LocalStorage() *v2.LocalStorageService {
return c.localStorage
}
func (c *store) Notify() NotifyServer {
return c.notify
}
func (c *store) Shares() external.ShareService {
return c.shares
}
func (c *store) MessageBus() *message_bus.ClientWithResponses {
client, _ := message_bus.NewClientWithResponses("", func(c *message_bus.Client) error {
// error will never be returned, as we always want to return a client, even with wrong address,
// in order to avoid panic.
//
// If we don't avoid panic, message bus becomes a hard dependency, which is not what we want.
messageBusAddress, err := external.GetMessageBusAddress(config.CommonInfo.RuntimePath)
if err != nil {
c.Server = "message bus address not found"
return nil
}
c.Server = messageBusAddress
return nil
})
return client
}