2021-06-13 19:15:41 -04:00
|
|
|
package nativeapi
|
2020-01-19 19:34:54 -05:00
|
|
|
|
|
|
|
|
import (
|
2020-01-19 20:40:18 -05:00
|
|
|
"context"
|
2026-11-18 13:55:16 -05:00
|
|
|
"mime"
|
2020-01-19 19:34:54 -05:00
|
|
|
"net/http"
|
2024-12-17 17:10:55 -05:00
|
|
|
"strconv"
|
2025-02-19 17:35:17 -08:00
|
|
|
"time"
|
2020-01-19 19:34:54 -05:00
|
|
|
|
2020-01-19 20:40:18 -05:00
|
|
|
"github.com/deluan/rest"
|
2021-05-11 17:21:18 -04:00
|
|
|
"github.com/go-chi/chi/v5"
|
2025-02-19 17:35:17 -08:00
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2020-04-27 21:23:15 -04:00
|
|
|
"github.com/navidrome/navidrome/conf"
|
2021-06-09 01:14:30 +05:30
|
|
|
"github.com/navidrome/navidrome/core"
|
2026-08-09 15:03:27 -04:00
|
|
|
"github.com/navidrome/navidrome/core/artwork"
|
2026-08-25 23:59:40 -04:00
|
|
|
"github.com/navidrome/navidrome/core/external"
|
2024-12-17 17:10:55 -05:00
|
|
|
"github.com/navidrome/navidrome/core/metrics"
|
2026-02-21 19:57:13 -05:00
|
|
|
playlistsvc "github.com/navidrome/navidrome/core/playlists"
|
2026-09-19 14:57:01 -04:00
|
|
|
"github.com/navidrome/navidrome/core/quickconnect"
|
2025-02-19 17:35:17 -08:00
|
|
|
"github.com/navidrome/navidrome/log"
|
2020-01-23 19:44:08 -05:00
|
|
|
"github.com/navidrome/navidrome/model"
|
2025-07-18 18:41:12 -04:00
|
|
|
"github.com/navidrome/navidrome/model/request"
|
2021-06-13 12:46:36 -04:00
|
|
|
"github.com/navidrome/navidrome/server"
|
2020-01-19 19:34:54 -05:00
|
|
|
)
|
|
|
|
|
|
2026-01-14 19:22:48 -05:00
|
|
|
// PluginManager defines the interface for plugin management operations.
|
|
|
|
|
// This interface is used by the API handlers to enable/disable plugins and update configuration.
|
|
|
|
|
type PluginManager interface {
|
|
|
|
|
EnablePlugin(ctx context.Context, id string) error
|
|
|
|
|
DisablePlugin(ctx context.Context, id string) error
|
2026-01-19 20:51:00 -05:00
|
|
|
ValidatePluginConfig(ctx context.Context, id, configJSON string) error
|
2026-01-14 19:22:48 -05:00
|
|
|
UpdatePluginConfig(ctx context.Context, id, configJSON string) error
|
|
|
|
|
UpdatePluginUsers(ctx context.Context, id, usersJSON string, allUsers bool) error
|
2026-02-28 10:59:13 -05:00
|
|
|
UpdatePluginLibraries(ctx context.Context, id, librariesJSON string, allLibraries, allowWriteAccess bool) error
|
2026-01-14 19:22:48 -05:00
|
|
|
RescanPlugins(ctx context.Context) error
|
|
|
|
|
UnloadDisabledPlugins(ctx context.Context)
|
2020-01-19 19:34:54 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-14 19:22:48 -05:00
|
|
|
type Router struct {
|
|
|
|
|
http.Handler
|
|
|
|
|
ds model.DataStore
|
|
|
|
|
share core.Share
|
2026-02-21 19:57:13 -05:00
|
|
|
playlists playlistsvc.Playlists
|
2026-01-14 19:22:48 -05:00
|
|
|
insights metrics.Insights
|
|
|
|
|
libs core.Library
|
|
|
|
|
users core.User
|
|
|
|
|
maintenance core.Maintenance
|
|
|
|
|
pluginManager PluginManager
|
2026-08-09 15:03:27 -04:00
|
|
|
imgUpload artwork.Uploader
|
2026-08-25 23:59:40 -04:00
|
|
|
provider external.Provider
|
2026-09-19 14:57:01 -04:00
|
|
|
quickConnect quickconnect.QuickConnect
|
2026-01-14 19:22:48 -05:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 14:57:01 -04:00
|
|
|
func New(ds model.DataStore, share core.Share, playlists playlistsvc.Playlists, insights metrics.Insights, libraryService core.Library, userService core.User, maintenance core.Maintenance, pluginManager PluginManager, imgUpload artwork.Uploader, provider external.Provider, quickConnect quickconnect.QuickConnect) *Router {
|
|
|
|
|
r := &Router{ds: ds, share: share, playlists: playlists, insights: insights, libs: libraryService, users: userService, maintenance: maintenance, pluginManager: pluginManager, imgUpload: imgUpload, provider: provider, quickConnect: quickConnect}
|
2021-06-13 12:46:36 -04:00
|
|
|
r.Handler = r.routes()
|
|
|
|
|
return r
|
2020-04-03 17:50:42 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) routes() http.Handler {
|
2020-01-19 19:34:54 -05:00
|
|
|
r := chi.NewRouter()
|
2020-01-19 20:40:18 -05:00
|
|
|
|
2022-09-27 19:16:10 -04:00
|
|
|
// Public
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/translation", newTranslationRepository(), false)
|
2020-07-19 14:45:05 -04:00
|
|
|
|
2022-09-27 19:16:10 -04:00
|
|
|
// Protected
|
|
|
|
|
r.Group(func(r chi.Router) {
|
2025-11-08 20:11:00 -05:00
|
|
|
r.Use(server.Authenticator(api.ds))
|
2022-09-27 19:16:10 -04:00
|
|
|
r.Use(server.JWTRefresher)
|
2025-11-08 20:11:00 -05:00
|
|
|
r.Use(server.UpdateLastAccessMiddleware(api.ds))
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/user", api.users.Repository(), true)
|
|
|
|
|
rx(r, "/song", api.ds.MediaFile(), false)
|
|
|
|
|
rx(r, "/album", api.ds.Album(), false)
|
2026-03-15 22:19:55 -04:00
|
|
|
api.addArtistRoute(r)
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/genre", api.ds.Genre(), false)
|
|
|
|
|
rx(r, "/player", api.ds.Player(), true)
|
2026-11-13 11:42:14 -05:00
|
|
|
rx(r, "/transcoding", api.ds.Transcoding(), conf.Server().EnableTranscodingConfig)
|
2026-03-18 18:57:33 -04:00
|
|
|
api.addRadioRoute(r)
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/tag", api.ds.Tag(), false)
|
|
|
|
|
rx(r, "/scrobble", api.ds.Scrobble(), false)
|
2026-11-13 11:42:14 -05:00
|
|
|
if conf.Server().EnableSharing {
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/share", api.share.Repository(), true)
|
2023-01-26 10:12:13 -05:00
|
|
|
}
|
2020-07-19 14:45:05 -04:00
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
api.addPlaylistRoute(r)
|
|
|
|
|
api.addPlaylistTrackRoute(r)
|
|
|
|
|
api.addSongPlaylistsRoute(r)
|
|
|
|
|
api.addQueueRoute(r)
|
|
|
|
|
api.addMissingFilesRoute(r)
|
|
|
|
|
api.addKeepAliveRoute(r)
|
|
|
|
|
api.addInsightsRoute(r)
|
2026-09-19 14:57:01 -04:00
|
|
|
api.addQuickConnectRoute(r)
|
2025-07-18 18:41:12 -04:00
|
|
|
|
|
|
|
|
r.With(adminOnlyMiddleware).Group(func(r chi.Router) {
|
2025-11-08 20:11:00 -05:00
|
|
|
api.addInspectRoute(r)
|
|
|
|
|
api.addConfigRoute(r)
|
|
|
|
|
api.addUserLibraryRoute(r)
|
2026-01-14 19:22:48 -05:00
|
|
|
api.addPluginRoute(r)
|
2026-08-25 23:59:40 -04:00
|
|
|
api.addMetadataRoute(r)
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/library", api.libs.Repository(), true)
|
2025-07-18 18:41:12 -04:00
|
|
|
})
|
2020-01-19 20:40:18 -05:00
|
|
|
})
|
2020-01-22 18:35:44 -05:00
|
|
|
|
2020-01-19 19:34:54 -05:00
|
|
|
return r
|
|
|
|
|
}
|
2020-01-19 20:40:18 -05:00
|
|
|
|
2026-09-25 18:06:10 -04:00
|
|
|
func rx[T any](r chi.Router, pathPrefix string, repo rest.Repository[T], persistable bool) {
|
2020-01-19 20:40:18 -05:00
|
|
|
r.Route(pathPrefix, func(r chi.Router) {
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Get("/", rest.GetAll(repo))
|
2020-04-27 21:23:15 -04:00
|
|
|
if persistable {
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Post("/", rest.Post(repo))
|
2020-04-27 21:23:15 -04:00
|
|
|
}
|
2020-05-01 18:29:50 -04:00
|
|
|
r.Route("/{id}", func(r chi.Router) {
|
2022-12-30 22:34:00 -05:00
|
|
|
r.Use(server.URLParamsMiddleware)
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Get("/", rest.Get(repo))
|
2020-04-27 21:23:15 -04:00
|
|
|
if persistable {
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Put("/", rest.Put(repo))
|
|
|
|
|
r.Delete("/", rest.Delete(repo))
|
2020-04-27 21:23:15 -04:00
|
|
|
}
|
2020-01-19 20:40:18 -05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addPlaylistRoute(r chi.Router) {
|
2026-09-25 18:06:10 -04:00
|
|
|
repo := api.playlists.Repository()
|
2023-11-01 12:59:47 -06:00
|
|
|
|
|
|
|
|
r.Route("/playlist", func(r chi.Router) {
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Get("/", rest.GetAll(repo))
|
2026-11-18 13:55:16 -05:00
|
|
|
createJSON := limitPlaylistJSON(rest.Post(repo))
|
2023-11-01 12:59:47 -06:00
|
|
|
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-11-18 13:55:16 -05:00
|
|
|
if mediaType, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); mediaType == "application/json" {
|
|
|
|
|
createJSON.ServeHTTP(w, r)
|
2023-11-01 12:59:47 -06:00
|
|
|
return
|
|
|
|
|
}
|
2025-11-08 20:11:00 -05:00
|
|
|
createPlaylistFromM3U(api.playlists)(w, r)
|
2023-11-01 12:59:47 -06:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Route("/{id}", func(r chi.Router) {
|
|
|
|
|
r.Use(server.URLParamsMiddleware)
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Get("/", rest.Get(repo))
|
2026-11-18 13:55:16 -05:00
|
|
|
r.With(limitPlaylistJSON).Put("/", rest.Put(repo))
|
2026-09-25 18:06:10 -04:00
|
|
|
r.Delete("/", rest.Delete(repo))
|
2026-03-01 20:07:18 +01:00
|
|
|
r.Post("/image", uploadPlaylistImage(api.playlists))
|
|
|
|
|
r.Delete("/image", deletePlaylistImage(api.playlists))
|
2023-11-01 12:59:47 -06:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addPlaylistTrackRoute(r chi.Router) {
|
2020-05-08 23:48:06 -04:00
|
|
|
r.Route("/playlist/{playlistId}/tracks", func(r chi.Router) {
|
2020-08-22 12:15:26 -04:00
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
getPlaylist(api.playlists)(w, r)
|
2020-08-22 12:15:26 -04:00
|
|
|
})
|
2022-12-30 22:34:00 -05:00
|
|
|
r.With(server.URLParamsMiddleware).Route("/", func(r chi.Router) {
|
2021-10-26 14:05:28 -04:00
|
|
|
r.Delete("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
deleteFromPlaylist(api.playlists)(w, r)
|
2021-10-26 14:05:28 -04:00
|
|
|
})
|
|
|
|
|
r.Post("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
addToPlaylist(api.playlists)(w, r)
|
2021-10-26 14:05:28 -04:00
|
|
|
})
|
|
|
|
|
})
|
2020-05-08 23:48:06 -04:00
|
|
|
r.Route("/{id}", func(r chi.Router) {
|
2022-12-30 22:34:00 -05:00
|
|
|
r.Use(server.URLParamsMiddleware)
|
2025-06-04 20:38:28 -04:00
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
getPlaylistTrack(api.playlists)(w, r)
|
2025-06-04 20:38:28 -04:00
|
|
|
})
|
2020-06-04 19:05:41 -04:00
|
|
|
r.Put("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
reorderItem(api.playlists)(w, r)
|
2020-06-04 19:05:41 -04:00
|
|
|
})
|
2020-05-16 18:10:08 -04:00
|
|
|
r.Delete("/", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
deleteFromPlaylist(api.playlists)(w, r)
|
2020-05-16 18:10:08 -04:00
|
|
|
})
|
2020-05-08 23:48:06 -04:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-02-19 17:35:17 -08:00
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addSongPlaylistsRoute(r chi.Router) {
|
2025-06-01 14:34:45 -04:00
|
|
|
r.With(server.URLParamsMiddleware).Get("/song/{id}/playlists", func(w http.ResponseWriter, r *http.Request) {
|
2026-02-21 19:57:13 -05:00
|
|
|
getSongPlaylists(api.playlists)(w, r)
|
2025-05-30 21:26:35 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addQueueRoute(r chi.Router) {
|
2025-06-10 23:00:44 -04:00
|
|
|
r.Route("/queue", func(r chi.Router) {
|
2025-11-08 20:11:00 -05:00
|
|
|
r.Get("/", getQueue(api.ds))
|
|
|
|
|
r.Post("/", saveQueue(api.ds))
|
|
|
|
|
r.Put("/", updateQueue(api.ds))
|
|
|
|
|
r.Delete("/", clearQueue(api.ds))
|
2025-06-10 23:00:44 -04:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addMissingFilesRoute(r chi.Router) {
|
2025-02-19 17:35:17 -08:00
|
|
|
r.Route("/missing", func(r chi.Router) {
|
2026-09-25 18:06:10 -04:00
|
|
|
rx(r, "/", newMissingRepository(api.ds), false)
|
2025-11-08 20:11:00 -05:00
|
|
|
r.Delete("/", deleteMissingFiles(api.maintenance))
|
2025-02-19 17:35:17 -08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeDeleteManyResponse(w http.ResponseWriter, r *http.Request, ids []string) {
|
2026-09-23 18:10:25 +02:00
|
|
|
var payload any
|
2025-02-19 17:35:17 -08:00
|
|
|
if len(ids) == 1 {
|
2026-09-23 18:10:25 +02:00
|
|
|
payload = struct {
|
|
|
|
|
ID string `json:"id"`
|
|
|
|
|
}{ID: ids[0]}
|
2025-02-19 17:35:17 -08:00
|
|
|
} else {
|
2026-09-23 18:10:25 +02:00
|
|
|
payload = struct {
|
2025-02-19 17:35:17 -08:00
|
|
|
Ids []string `json:"ids"`
|
2026-09-23 18:10:25 +02:00
|
|
|
}{Ids: ids}
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
2026-09-23 18:10:25 +02:00
|
|
|
if err := rest.RespondWithJSON(w, http.StatusOK, payload); err != nil {
|
|
|
|
|
log.Error(r.Context(), "Error writing response", "ids", ids, err)
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addInspectRoute(r chi.Router) {
|
2026-11-13 11:42:14 -05:00
|
|
|
if conf.Server().Inspect.Enabled {
|
2025-02-19 17:35:17 -08:00
|
|
|
r.Group(func(r chi.Router) {
|
2026-11-13 11:42:14 -05:00
|
|
|
if conf.Server().Inspect.MaxRequests > 0 {
|
|
|
|
|
log.Debug("Throttling inspect", "maxRequests", conf.Server().Inspect.MaxRequests,
|
|
|
|
|
"backlogLimit", conf.Server().Inspect.BacklogLimit, "backlogTimeout",
|
|
|
|
|
conf.Server().Inspect.BacklogTimeout)
|
|
|
|
|
r.Use(middleware.ThrottleBacklog(conf.Server().Inspect.MaxRequests, conf.Server().Inspect.BacklogLimit, time.Duration(conf.Server().Inspect.BacklogTimeout)))
|
2025-02-19 17:35:17 -08:00
|
|
|
}
|
2025-11-08 20:11:00 -05:00
|
|
|
r.Get("/inspect", inspect(api.ds))
|
2025-02-19 17:35:17 -08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-05-30 21:07:08 -04:00
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addConfigRoute(r chi.Router) {
|
2026-11-13 11:42:14 -05:00
|
|
|
if conf.Server().DevUIShowConfig {
|
2025-05-30 21:07:08 -04:00
|
|
|
r.Get("/config/*", getConfig)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addKeepAliveRoute(r chi.Router) {
|
2025-05-30 21:07:08 -04:00
|
|
|
r.Get("/keepalive/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
_, _ = w.Write([]byte(`{"response":"ok", "id":"keepalive"}`))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-08 20:11:00 -05:00
|
|
|
func (api *Router) addInsightsRoute(r chi.Router) {
|
2025-05-30 21:07:08 -04:00
|
|
|
r.Get("/insights/*", func(w http.ResponseWriter, r *http.Request) {
|
2025-11-08 20:11:00 -05:00
|
|
|
last, success := api.insights.LastRun(r.Context())
|
2026-11-13 11:42:14 -05:00
|
|
|
if conf.Server().EnableInsightsCollector {
|
2026-11-15 17:03:17 -05:00
|
|
|
_, _ = w.Write([]byte(`{"id":"insights_status", "lastRun":"` + last.Format("2006-01-02 15:04:05") + `", "success":` + strconv.FormatBool(success) + `}`))
|
2025-05-30 21:07:08 -04:00
|
|
|
} else {
|
|
|
|
|
_, _ = w.Write([]byte(`{"id":"insights_status", "lastRun":"disabled", "success":false}`))
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-07-18 18:41:12 -04:00
|
|
|
|
2026-11-17 20:38:14 -05:00
|
|
|
// adminOnlyMiddleware ensures only admin users can access endpoints
|
2025-07-18 18:41:12 -04:00
|
|
|
func adminOnlyMiddleware(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
user, ok := request.UserFrom(r.Context())
|
|
|
|
|
if !ok || !user.IsAdmin {
|
|
|
|
|
http.Error(w, "Access denied: admin privileges required", http.StatusForbidden)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
|
})
|
|
|
|
|
}
|