mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 04:57:37 +03:00
Move API dependency injection to main package, opening the possibility to switch persistence without code changes
This commit is contained in:
parent
52f682debf
commit
30ebbc1fa1
6 changed files with 92 additions and 72 deletions
41
api/api.go
41
api/api.go
|
@ -8,6 +8,8 @@ import (
|
|||
|
||||
"github.com/cloudsonic/sonic-server/api/responses"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
"github.com/cloudsonic/sonic-server/domain"
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
|
@ -15,7 +17,26 @@ const ApiVersion = "1.8.0"
|
|||
|
||||
type SubsonicHandler = func(http.ResponseWriter, *http.Request) (*responses.Subsonic, error)
|
||||
|
||||
func Router() http.Handler {
|
||||
type Router struct {
|
||||
Browser engine.Browser
|
||||
Cover engine.Cover
|
||||
ListGenerator engine.ListGenerator
|
||||
Playlists engine.Playlists
|
||||
Ratings engine.Ratings
|
||||
Scrobbler engine.Scrobbler
|
||||
Search engine.Search
|
||||
MediaFileRepository domain.MediaFileRepository
|
||||
}
|
||||
|
||||
func NewRouter(browser engine.Browser, cover engine.Cover, listGenerator engine.ListGenerator,
|
||||
playlists engine.Playlists, ratings engine.Ratings, scrobbler engine.Scrobbler, search engine.Search,
|
||||
mediaFileRepository domain.MediaFileRepository) *Router {
|
||||
|
||||
return &Router{Browser: browser, Cover: cover, ListGenerator: listGenerator, Playlists: playlists,
|
||||
Ratings: ratings, Scrobbler: scrobbler, Search: search, MediaFileRepository: mediaFileRepository}
|
||||
}
|
||||
|
||||
func (api *Router) Routes() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
|
||||
r.Use(checkRequiredParameters)
|
||||
|
@ -27,12 +48,12 @@ func Router() http.Handler {
|
|||
}
|
||||
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initSystemController()
|
||||
c := initSystemController(api)
|
||||
addEndpoint(r, "ping", c.Ping)
|
||||
addEndpoint(r, "getLicense", c.GetLicense)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initBrowsingController()
|
||||
c := initBrowsingController(api)
|
||||
addEndpoint(r, "getMusicFolders", c.GetMusicFolders)
|
||||
addEndpoint(r, "getMusicFolders", c.GetMusicFolders)
|
||||
addEndpoint(r, "getIndexes", c.GetIndexes)
|
||||
|
@ -44,7 +65,7 @@ func Router() http.Handler {
|
|||
addEndpoint(reqParams, "getSong", c.GetSong)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initAlbumListController()
|
||||
c := initAlbumListController(api)
|
||||
addEndpoint(r, "getAlbumList", c.GetAlbumList)
|
||||
addEndpoint(r, "getAlbumList2", c.GetAlbumList2)
|
||||
addEndpoint(r, "getStarred", c.GetStarred)
|
||||
|
@ -53,14 +74,14 @@ func Router() http.Handler {
|
|||
addEndpoint(r, "getRandomSongs", c.GetRandomSongs)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initMediaAnnotationController()
|
||||
c := initMediaAnnotationController(api)
|
||||
addEndpoint(r, "setRating", c.SetRating)
|
||||
addEndpoint(r, "star", c.Star)
|
||||
addEndpoint(r, "unstar", c.Unstar)
|
||||
addEndpoint(r, "scrobble", c.Scrobble)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initPlaylistsController()
|
||||
c := initPlaylistsController(api)
|
||||
addEndpoint(r, "getPlaylists", c.GetPlaylists)
|
||||
addEndpoint(r, "getPlaylist", c.GetPlaylist)
|
||||
addEndpoint(r, "createPlaylist", c.CreatePlaylist)
|
||||
|
@ -68,21 +89,21 @@ func Router() http.Handler {
|
|||
addEndpoint(r, "updatePlaylist", c.UpdatePlaylist)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initSearchingController()
|
||||
c := initSearchingController(api)
|
||||
addEndpoint(r, "search2", c.Search2)
|
||||
addEndpoint(r, "search3", c.Search3)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initUsersController()
|
||||
c := initUsersController(api)
|
||||
addEndpoint(r, "getUser", c.GetUser)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initMediaRetrievalController()
|
||||
c := initMediaRetrievalController(api)
|
||||
addEndpoint(r, "getAvatar", c.GetAvatar)
|
||||
addEndpoint(r, "getCoverArt", c.GetCoverArt)
|
||||
})
|
||||
r.Group(func(r chi.Router) {
|
||||
c := initStreamController()
|
||||
c := initStreamController(api)
|
||||
addEndpoint(r, "stream", c.Stream)
|
||||
addEndpoint(r, "download", c.Download)
|
||||
})
|
||||
|
|
|
@ -6,11 +6,8 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/cloudsonic/sonic-server/itunesbridge"
|
||||
"github.com/cloudsonic/sonic-server/persistence"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_storm"
|
||||
"github.com/deluan/gomate"
|
||||
"github.com/deluan/gomate/ledis"
|
||||
"github.com/google/wire"
|
||||
|
@ -18,85 +15,62 @@ import (
|
|||
|
||||
// Injectors from wire_injectors.go:
|
||||
|
||||
func initSystemController() *SystemController {
|
||||
func initSystemController(router *Router) *SystemController {
|
||||
systemController := NewSystemController()
|
||||
return systemController
|
||||
}
|
||||
|
||||
func initBrowsingController() *BrowsingController {
|
||||
propertyRepository := db_storm.NewPropertyRepository()
|
||||
mediaFolderRepository := persistence.NewMediaFolderRepository()
|
||||
artistIndexRepository := db_storm.NewArtistIndexRepository()
|
||||
artistRepository := db_storm.NewArtistRepository()
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
browser := engine.NewBrowser(propertyRepository, mediaFolderRepository, artistIndexRepository, artistRepository, albumRepository, mediaFileRepository)
|
||||
func initBrowsingController(router *Router) *BrowsingController {
|
||||
browser := router.Browser
|
||||
browsingController := NewBrowsingController(browser)
|
||||
return browsingController
|
||||
}
|
||||
|
||||
func initAlbumListController() *AlbumListController {
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
nowPlayingRepository := persistence.NewNowPlayingRepository()
|
||||
listGenerator := engine.NewListGenerator(albumRepository, mediaFileRepository, nowPlayingRepository)
|
||||
func initAlbumListController(router *Router) *AlbumListController {
|
||||
listGenerator := router.ListGenerator
|
||||
albumListController := NewAlbumListController(listGenerator)
|
||||
return albumListController
|
||||
}
|
||||
|
||||
func initMediaAnnotationController() *MediaAnnotationController {
|
||||
itunesControl := itunesbridge.NewItunesControl()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
nowPlayingRepository := persistence.NewNowPlayingRepository()
|
||||
scrobbler := engine.NewScrobbler(itunesControl, mediaFileRepository, nowPlayingRepository)
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
artistRepository := db_storm.NewArtistRepository()
|
||||
ratings := engine.NewRatings(itunesControl, mediaFileRepository, albumRepository, artistRepository)
|
||||
func initMediaAnnotationController(router *Router) *MediaAnnotationController {
|
||||
scrobbler := router.Scrobbler
|
||||
ratings := router.Ratings
|
||||
mediaAnnotationController := NewMediaAnnotationController(scrobbler, ratings)
|
||||
return mediaAnnotationController
|
||||
}
|
||||
|
||||
func initPlaylistsController() *PlaylistsController {
|
||||
itunesControl := itunesbridge.NewItunesControl()
|
||||
playlistRepository := db_storm.NewPlaylistRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
playlists := engine.NewPlaylists(itunesControl, playlistRepository, mediaFileRepository)
|
||||
func initPlaylistsController(router *Router) *PlaylistsController {
|
||||
playlists := router.Playlists
|
||||
playlistsController := NewPlaylistsController(playlists)
|
||||
return playlistsController
|
||||
}
|
||||
|
||||
func initSearchingController() *SearchingController {
|
||||
artistRepository := db_storm.NewArtistRepository()
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
db := newDB()
|
||||
search := engine.NewSearch(artistRepository, albumRepository, mediaFileRepository, db)
|
||||
func initSearchingController(router *Router) *SearchingController {
|
||||
search := router.Search
|
||||
searchingController := NewSearchingController(search)
|
||||
return searchingController
|
||||
}
|
||||
|
||||
func initUsersController() *UsersController {
|
||||
func initUsersController(router *Router) *UsersController {
|
||||
usersController := NewUsersController()
|
||||
return usersController
|
||||
}
|
||||
|
||||
func initMediaRetrievalController() *MediaRetrievalController {
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
cover := engine.NewCover(mediaFileRepository, albumRepository)
|
||||
func initMediaRetrievalController(router *Router) *MediaRetrievalController {
|
||||
cover := router.Cover
|
||||
mediaRetrievalController := NewMediaRetrievalController(cover)
|
||||
return mediaRetrievalController
|
||||
}
|
||||
|
||||
func initStreamController() *StreamController {
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
func initStreamController(router *Router) *StreamController {
|
||||
mediaFileRepository := router.MediaFileRepository
|
||||
streamController := NewStreamController(mediaFileRepository)
|
||||
return streamController
|
||||
}
|
||||
|
||||
// wire_injectors.go:
|
||||
|
||||
var allProviders = wire.NewSet(itunesbridge.NewItunesControl, db_storm.Set, engine.Set, NewSystemController,
|
||||
var allProviders = wire.NewSet(itunesbridge.NewItunesControl, NewSystemController,
|
||||
NewBrowsingController,
|
||||
NewAlbumListController,
|
||||
NewMediaAnnotationController,
|
||||
|
@ -105,7 +79,7 @@ var allProviders = wire.NewSet(itunesbridge.NewItunesControl, db_storm.Set, engi
|
|||
NewUsersController,
|
||||
NewMediaRetrievalController,
|
||||
NewStreamController,
|
||||
newDB,
|
||||
newDB, wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search", "MediaFileRepository"),
|
||||
)
|
||||
|
||||
func newDB() gomate.DB {
|
||||
|
|
|
@ -3,10 +3,8 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/cloudsonic/sonic-server/itunesbridge"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_storm"
|
||||
"github.com/deluan/gomate"
|
||||
"github.com/deluan/gomate/ledis"
|
||||
"github.com/google/wire"
|
||||
|
@ -14,9 +12,6 @@ import (
|
|||
|
||||
var allProviders = wire.NewSet(
|
||||
itunesbridge.NewItunesControl,
|
||||
//db_ledis.Set,
|
||||
db_storm.Set,
|
||||
engine.Set,
|
||||
NewSystemController,
|
||||
NewBrowsingController,
|
||||
NewAlbumListController,
|
||||
|
@ -27,41 +22,42 @@ var allProviders = wire.NewSet(
|
|||
NewMediaRetrievalController,
|
||||
NewStreamController,
|
||||
newDB,
|
||||
wire.FieldsOf(new(*Router), "Browser", "Cover", "ListGenerator", "Playlists", "Ratings", "Scrobbler", "Search", "MediaFileRepository"),
|
||||
)
|
||||
|
||||
func initSystemController() *SystemController {
|
||||
func initSystemController(router *Router) *SystemController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initBrowsingController() *BrowsingController {
|
||||
func initBrowsingController(router *Router) *BrowsingController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initAlbumListController() *AlbumListController {
|
||||
func initAlbumListController(router *Router) *AlbumListController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initMediaAnnotationController() *MediaAnnotationController {
|
||||
func initMediaAnnotationController(router *Router) *MediaAnnotationController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initPlaylistsController() *PlaylistsController {
|
||||
func initPlaylistsController(router *Router) *PlaylistsController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initSearchingController() *SearchingController {
|
||||
func initSearchingController(router *Router) *SearchingController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initUsersController() *UsersController {
|
||||
func initUsersController(router *Router) *UsersController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initMediaRetrievalController() *MediaRetrievalController {
|
||||
func initMediaRetrievalController(router *Router) *MediaRetrievalController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initStreamController() *StreamController {
|
||||
func initStreamController(router *Router) *StreamController {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
|
|
3
main.go
3
main.go
|
@ -3,7 +3,6 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cloudsonic/sonic-server/api"
|
||||
"github.com/cloudsonic/sonic-server/conf"
|
||||
)
|
||||
|
||||
|
@ -14,6 +13,6 @@ func main() {
|
|||
|
||||
a := App{}
|
||||
a.Initialize()
|
||||
a.MountRouter("/rest/", api.Router())
|
||||
a.MountRouter("/rest/", initRouter().Routes())
|
||||
a.Run(":" + conf.Sonic.Port)
|
||||
}
|
||||
|
|
26
wire_gen.go
26
wire_gen.go
|
@ -6,8 +6,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/api"
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/cloudsonic/sonic-server/itunesbridge"
|
||||
"github.com/cloudsonic/sonic-server/persistence"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_storm"
|
||||
"github.com/cloudsonic/sonic-server/scanner"
|
||||
|
@ -18,6 +20,28 @@ import (
|
|||
|
||||
// Injectors from wire_injectors.go:
|
||||
|
||||
func initRouter() *api.Router {
|
||||
propertyRepository := db_storm.NewPropertyRepository()
|
||||
mediaFolderRepository := persistence.NewMediaFolderRepository()
|
||||
artistIndexRepository := db_storm.NewArtistIndexRepository()
|
||||
artistRepository := db_storm.NewArtistRepository()
|
||||
albumRepository := db_storm.NewAlbumRepository()
|
||||
mediaFileRepository := db_storm.NewMediaFileRepository()
|
||||
browser := engine.NewBrowser(propertyRepository, mediaFolderRepository, artistIndexRepository, artistRepository, albumRepository, mediaFileRepository)
|
||||
cover := engine.NewCover(mediaFileRepository, albumRepository)
|
||||
nowPlayingRepository := persistence.NewNowPlayingRepository()
|
||||
listGenerator := engine.NewListGenerator(albumRepository, mediaFileRepository, nowPlayingRepository)
|
||||
itunesControl := itunesbridge.NewItunesControl()
|
||||
playlistRepository := db_storm.NewPlaylistRepository()
|
||||
playlists := engine.NewPlaylists(itunesControl, playlistRepository, mediaFileRepository)
|
||||
ratings := engine.NewRatings(itunesControl, mediaFileRepository, albumRepository, artistRepository)
|
||||
scrobbler := engine.NewScrobbler(itunesControl, mediaFileRepository, nowPlayingRepository)
|
||||
db := newDB()
|
||||
search := engine.NewSearch(artistRepository, albumRepository, mediaFileRepository, db)
|
||||
router := api.NewRouter(browser, cover, listGenerator, playlists, ratings, scrobbler, search, mediaFileRepository)
|
||||
return router
|
||||
}
|
||||
|
||||
func initImporter(musicFolder string) *scanner.Importer {
|
||||
checkSumRepository := db_storm.NewCheckSumRepository()
|
||||
itunesScanner := scanner.NewItunesScanner(checkSumRepository)
|
||||
|
@ -35,7 +59,7 @@ func initImporter(musicFolder string) *scanner.Importer {
|
|||
|
||||
// wire_injectors.go:
|
||||
|
||||
var allProviders = wire.NewSet(itunesbridge.NewItunesControl, db_storm.Set, engine.Set, scanner.Set, newDB)
|
||||
var allProviders = wire.NewSet(itunesbridge.NewItunesControl, db_storm.Set, engine.Set, scanner.Set, newDB, api.NewRouter)
|
||||
|
||||
func newDB() gomate.DB {
|
||||
return ledis.NewEmbeddedDB(db_ledis.Db())
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/cloudsonic/sonic-server/api"
|
||||
"github.com/cloudsonic/sonic-server/engine"
|
||||
"github.com/cloudsonic/sonic-server/itunesbridge"
|
||||
"github.com/cloudsonic/sonic-server/persistence/db_ledis"
|
||||
|
@ -20,8 +21,13 @@ var allProviders = wire.NewSet(
|
|||
engine.Set,
|
||||
scanner.Set,
|
||||
newDB,
|
||||
api.NewRouter,
|
||||
)
|
||||
|
||||
func initRouter() *api.Router {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
||||
func initImporter(musicFolder string) *scanner.Importer {
|
||||
panic(wire.Build(allProviders))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue