CasaOS/service/service.go

148 lines
3.2 KiB
Go
Raw Permalink Normal View History

/*
* @Author: LinkLeong link@icewhale.com
* @Date: 2022-07-12 09:48:56
* @LastEditors: LinkLeong
* @LastEditTime: 2022-09-02 22:10:05
* @FilePath: /CasaOS/service/service.go
* @Description:
* @Website: https://www.casaos.io
* Copyright (c) 2022 by icewhale, All Rights Reserved.
*/
2021-09-26 02:35:02 +00:00
package service
import (
"github.com/IceWhaleTech/CasaOS-Common/external"
"github.com/IceWhaleTech/CasaOS/codegen/message_bus"
"github.com/IceWhaleTech/CasaOS/pkg/config"
2022-02-17 10:43:25 +00:00
"github.com/gorilla/websocket"
"github.com/patrickmn/go-cache"
2021-09-26 02:35:02 +00:00
"gorm.io/gorm"
)
var Cache *cache.Cache
var (
MyService Repository
)
var (
WebSocketConns []*websocket.Conn
SocketRun bool
)
2022-02-17 10:43:25 +00:00
2021-09-26 02:35:02 +00:00
type Repository interface {
Casa() CasaService
Connections() ConnectionsService
Gateway() external.ManagementService
Health() HealthService
2021-09-26 02:35:02 +00:00
Notify() NotifyServer
Rely() RelyService
Shares() SharesService
System() SystemService
2023-03-17 07:37:28 +00:00
Storage() StorageService
MessageBus() *message_bus.ClientWithResponses
2023-03-13 01:42:18 +00:00
Peer() PeerService
Other() OtherService
2021-09-26 02:35:02 +00:00
}
func NewService(db *gorm.DB, RuntimePath string) Repository {
gatewayManagement, err := external.NewManagementService(RuntimePath)
if err != nil && len(RuntimePath) > 0 {
panic(err)
}
2021-09-26 02:35:02 +00:00
return &store{
2023-03-06 08:10:19 +00:00
casa: NewCasaService(),
connections: NewConnectionsService(db),
gateway: gatewayManagement,
notify: NewNotifyService(db),
rely: NewRelyService(db),
system: NewSystemService(),
health: NewHealthService(),
shares: NewSharesService(db),
2023-03-17 07:37:28 +00:00
storage: NewStorageService(),
other: NewOtherService(),
2023-03-06 08:10:19 +00:00
2023-03-17 07:37:28 +00:00
peer: NewPeerService(db),
2021-09-26 02:35:02 +00:00
}
}
type store struct {
2023-03-13 01:42:18 +00:00
peer PeerService
2023-03-06 08:10:19 +00:00
db *gorm.DB
casa CasaService
notify NotifyServer
rely RelyService
system SystemService
shares SharesService
connections ConnectionsService
gateway external.ManagementService
2023-03-17 07:37:28 +00:00
storage StorageService
health HealthService
other OtherService
}
2023-03-06 08:10:19 +00:00
2023-03-17 07:37:28 +00:00
func (c *store) Storage() StorageService {
return c.storage
}
2023-03-13 01:42:18 +00:00
func (c *store) Peer() PeerService {
return c.peer
}
func (c *store) Other() OtherService {
return c.other
2023-01-16 05:54:44 +00:00
}
func (c *store) Gateway() external.ManagementService {
return c.gateway
}
func (s *store) Connections() ConnectionsService {
return s.connections
}
func (s *store) Shares() SharesService {
return s.shares
2021-09-26 02:35:02 +00:00
}
func (c *store) Rely() RelyService {
return c.rely
}
2021-09-26 02:35:02 +00:00
func (c *store) System() SystemService {
return c.system
}
func (c *store) Notify() NotifyServer {
2021-09-26 02:35:02 +00:00
return c.notify
}
func (c *store) Casa() CasaService {
return c.casa
2021-09-26 02:35:02 +00:00
}
func (c *store) Health() HealthService {
return c.health
}
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
}