Skip to content

🎧 Your Personal Streaming Service

feat: better getPlayer middleware setup

Deluanda3694125201
diff --git a/server/subsonic/api.go b/server/subsonic/api.go
index cea352472..37910a394 100644
--- a/server/subsonic/api.go
+++ b/server/subsonic/api.go
@@ -51,40 +51,39 @@ func (api *Router) routes() http.Handler {
r.Use(postFormToQueryParams)
r.Use(checkRequiredParameters)
- r.Use(getPlayer(api.Players))
-
- // Add validation middleware
r.Use(authenticate(api.Users))
// TODO Validate version
// Subsonic endpoints, grouped by controller
r.Group(func(r chi.Router) {
c := initSystemController(api)
- H(r, "ping", c.Ping)
- H(r, "getLicense", c.GetLicense)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "ping", c.Ping)
+ H(withPlayer, "getLicense", c.GetLicense)
})
r.Group(func(r chi.Router) {
c := initBrowsingController(api)
- H(r, "getMusicFolders", c.GetMusicFolders)
- H(r, "getMusicFolders", c.GetMusicFolders)
- H(r, "getIndexes", c.GetIndexes)
- H(r, "getArtists", c.GetArtists)
- H(r, "getGenres", c.GetGenres)
- H(r, "getMusicDirectory", c.GetMusicDirectory)
- H(r, "getArtist", c.GetArtist)
- H(r, "getAlbum", c.GetAlbum)
- H(r, "getSong", c.GetSong)
- H(r, "getArtistInfo", c.GetArtistInfo)
- H(r, "getArtistInfo2", c.GetArtistInfo2)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "getMusicFolders", c.GetMusicFolders)
+ H(withPlayer, "getIndexes", c.GetIndexes)
+ H(withPlayer, "getArtists", c.GetArtists)
+ H(withPlayer, "getGenres", c.GetGenres)
+ H(withPlayer, "getMusicDirectory", c.GetMusicDirectory)
+ H(withPlayer, "getArtist", c.GetArtist)
+ H(withPlayer, "getAlbum", c.GetAlbum)
+ H(withPlayer, "getSong", c.GetSong)
+ H(withPlayer, "getArtistInfo", c.GetArtistInfo)
+ H(withPlayer, "getArtistInfo2", c.GetArtistInfo2)
})
r.Group(func(r chi.Router) {
c := initAlbumListController(api)
- H(r, "getAlbumList", c.GetAlbumList)
- H(r, "getAlbumList2", c.GetAlbumList2)
- H(r, "getStarred", c.GetStarred)
- H(r, "getStarred2", c.GetStarred2)
- H(r, "getNowPlaying", c.GetNowPlaying)
- H(r, "getRandomSongs", c.GetRandomSongs)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "getAlbumList", c.GetAlbumList)
+ H(withPlayer, "getAlbumList2", c.GetAlbumList2)
+ H(withPlayer, "getStarred", c.GetStarred)
+ H(withPlayer, "getStarred2", c.GetStarred2)
+ H(withPlayer, "getNowPlaying", c.GetNowPlaying)
+ H(withPlayer, "getRandomSongs", c.GetRandomSongs)
})
r.Group(func(r chi.Router) {
c := initMediaAnnotationController(api)
@@ -95,16 +94,18 @@ func (api *Router) routes() http.Handler {
})
r.Group(func(r chi.Router) {
c := initPlaylistsController(api)
- H(r, "getPlaylists", c.GetPlaylists)
- H(r, "getPlaylist", c.GetPlaylist)
- H(r, "createPlaylist", c.CreatePlaylist)
- H(r, "deletePlaylist", c.DeletePlaylist)
- H(r, "updatePlaylist", c.UpdatePlaylist)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "getPlaylists", c.GetPlaylists)
+ H(withPlayer, "getPlaylist", c.GetPlaylist)
+ H(withPlayer, "createPlaylist", c.CreatePlaylist)
+ H(withPlayer, "deletePlaylist", c.DeletePlaylist)
+ H(withPlayer, "updatePlaylist", c.UpdatePlaylist)
})
r.Group(func(r chi.Router) {
c := initSearchingController(api)
- H(r, "search2", c.Search2)
- H(r, "search3", c.Search3)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "search2", c.Search2)
+ H(withPlayer, "search3", c.Search3)
})
r.Group(func(r chi.Router) {
c := initUsersController(api)
@@ -117,8 +118,9 @@ func (api *Router) routes() http.Handler {
})
r.Group(func(r chi.Router) {
c := initStreamController(api)
- H(r, "stream", c.Stream)
- H(r, "download", c.Download)
+ withPlayer := r.With(getPlayer(api.Players))
+ H(withPlayer, "stream", c.Stream)
+ H(withPlayer, "download", c.Download)
})
// Deprecated/Out of scope endpoints
diff --git a/server/subsonic/middlewares.go b/server/subsonic/middlewares.go
index dc8470894..aa03b5472 100644
--- a/server/subsonic/middlewares.go
+++ b/server/subsonic/middlewares.go
@@ -106,11 +106,11 @@ func getPlayer(players engine.Players) func(next http.Handler) http.Handler {
player, err := players.Register(ctx, playerId, client, r.Header.Get("user-agent"), ip)
if err != nil {
log.Error("Could not register player", "userName", userName, "client", client)
+ } else {
+ ctx = context.WithValue(ctx, "player", *player)
+ r = r.WithContext(ctx)
}
- ctx = context.WithValue(ctx, "player", *player)
- r = r.WithContext(ctx)
-
cookie := &http.Cookie{
Name: playerIDCookieName(userName),
Value: player.ID,