navidrome/server/public/public.go
Deluan Quintão a6f3b91f87
chore(lint): add depguard walls between API packages and persistence (#6475)
* refactor: move ShareURL to core/publicurl

The Subsonic sharing endpoint imported the server/public router package only
to build share page URLs. ShareURL now lives in core/publicurl next to
ImageURL and PublicURL, so the two API packages no longer depend on each
other. The generated URLs are unchanged.

* chore(lint): add depguard walls between API packages and persistence

Enable depguard with two kinds of rules for production code:

- subsonic, nativeapi, jellyfin, apiv1 and public must not import each
  other. Shared code belongs in a neutral package.
- persistence must not import core, server, scanner, plugins, adapters or
  cmd.

Each rule's message names the fix. Test files are excluded because the
end-to-end suites wire several layers together on purpose. A model rule
was left out because the Go compiler already rejects those imports as
cycles.
2026-11-15 17:23:51 -05:00

61 lines
1.8 KiB
Go

package public
import (
"net/http"
"path"
"github.com/go-chi/chi/v5"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/stream"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/ui"
)
type Router struct {
http.Handler
artwork artwork.Artwork
streamer stream.MediaStreamer
decider stream.TranscodeDecider
archiver core.Archiver
share core.Share
assetsHandler http.Handler
ds model.DataStore
}
func New(ds model.DataStore, artwork artwork.Artwork, streamer stream.MediaStreamer, decider stream.TranscodeDecider, share core.Share, archiver core.Archiver) *Router {
p := &Router{ds: ds, artwork: artwork, streamer: streamer, decider: decider, share: share, archiver: archiver}
shareRoot := path.Join(conf.Server().BasePath, consts.URLPathPublic)
p.assetsHandler = http.StripPrefix(shareRoot, http.FileServer(http.FS(ui.BuildAssets())))
p.Handler = p.routes()
return p
}
func (pub *Router) routes() http.Handler {
r := chi.NewRouter()
r.Group(func(r chi.Router) {
r.Use(server.URLParamsMiddleware)
r.Group(func(r chi.Router) {
r.Use(server.ThrottleBacklog(conf.Server().DevArtworkMaxRequests, conf.Server().DevArtworkThrottleBacklogLimit,
conf.Server().DevArtworkThrottleBacklogTimeout))
r.HandleFunc("/img/{id}", pub.handleImages)
})
if conf.Server().EnableSharing {
r.HandleFunc("/s/{id}", pub.handleStream)
if conf.Server().EnableDownloads {
r.HandleFunc("/d/{id}", pub.handleDownloads)
}
r.HandleFunc("/{id}/m3u", pub.handleM3U)
r.HandleFunc("/{id}", pub.handleShares)
r.HandleFunc("/", pub.handleShares)
r.Handle("/*", pub.assetsHandler)
}
})
return r
}