From a6f3b91f87e3d2a3124d6f88d59185dfa87e656e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deluan=20Quint=C3=A3o?= Date: Sun, 15 Nov 2026 17:23:51 -0500 Subject: [PATCH] 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. --- .golangci.yml | 91 ++++++++++++++++++++++++++++++++ core/publicurl/publicurl.go | 4 ++ core/publicurl/publicurl_test.go | 12 +++++ server/public/handle_shares.go | 2 +- server/public/public.go | 7 --- server/subsonic/sharing.go | 4 +- 6 files changed, 110 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 8def002eb..95ce5d685 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -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 diff --git a/core/publicurl/publicurl.go b/core/publicurl/publicurl.go index b7c58aefa..db3f38226 100644 --- a/core/publicurl/publicurl.go +++ b/core/publicurl/publicurl.go @@ -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. diff --git a/core/publicurl/publicurl_test.go b/core/publicurl/publicurl_test.go index 5ec46dae4..a7bfc64fb 100644 --- a/core/publicurl/publicurl_test.go +++ b/core/publicurl/publicurl_test.go @@ -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 diff --git a/server/public/handle_shares.go b/server/public/handle_shares.go index 5e298375b..9e3e8c402 100644 --- a/server/public/handle_shares.go +++ b/server/public/handle_shares.go @@ -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) diff --git a/server/public/public.go b/server/public/public.go index a5f10e87f..dd7f180b7 100644 --- a/server/public/public.go +++ b/server/public/public.go @@ -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) -} diff --git a/server/subsonic/sharing.go b/server/subsonic/sharing.go index 85d9f9363..0de26211b 100644 --- a/server/subsonic/sharing.go +++ b/server/subsonic/sharing.go @@ -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,