navidrome/server/subsonic/api.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

244 lines
7.6 KiB
Go
Raw Normal View History

package subsonic
import (
"encoding/json"
"encoding/xml"
"fmt"
"net/http"
"runtime"
2020-04-21 14:41:04 +02:00
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/core"
2020-02-02 02:07:15 +01:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/scanner"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/server/subsonic/responses"
"github.com/deluan/navidrome/utils"
"github.com/go-chi/chi"
2020-04-21 14:41:04 +02:00
"github.com/go-chi/chi/middleware"
)
2020-11-01 23:04:53 +01:00
const Version = "1.16.1"
2020-10-27 20:23:29 +01:00
type handler = func(http.ResponseWriter, *http.Request) (*responses.Subsonic, error)
type Router struct {
DataStore model.DataStore
2020-10-27 15:19:58 +01:00
Artwork core.Artwork
Streamer core.MediaStreamer
Archiver core.Archiver
2020-10-27 16:01:40 +01:00
Players core.Players
2020-10-27 15:19:58 +01:00
ExternalInfo core.ExternalInfo
Scanner scanner.Scanner
2020-01-11 19:21:43 +01:00
mux http.Handler
}
func New(ds model.DataStore, artwork core.Artwork, streamer core.MediaStreamer, archiver core.Archiver, players core.Players,
externalInfo core.ExternalInfo, scanner scanner.Scanner) *Router {
r := &Router{
DataStore: ds,
Artwork: artwork,
Streamer: streamer,
Archiver: archiver,
Players: players,
ExternalInfo: externalInfo,
Scanner: scanner,
}
2020-01-11 19:21:43 +01:00
r.mux = r.routes()
return r
}
2020-04-03 23:50:42 +02:00
func (api *Router) Setup(path string) {}
2020-01-11 19:21:43 +01:00
func (api *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
api.mux.ServeHTTP(w, r)
}
2020-01-11 19:21:43 +01:00
func (api *Router) routes() http.Handler {
r := chi.NewRouter()
r.Use(postFormToQueryParams)
2020-01-09 16:35:00 +01:00
r.Use(checkRequiredParameters)
2020-08-14 16:10:17 +02:00
r.Use(authenticate(api.DataStore))
// TODO Validate version
// Subsonic endpoints, grouped by controller
r.Group(func(r chi.Router) {
c := initSystemController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
h(withPlayer, "ping", c.Ping)
h(withPlayer, "getLicense", c.GetLicense)
})
r.Group(func(r chi.Router) {
c := initBrowsingController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
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)
h(withPlayer, "getTopSongs", c.GetTopSongs)
h(withPlayer, "getSimilarSongs", c.GetSimilarSongs)
h(withPlayer, "getSimilarSongs2", c.GetSimilarSongs2)
})
r.Group(func(r chi.Router) {
c := initAlbumListController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
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)
h(withPlayer, "getSongsByGenre", c.GetSongsByGenre)
})
r.Group(func(r chi.Router) {
c := initMediaAnnotationController(api)
2020-10-27 20:23:29 +01:00
h(r, "setRating", c.SetRating)
h(r, "star", c.Star)
h(r, "unstar", c.Unstar)
h(r, "scrobble", c.Scrobble)
})
r.Group(func(r chi.Router) {
c := initPlaylistsController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
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 := initBookmarksController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
h(withPlayer, "getBookmarks", c.GetBookmarks)
h(withPlayer, "createBookmark", c.CreateBookmark)
h(withPlayer, "deleteBookmark", c.DeleteBookmark)
h(withPlayer, "getPlayQueue", c.GetPlayQueue)
h(withPlayer, "savePlayQueue", c.SavePlayQueue)
})
r.Group(func(r chi.Router) {
c := initSearchingController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
h(withPlayer, "search2", c.Search2)
h(withPlayer, "search3", c.Search3)
})
r.Group(func(r chi.Router) {
c := initUsersController(api)
2020-10-27 20:23:29 +01:00
h(r, "getUser", c.GetUser)
h(r, "getUsers", c.GetUsers)
})
r.Group(func(r chi.Router) {
c := initLibraryScanningController(api)
h(r, "getScanStatus", c.GetScanStatus)
h(r, "startScan", c.StartScan)
})
r.Group(func(r chi.Router) {
c := initMediaRetrievalController(api)
// configure request throttling
2020-04-21 14:41:04 +02:00
maxRequests := utils.MaxInt(2, runtime.NumCPU())
withThrottle := r.With(middleware.ThrottleBacklog(maxRequests, consts.RequestThrottleBacklogLimit, consts.RequestThrottleBacklogTimeout))
2020-10-27 20:23:29 +01:00
h(withThrottle, "getAvatar", c.GetAvatar)
h(withThrottle, "getCoverArt", c.GetCoverArt)
})
r.Group(func(r chi.Router) {
c := initStreamController(api)
withPlayer := r.With(getPlayer(api.Players))
2020-10-27 20:23:29 +01:00
h(withPlayer, "stream", c.Stream)
h(withPlayer, "download", c.Download)
})
// Deprecated/Out of scope endpoints
2020-10-27 20:23:29 +01:00
h410(r, "getChatMessages")
h410(r, "addChatMessage")
h410(r, "getVideos")
h410(r, "getVideoInfo")
h410(r, "getCaptions")
return r
}
// Add the Subsonic handler, with and without `.view` extension
// Ex: if path = `ping` it will create the routes `/ping` and `/ping.view`
2020-10-27 20:23:29 +01:00
func h(r chi.Router, path string, f handler) {
handle := func(w http.ResponseWriter, r *http.Request) {
res, err := f(w, r)
if err != nil {
2020-10-27 20:23:29 +01:00
// If it is not a Subsonic error, convert it to an ErrorGeneric
if _, ok := err.(subError); !ok {
2020-11-13 20:57:49 +01:00
if err == model.ErrNotFound {
err = newError(responses.ErrorDataNotFound, "data not found")
} else {
err = newError(responses.ErrorGeneric, "Internal Error")
}
2020-10-27 20:23:29 +01:00
}
sendError(w, r, err)
return
}
if res != nil {
2020-10-27 20:23:29 +01:00
sendResponse(w, r, res)
}
}
r.HandleFunc("/"+path, handle)
r.HandleFunc("/"+path+".view", handle)
}
// Add a handler that returns 410 - Gone. Used to signal that an endpoint will not be implemented
2020-10-27 20:23:29 +01:00
func h410(r chi.Router, path string) {
handle := func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(410)
2020-04-26 18:35:26 +02:00
_, _ = w.Write([]byte("This endpoint will not be implemented"))
}
r.HandleFunc("/"+path, handle)
r.HandleFunc("/"+path+".view", handle)
}
2020-10-27 20:23:29 +01:00
func sendError(w http.ResponseWriter, r *http.Request, err error) {
response := newResponse()
code := responses.ErrorGeneric
2020-10-27 20:23:29 +01:00
if e, ok := err.(subError); ok {
code = e.code
}
response.Status = "fail"
response.Error = &responses.Error{Code: code, Message: err.Error()}
2020-10-27 20:23:29 +01:00
sendResponse(w, r, response)
}
2020-10-27 20:23:29 +01:00
func sendResponse(w http.ResponseWriter, r *http.Request, payload *responses.Subsonic) {
f := utils.ParamString(r, "f")
var response []byte
switch f {
case "json":
w.Header().Set("Content-Type", "application/json")
wrapper := &responses.JsonWrapper{Subsonic: *payload}
response, _ = json.Marshal(wrapper)
case "jsonp":
2020-01-09 06:18:55 +01:00
w.Header().Set("Content-Type", "application/javascript")
callback := utils.ParamString(r, "callback")
wrapper := &responses.JsonWrapper{Subsonic: *payload}
data, _ := json.Marshal(wrapper)
response = []byte(fmt.Sprintf("%s(%s)", callback, data))
default:
w.Header().Set("Content-Type", "application/xml")
response, _ = xml.Marshal(payload)
}
2020-02-02 02:07:15 +01:00
if payload.Status == "ok" {
if log.CurrentLevel() >= log.LevelTrace {
log.Debug(r.Context(), "API: Successful response", "status", "OK", "body", string(response))
2020-02-02 02:07:15 +01:00
} else {
log.Debug(r.Context(), "API: Successful response", "status", "OK")
2020-02-02 02:07:15 +01:00
}
} else {
log.Warn(r.Context(), "API: Failed response", "error", payload.Error.Code, "message", payload.Error.Message)
}
2020-04-26 18:35:26 +02:00
if _, err := w.Write(response); err != nil {
log.Error(r, "Error sending response to client", "payload", string(response), err)
}
}