Skip to content

Commit

Permalink
feat: better getPlayer middleware setup
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Mar 17, 2020
1 parent 8ec7890 commit da36941
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
64 changes: 33 additions & 31 deletions server/subsonic/api.go
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions server/subsonic/middlewares.go
Expand Up @@ -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,
Expand Down

0 comments on commit da36941

Please sign in to comment.