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.
This commit is contained in:
Deluan Quintão 2026-11-15 17:23:51 -05:00 committed by GitHub
parent dfe90ba384
commit a6f3b91f87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 110 additions and 10 deletions

View File

@ -11,6 +11,7 @@ linters:
- bodyclose
- containedctx
- copyloopvar
- depguard
- dogsled
- durationcheck
- errorlint
@ -29,6 +30,96 @@ linters:
disable:
- staticcheck
settings:
depguard:
rules:
api-subsonic:
list-mode: lax
files:
- "**/server/subsonic/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/server/nativeapi
desc: &api_isolation "API packages must not import each other. Move shared code to a neutral package (for example server/filter or core/publicurl)."
- pkg: github.com/navidrome/navidrome/server/jellyfin
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/apiv1
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/public
desc: *api_isolation
api-nativeapi:
list-mode: lax
files:
- "**/server/nativeapi/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/server/subsonic
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/jellyfin
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/apiv1
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/public
desc: *api_isolation
api-jellyfin:
list-mode: lax
files:
- "**/server/jellyfin/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/server/subsonic
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/nativeapi
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/apiv1
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/public
desc: *api_isolation
api-apiv1:
list-mode: lax
files:
- "**/server/apiv1/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/server/subsonic
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/nativeapi
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/jellyfin
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/public
desc: *api_isolation
api-public:
list-mode: lax
files:
- "**/server/public/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/server/subsonic
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/nativeapi
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/jellyfin
desc: *api_isolation
- pkg: github.com/navidrome/navidrome/server/apiv1
desc: *api_isolation
persistence-layer:
list-mode: lax
files:
- "**/persistence/**"
- "!$test"
deny:
- pkg: github.com/navidrome/navidrome/core
desc: &persistence_layer "persistence implements the model repositories. Do not depend on business logic or HTTP layers; move the logic to core."
- pkg: github.com/navidrome/navidrome/server
desc: *persistence_layer
- pkg: github.com/navidrome/navidrome/scanner
desc: *persistence_layer
- pkg: github.com/navidrome/navidrome/plugins
desc: *persistence_layer
- pkg: github.com/navidrome/navidrome/adapters
desc: *persistence_layer
- pkg: github.com/navidrome/navidrome/cmd
desc: *persistence_layer
errcheck:
exclude-functions:
- (*github.com/zeebo/xxh3.Hasher).Write

View File

@ -29,6 +29,10 @@ func ImageURL(ctx context.Context, artID model.ArtworkID, size int) string {
return PublicURL(ctx, uri, params)
}
func ShareURL(ctx context.Context, id string) string {
return PublicURL(ctx, path.Join(consts.URLPathPublic, id), nil)
}
// PublicURL builds a full URL for public-facing resources.
// It uses ShareURL from config if available, otherwise falls back to the address the
// client used to reach the server, recorded in the context.

View File

@ -215,6 +215,18 @@ var _ = Describe("Public URL Utilities", func() {
})
})
Describe("ShareURL", func() {
It("builds the share page URL under the configured ShareURL path", func() {
conf.Update(func(c *conf.Options) { c.ShareURL = "https://example.com/navi/" })
Expect(publicurl.ShareURL(ctx, "abc123")).To(Equal("https://example.com/navi/share/abc123"))
})
It("uses the request address when ShareURL is not set", func() {
ctx = request.WithServerAddress(ctx, "https", "music.example.com")
Expect(publicurl.ShareURL(ctx, "abc123")).To(Equal("https://music.example.com/share/abc123"))
})
})
Describe("ImageURL address precedence", func() {
var artID model.ArtworkID

View File

@ -81,7 +81,7 @@ func checkShareError(ctx context.Context, w http.ResponseWriter, err error, id s
}
func (pub *Router) mapShareInfo(r *http.Request, s model.Share) *model.Share {
s.URL = ShareURL(r.Context(), s.ID)
s.URL = publicurl.ShareURL(r.Context(), s.ID)
s.ImageURL = publicurl.ImageURL(r.Context(), s.CoverArtID(), conf.Server().UICoverArtSize)
for i := range s.Tracks {
s.Tracks[i].ID = encodeMediafileShare(s, s.Tracks[i].ID)

View File

@ -1,7 +1,6 @@
package public
import (
"context"
"net/http"
"path"
@ -10,7 +9,6 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/artwork"
"github.com/navidrome/navidrome/core/publicurl"
"github.com/navidrome/navidrome/core/stream"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server"
@ -60,8 +58,3 @@ func (pub *Router) routes() http.Handler {
})
return r
}
func ShareURL(ctx context.Context, id string) string {
uri := path.Join(consts.URLPathPublic, id)
return publicurl.PublicURL(ctx, uri, nil)
}

View File

@ -7,8 +7,8 @@ import (
"time"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/core/publicurl"
"github.com/navidrome/navidrome/model"
"github.com/navidrome/navidrome/server/public"
"github.com/navidrome/navidrome/server/subsonic/responses"
"github.com/navidrome/navidrome/utils/req"
"github.com/navidrome/navidrome/utils/slice"
@ -32,7 +32,7 @@ func (api *Router) GetShares(r *http.Request) (*responses.Subsonic, error) {
func (api *Router) buildShare(r *http.Request, share model.Share) responses.Share {
resp := responses.Share{
ID: share.ID,
Url: public.ShareURL(r.Context(), share.ID),
Url: publicurl.ShareURL(r.Context(), share.ID),
Description: share.Description,
Username: share.Username,
Created: share.CreatedAt,