100 lines
1.8 KiB
Go
100 lines
1.8 KiB
Go
package node
|
|
|
|
import (
|
|
"path/filepath"
|
|
|
|
"mediarr/acl"
|
|
"mediarr/auth"
|
|
"mediarr/cluster"
|
|
"mediarr/database"
|
|
"mediarr/ident"
|
|
"mediarr/identity"
|
|
"mediarr/storage"
|
|
)
|
|
|
|
const defaultStorageBackendID = "00000000-0000-7000-8000-000000000201"
|
|
|
|
type Node struct {
|
|
db database.DatabaseClient
|
|
clusterKeyPath string
|
|
cluster cluster.Identity
|
|
storagePath string
|
|
store storage.Store
|
|
backendID ident.UUID
|
|
}
|
|
|
|
type NewNodeParams struct {
|
|
DatabaseName string
|
|
ClusterKeyPath string
|
|
StoragePath string
|
|
}
|
|
|
|
func NewNode(params NewNodeParams) (error, Node) {
|
|
|
|
storagePath := params.StoragePath
|
|
if storagePath == "" {
|
|
storagePath = "data"
|
|
}
|
|
|
|
absoluteStoragePath, err := filepath.Abs(storagePath)
|
|
if err != nil {
|
|
return err, Node{}
|
|
}
|
|
|
|
store, err := storage.NewLocal(absoluteStoragePath)
|
|
if err != nil {
|
|
return err, Node{}
|
|
}
|
|
|
|
backendID, err := ident.Parse(defaultStorageBackendID)
|
|
if err != nil {
|
|
return err, Node{}
|
|
}
|
|
|
|
var databaseName *string
|
|
if params.DatabaseName != "" {
|
|
databaseName = ¶ms.DatabaseName
|
|
}
|
|
|
|
err, db := database.NewDatabase(database.NewDatabaseParams{
|
|
DatabaseName: databaseName,
|
|
})
|
|
|
|
if err != nil {
|
|
return err, Node{}
|
|
}
|
|
|
|
clusterKeyPath := params.ClusterKeyPath
|
|
if clusterKeyPath == "" {
|
|
clusterKeyPath = "cluster.key"
|
|
}
|
|
|
|
return nil, Node{
|
|
db: *db,
|
|
clusterKeyPath: clusterKeyPath,
|
|
storagePath: absoluteStoragePath,
|
|
store: store,
|
|
backendID: backendID,
|
|
}
|
|
}
|
|
|
|
func (n *Node) Close() error {
|
|
return n.db.Close()
|
|
}
|
|
|
|
func (n *Node) ClusterIdentity() cluster.Identity {
|
|
return n.cluster
|
|
}
|
|
|
|
func (n *Node) Identity() *identity.Service {
|
|
return identity.NewService(n.db)
|
|
}
|
|
|
|
func (n *Node) Auth() *auth.Service {
|
|
return auth.NewService(n.db)
|
|
}
|
|
|
|
func (n *Node) ACL() *acl.Service {
|
|
return acl.NewService(n.db)
|
|
}
|