Make dependency injection more consistent

This commit is contained in:
Deluan 2024-05-08 22:21:38 -04:00
parent a0290587b9
commit 677d9947f3
11 changed files with 54 additions and 63 deletions

View file

@ -14,7 +14,6 @@ import (
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts" "github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/resources" "github.com/navidrome/navidrome/resources"
@ -164,7 +163,7 @@ func startScheduler(ctx context.Context) func() error {
func startPlaybackServer(ctx context.Context) func() error { func startPlaybackServer(ctx context.Context) func() error {
log.Info(ctx, "Starting playback server") log.Info(ctx, "Starting playback server")
playbackInstance := playback.GetInstance() playbackInstance := GetPlaybackServer()
return func() error { return func() error {
return playbackInstance.Run(ctx) return playbackInstance.Run(ctx)

View file

@ -14,6 +14,7 @@ import (
"github.com/navidrome/navidrome/core/agents/listenbrainz" "github.com/navidrome/navidrome/core/agents/listenbrainz"
"github.com/navidrome/navidrome/core/artwork" "github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/ffmpeg" "github.com/navidrome/navidrome/core/ffmpeg"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
"github.com/navidrome/navidrome/db" "github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/persistence" "github.com/navidrome/navidrome/persistence"
@ -23,7 +24,6 @@ import (
"github.com/navidrome/navidrome/server/nativeapi" "github.com/navidrome/navidrome/server/nativeapi"
"github.com/navidrome/navidrome/server/public" "github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic" "github.com/navidrome/navidrome/server/subsonic"
"sync"
) )
// Injectors from wire_injectors.go: // Injectors from wire_injectors.go:
@ -58,11 +58,13 @@ func CreateSubsonicAPIRouter() *subsonic.Router {
share := core.NewShare(dataStore) share := core.NewShare(dataStore)
archiver := core.NewArchiver(mediaStreamer, dataStore, share) archiver := core.NewArchiver(mediaStreamer, dataStore, share)
players := core.NewPlayers(dataStore) players := core.NewPlayers(dataStore)
scanner := GetScanner()
broker := events.GetBroker()
playlists := core.NewPlaylists(dataStore) playlists := core.NewPlaylists(dataStore)
cacheWarmer := artwork.NewCacheWarmer(artworkArtwork, fileCache)
broker := events.GetBroker()
scannerScanner := scanner.GetInstance(dataStore, playlists, cacheWarmer, broker)
playTracker := scrobbler.GetPlayTracker(dataStore, broker) playTracker := scrobbler.GetPlayTracker(dataStore, broker)
router := subsonic.New(dataStore, artworkArtwork, mediaStreamer, archiver, players, externalMetadata, scanner, broker, playlists, playTracker, share) playbackServer := playback.GetInstance(dataStore)
router := subsonic.New(dataStore, artworkArtwork, mediaStreamer, archiver, players, externalMetadata, scannerScanner, broker, playlists, playTracker, share, playbackServer)
return router return router
} }
@ -96,7 +98,7 @@ func CreateListenBrainzRouter() *listenbrainz.Router {
return router return router
} }
func createScanner() scanner.Scanner { func GetScanner() scanner.Scanner {
sqlDB := db.Db() sqlDB := db.Db()
dataStore := persistence.New(sqlDB) dataStore := persistence.New(sqlDB)
playlists := core.NewPlaylists(dataStore) playlists := core.NewPlaylists(dataStore)
@ -107,23 +109,17 @@ func createScanner() scanner.Scanner {
artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata)
cacheWarmer := artwork.NewCacheWarmer(artworkArtwork, fileCache) cacheWarmer := artwork.NewCacheWarmer(artworkArtwork, fileCache)
broker := events.GetBroker() broker := events.GetBroker()
scannerScanner := scanner.New(dataStore, playlists, cacheWarmer, broker) scannerScanner := scanner.GetInstance(dataStore, playlists, cacheWarmer, broker)
return scannerScanner return scannerScanner
} }
func GetPlaybackServer() playback.PlaybackServer {
sqlDB := db.Db()
dataStore := persistence.New(sqlDB)
playbackServer := playback.GetInstance(dataStore)
return playbackServer
}
// wire_injectors.go: // wire_injectors.go:
var allProviders = wire.NewSet(core.Set, artwork.Set, subsonic.New, nativeapi.New, public.New, persistence.New, lastfm.NewRouter, listenbrainz.NewRouter, events.GetBroker, db.Db) var allProviders = wire.NewSet(core.Set, artwork.Set, server.New, subsonic.New, nativeapi.New, public.New, persistence.New, lastfm.NewRouter, listenbrainz.NewRouter, events.GetBroker, scanner.GetInstance, db.Db)
// Scanner must be a Singleton
var (
onceScanner sync.Once
scannerInstance scanner.Scanner
)
func GetScanner() scanner.Scanner {
onceScanner.Do(func() {
scannerInstance = createScanner()
})
return scannerInstance
}

View file

@ -3,13 +3,12 @@
package cmd package cmd
import ( import (
"sync"
"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/core/agents/listenbrainz" "github.com/navidrome/navidrome/core/agents/listenbrainz"
"github.com/navidrome/navidrome/core/artwork" "github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/db" "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"
@ -23,6 +22,7 @@ import (
var allProviders = wire.NewSet( var allProviders = wire.NewSet(
core.Set, core.Set,
artwork.Set, artwork.Set,
server.New,
subsonic.New, subsonic.New,
nativeapi.New, nativeapi.New,
public.New, public.New,
@ -30,12 +30,12 @@ var allProviders = wire.NewSet(
lastfm.NewRouter, lastfm.NewRouter,
listenbrainz.NewRouter, listenbrainz.NewRouter,
events.GetBroker, events.GetBroker,
scanner.GetInstance,
db.Db, db.Db,
) )
func CreateServer(musicFolder string) *server.Server { func CreateServer(musicFolder string) *server.Server {
panic(wire.Build( panic(wire.Build(
server.New,
allProviders, allProviders,
)) ))
} }
@ -49,7 +49,6 @@ func CreateNativeAPIRouter() *nativeapi.Router {
func CreateSubsonicAPIRouter() *subsonic.Router { func CreateSubsonicAPIRouter() *subsonic.Router {
panic(wire.Build( panic(wire.Build(
allProviders, allProviders,
GetScanner,
)) ))
} }
@ -71,22 +70,14 @@ func CreateListenBrainzRouter() *listenbrainz.Router {
)) ))
} }
// Scanner must be a Singleton
var (
onceScanner sync.Once
scannerInstance scanner.Scanner
)
func GetScanner() scanner.Scanner { func GetScanner() scanner.Scanner {
onceScanner.Do(func() {
scannerInstance = createScanner()
})
return scannerInstance
}
func createScanner() scanner.Scanner {
panic(wire.Build( panic(wire.Build(
allProviders, allProviders,
scanner.New, ))
}
func GetPlaybackServer() playback.PlaybackServer {
panic(wire.Build(
allProviders,
)) ))
} }

View file

@ -10,10 +10,8 @@ import (
"fmt" "fmt"
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/utils/singleton" "github.com/navidrome/navidrome/utils/singleton"
) )
@ -31,15 +29,14 @@ type playbackServer struct {
} }
// GetInstance returns the playback-server singleton // GetInstance returns the playback-server singleton
func GetInstance() PlaybackServer { func GetInstance(ds model.DataStore) PlaybackServer {
return singleton.GetInstance(func() *playbackServer { return singleton.GetInstance(func() *playbackServer {
return &playbackServer{} return &playbackServer{datastore: ds}
}) })
} }
// Run starts the playback server which serves request until canceled using the given context // Run starts the playback server which serves request until canceled using the given context
func (ps *playbackServer) Run(ctx context.Context) error { func (ps *playbackServer) Run(ctx context.Context) error {
ps.datastore = persistence.New(db.Db())
devices, err := ps.initDeviceStatus(conf.Server.Jukebox.Devices, conf.Server.Jukebox.Default) devices, err := ps.initDeviceStatus(conf.Server.Jukebox.Devices, conf.Server.Jukebox.Default)
ps.playbackDevices = devices ps.playbackDevices = devices

View file

@ -4,6 +4,7 @@ import (
"github.com/google/wire" "github.com/google/wire"
"github.com/navidrome/navidrome/core/agents" "github.com/navidrome/navidrome/core/agents"
"github.com/navidrome/navidrome/core/ffmpeg" "github.com/navidrome/navidrome/core/ffmpeg"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
) )
@ -18,4 +19,5 @@ var Set = wire.NewSet(
agents.New, agents.New,
ffmpeg.New, ffmpeg.New,
scrobbler.GetPlayTracker, scrobbler.GetPlayTracker,
playback.GetInstance,
) )

View file

@ -13,6 +13,7 @@ import (
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/events" "github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/utils/singleton"
) )
type Scanner interface { type Scanner interface {
@ -57,7 +58,8 @@ type scanStatus struct {
lastUpdate time.Time lastUpdate time.Time
} }
func New(ds model.DataStore, playlists core.Playlists, cacheWarmer artwork.CacheWarmer, broker events.Broker) Scanner { func GetInstance(ds model.DataStore, playlists core.Playlists, cacheWarmer artwork.CacheWarmer, broker events.Broker) Scanner {
return singleton.GetInstance(func() *scanner {
s := &scanner{ s := &scanner{
ds: ds, ds: ds,
pls: playlists, pls: playlists,
@ -69,6 +71,7 @@ func New(ds model.DataStore, playlists core.Playlists, cacheWarmer artwork.Cache
} }
s.loadFolders() s.loadFolders()
return s return s
})
} }
func (s *scanner) rescan(ctx context.Context, mediaFolder string, fullRescan bool) error { func (s *scanner) rescan(ctx context.Context, mediaFolder string, fullRescan bool) error {

View file

@ -25,7 +25,7 @@ var _ = Describe("Album Lists", func() {
BeforeEach(func() { BeforeEach(func() {
ds = &tests.MockDataStore{} ds = &tests.MockDataStore{}
mockRepo = ds.Album(ctx).(*tests.MockAlbumRepo) mockRepo = ds.Album(ctx).(*tests.MockAlbumRepo)
router = New(ds, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil) router = New(ds, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
w = httptest.NewRecorder() w = httptest.NewRecorder()
}) })

View file

@ -12,6 +12,7 @@ import (
"github.com/navidrome/navidrome/conf" "github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core" "github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork" "github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/playback"
"github.com/navidrome/navidrome/core/scrobbler" "github.com/navidrome/navidrome/core/scrobbler"
"github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/model"
@ -39,11 +40,13 @@ type Router struct {
broker events.Broker broker events.Broker
scrobbler scrobbler.PlayTracker scrobbler scrobbler.PlayTracker
share core.Share share core.Share
playback playback.PlaybackServer
} }
func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreamer, archiver core.Archiver, func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreamer, archiver core.Archiver,
players core.Players, externalMetadata core.ExternalMetadata, scanner scanner.Scanner, broker events.Broker, players core.Players, externalMetadata core.ExternalMetadata, scanner scanner.Scanner, broker events.Broker,
playlists core.Playlists, scrobbler scrobbler.PlayTracker, share core.Share) *Router { playlists core.Playlists, scrobbler scrobbler.PlayTracker, share core.Share, playback playback.PlaybackServer,
) *Router {
r := &Router{ r := &Router{
ds: ds, ds: ds,
artwork: artwork, artwork: artwork,
@ -56,6 +59,7 @@ func New(ds model.DataStore, artwork artwork.Artwork, streamer core.MediaStreame
broker: broker, broker: broker,
scrobbler: scrobbler, scrobbler: scrobbler,
share: share, share: share,
playback: playback,
} }
r.Handler = r.routes() r.Handler = r.routes()
return r return r

View file

@ -43,8 +43,7 @@ func (api *Router) JukeboxControl(r *http.Request) (*responses.Subsonic, error)
return nil, err return nil, err
} }
pbServer := playback.GetInstance() pb, err := api.playback.GetDeviceForUser(user.UserName)
pb, err := pbServer.GetDeviceForUser(user.UserName)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -29,7 +29,7 @@ var _ = Describe("MediaAnnotationController", func() {
ds = &tests.MockDataStore{} ds = &tests.MockDataStore{}
playTracker = &fakePlayTracker{} playTracker = &fakePlayTracker{}
eventBroker = &fakeEventBroker{} eventBroker = &fakeEventBroker{}
router = New(ds, nil, nil, nil, nil, nil, nil, eventBroker, nil, playTracker, nil) router = New(ds, nil, nil, nil, nil, nil, nil, eventBroker, nil, playTracker, nil, nil)
}) })
Describe("Scrobble", func() { Describe("Scrobble", func() {

View file

@ -30,7 +30,7 @@ var _ = Describe("MediaRetrievalController", func() {
MockedMediaFile: mockRepo, MockedMediaFile: mockRepo,
} }
artwork = &fakeArtwork{} artwork = &fakeArtwork{}
router = New(ds, artwork, nil, nil, nil, nil, nil, nil, nil, nil, nil) router = New(ds, artwork, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
w = httptest.NewRecorder() w = httptest.NewRecorder()
}) })