mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Inject DB into DataStore, instead of hardcode the dependency
This commit is contained in:
parent
c0fc36da63
commit
344d7a4392
4 changed files with 26 additions and 22 deletions
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/navidrome/navidrome/core/agents/lastfm"
|
"github.com/navidrome/navidrome/core/agents/lastfm"
|
||||||
"github.com/navidrome/navidrome/core/scrobbler"
|
"github.com/navidrome/navidrome/core/scrobbler"
|
||||||
"github.com/navidrome/navidrome/core/transcoder"
|
"github.com/navidrome/navidrome/core/transcoder"
|
||||||
|
"github.com/navidrome/navidrome/db"
|
||||||
"github.com/navidrome/navidrome/persistence"
|
"github.com/navidrome/navidrome/persistence"
|
||||||
"github.com/navidrome/navidrome/scanner"
|
"github.com/navidrome/navidrome/scanner"
|
||||||
"github.com/navidrome/navidrome/server"
|
"github.com/navidrome/navidrome/server"
|
||||||
|
@ -24,13 +25,15 @@ import (
|
||||||
// Injectors from wire_injectors.go:
|
// Injectors from wire_injectors.go:
|
||||||
|
|
||||||
func CreateServer(musicFolder string) *server.Server {
|
func CreateServer(musicFolder string) *server.Server {
|
||||||
dataStore := persistence.New()
|
sqlDB := db.Db()
|
||||||
|
dataStore := persistence.New(sqlDB)
|
||||||
serverServer := server.New(dataStore)
|
serverServer := server.New(dataStore)
|
||||||
return serverServer
|
return serverServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateNativeAPIRouter() *nativeapi.Router {
|
func CreateNativeAPIRouter() *nativeapi.Router {
|
||||||
dataStore := persistence.New()
|
sqlDB := db.Db()
|
||||||
|
dataStore := persistence.New(sqlDB)
|
||||||
broker := events.GetBroker()
|
broker := events.GetBroker()
|
||||||
share := core.NewShare(dataStore)
|
share := core.NewShare(dataStore)
|
||||||
router := nativeapi.New(dataStore, broker, share)
|
router := nativeapi.New(dataStore, broker, share)
|
||||||
|
@ -38,7 +41,8 @@ func CreateNativeAPIRouter() *nativeapi.Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateSubsonicAPIRouter() *subsonic.Router {
|
func CreateSubsonicAPIRouter() *subsonic.Router {
|
||||||
dataStore := persistence.New()
|
sqlDB := db.Db()
|
||||||
|
dataStore := persistence.New(sqlDB)
|
||||||
artworkCache := core.GetImageCache()
|
artworkCache := core.GetImageCache()
|
||||||
artwork := core.NewArtwork(dataStore, artworkCache)
|
artwork := core.NewArtwork(dataStore, artworkCache)
|
||||||
transcoderTranscoder := transcoder.New()
|
transcoderTranscoder := transcoder.New()
|
||||||
|
@ -56,13 +60,15 @@ func CreateSubsonicAPIRouter() *subsonic.Router {
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateLastFMRouter() *lastfm.Router {
|
func CreateLastFMRouter() *lastfm.Router {
|
||||||
dataStore := persistence.New()
|
sqlDB := db.Db()
|
||||||
|
dataStore := persistence.New(sqlDB)
|
||||||
router := lastfm.NewRouter(dataStore)
|
router := lastfm.NewRouter(dataStore)
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
||||||
func createScanner() scanner.Scanner {
|
func createScanner() scanner.Scanner {
|
||||||
dataStore := persistence.New()
|
sqlDB := db.Db()
|
||||||
|
dataStore := persistence.New(sqlDB)
|
||||||
artworkCache := core.GetImageCache()
|
artworkCache := core.GetImageCache()
|
||||||
artwork := core.NewArtwork(dataStore, artworkCache)
|
artwork := core.NewArtwork(dataStore, artworkCache)
|
||||||
cacheWarmer := core.NewCacheWarmer(artwork, artworkCache)
|
cacheWarmer := core.NewCacheWarmer(artwork, artworkCache)
|
||||||
|
@ -73,7 +79,7 @@ func createScanner() scanner.Scanner {
|
||||||
|
|
||||||
// wire_injectors.go:
|
// wire_injectors.go:
|
||||||
|
|
||||||
var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, events.GetBroker)
|
var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, events.GetBroker, db.Db)
|
||||||
|
|
||||||
// Scanner must be a Singleton
|
// Scanner must be a Singleton
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -5,14 +5,14 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/navidrome/navidrome/server/events"
|
|
||||||
|
|
||||||
"github.com/google/wire"
|
"github.com/google/wire"
|
||||||
"github.com/navidrome/navidrome/core"
|
"github.com/navidrome/navidrome/core"
|
||||||
"github.com/navidrome/navidrome/core/agents/lastfm"
|
"github.com/navidrome/navidrome/core/agents/lastfm"
|
||||||
|
"github.com/navidrome/navidrome/db"
|
||||||
"github.com/navidrome/navidrome/persistence"
|
"github.com/navidrome/navidrome/persistence"
|
||||||
"github.com/navidrome/navidrome/scanner"
|
"github.com/navidrome/navidrome/scanner"
|
||||||
"github.com/navidrome/navidrome/server"
|
"github.com/navidrome/navidrome/server"
|
||||||
|
"github.com/navidrome/navidrome/server/events"
|
||||||
"github.com/navidrome/navidrome/server/nativeapi"
|
"github.com/navidrome/navidrome/server/nativeapi"
|
||||||
"github.com/navidrome/navidrome/server/subsonic"
|
"github.com/navidrome/navidrome/server/subsonic"
|
||||||
)
|
)
|
||||||
|
@ -24,6 +24,7 @@ var allProviders = wire.NewSet(
|
||||||
persistence.New,
|
persistence.New,
|
||||||
lastfm.NewRouter,
|
lastfm.NewRouter,
|
||||||
events.GetBroker,
|
events.GetBroker,
|
||||||
|
db.Db,
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateServer(musicFolder string) *server.Server {
|
func CreateServer(musicFolder string) *server.Server {
|
||||||
|
|
15
db/db.go
15
db/db.go
|
@ -4,12 +4,12 @@ import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
|
||||||
|
|
||||||
_ "github.com/mattn/go-sqlite3"
|
_ "github.com/mattn/go-sqlite3"
|
||||||
"github.com/navidrome/navidrome/conf"
|
"github.com/navidrome/navidrome/conf"
|
||||||
_ "github.com/navidrome/navidrome/db/migration"
|
_ "github.com/navidrome/navidrome/db/migration"
|
||||||
"github.com/navidrome/navidrome/log"
|
"github.com/navidrome/navidrome/log"
|
||||||
|
"github.com/navidrome/navidrome/utils/singleton"
|
||||||
"github.com/pressly/goose"
|
"github.com/pressly/goose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,26 +18,21 @@ var (
|
||||||
Path string
|
Path string
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
once sync.Once
|
|
||||||
db *sql.DB
|
|
||||||
)
|
|
||||||
|
|
||||||
func Db() *sql.DB {
|
func Db() *sql.DB {
|
||||||
once.Do(func() {
|
instance := singleton.Get(&sql.DB{}, func() interface{} {
|
||||||
var err error
|
|
||||||
Path = conf.Server.DbPath
|
Path = conf.Server.DbPath
|
||||||
if Path == ":memory:" {
|
if Path == ":memory:" {
|
||||||
Path = "file::memory:?cache=shared&_foreign_keys=on"
|
Path = "file::memory:?cache=shared&_foreign_keys=on"
|
||||||
conf.Server.DbPath = Path
|
conf.Server.DbPath = Path
|
||||||
}
|
}
|
||||||
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
|
log.Debug("Opening DataBase", "dbPath", Path, "driver", Driver)
|
||||||
db, err = sql.Open(Driver, Path)
|
instance, err := sql.Open(Driver, Path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
return instance
|
||||||
})
|
})
|
||||||
return db
|
return instance.(*sql.DB)
|
||||||
}
|
}
|
||||||
|
|
||||||
func EnsureLatestVersion() {
|
func EnsureLatestVersion() {
|
||||||
|
|
|
@ -2,6 +2,7 @@ package persistence
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
"github.com/astaxie/beego/orm"
|
"github.com/astaxie/beego/orm"
|
||||||
|
@ -12,10 +13,11 @@ import (
|
||||||
|
|
||||||
type SQLStore struct {
|
type SQLStore struct {
|
||||||
orm orm.Ormer
|
orm orm.Ormer
|
||||||
|
db *sql.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() model.DataStore {
|
func New(db *sql.DB) model.DataStore {
|
||||||
return &SQLStore{}
|
return &SQLStore{db: db}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
|
func (s *SQLStore) Album(ctx context.Context) model.AlbumRepository {
|
||||||
|
@ -100,7 +102,7 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
|
func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error {
|
||||||
o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
|
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -177,7 +179,7 @@ func (s *SQLStore) GC(ctx context.Context, rootFolder string) error {
|
||||||
|
|
||||||
func (s *SQLStore) getOrmer() orm.Ormer {
|
func (s *SQLStore) getOrmer() orm.Ormer {
|
||||||
if s.orm == nil {
|
if s.orm == nil {
|
||||||
o, err := orm.NewOrmWithDB(db.Driver, "default", db.Db())
|
o, err := orm.NewOrmWithDB(db.Driver, "default", s.db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Error obtaining new orm instance", err)
|
log.Error("Error obtaining new orm instance", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue