Trim comments to the non-obvious why
Test and deploy / test (push) Successful in 28s
Test and deploy / deploy (push) Successful in 31s

Drop narration, history and restated-code comments across the repo,
keep short ones for security boundaries, upstream quirks and ordering
constraints, and record the convention in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-24 20:49:03 +00:00
co-authored by Claude Opus 5.5
parent effddd2794
commit 4530cb8dc4
222 changed files with 1173 additions and 5423 deletions
+8 -39
View File
@@ -13,26 +13,17 @@ import (
"golang.org/x/oauth2"
)
// SpotifyConfig holds the OAuth app credentials.
//
// There is no state value here any more: the `state` parameter is a one-shot
// CSRF nonce minted per authorisation request by handlers.Store, so a fixed
// configured value would defeat the point.
type SpotifyConfig struct {
RedirectURL string
ClientID string
ClientSecret string
}
// SPOTIFY_TOKEN_JSON_PATH is inside a Docker volume, so the refresh token
// survives container restarts and the OAuth dance only has to be done once.
// On a mounted volume, so the OAuth dance survives rebuilds.
const SPOTIFY_TOKEN_JSON_PATH = "/backend/token/spotify_token.json"
// SaveSpotifyToken persists an OAuth token to disk.
//
// It copies the fields into a local anonymous struct rather than marshalling
// oauth2.Token directly, so the on-disk format stays stable regardless of what
// the oauth2 package adds to its own struct or its JSON tags.
// SaveSpotifyToken writes its own struct so the file format doesn't follow
// oauth2.Token's.
func SaveSpotifyToken(path string, tok *oauth2.Token) error {
data := struct {
AccessToken string `json:"access_token"`
@@ -55,13 +46,9 @@ func SaveSpotifyToken(path string, tok *oauth2.Token) error {
return err
}
// 0600 ensures only your user can read/write the file
return os.WriteFile(path, jsonBytes, 0600)
}
// LoadSpotifyToken reads a token previously written by SaveSpotifyToken.
// A missing file returns an error, which callers treat as "not yet
// authenticated" rather than a fault.
func LoadSpotifyToken(path string) (*oauth2.Token, error) {
data, err := os.ReadFile(path)
if err != nil {
@@ -89,16 +76,8 @@ func LoadSpotifyToken(path string) (*oauth2.Token, error) {
return tok, nil
}
// InitSpotifyAuth builds the OAuth authenticator and, if a saved token
// exists, an authenticated client.
//
// Start-up never fails on Spotify problems: when there is no token or the
// refresh is rejected it logs how to authenticate and returns a nil client.
// Every Spotify handler and resolver therefore has to nil-check
// Store.SpotifyClient. The authorisation URL can no longer be printed here
// because its state nonce is per-request (see handlers.Store.StartSpotifyAuth);
// following it sends the browser to /spotify/callback, which fills the client
// in at runtime.
// InitSpotifyAuth never fails startup: with no usable token it returns a nil
// client, so every caller must nil-check Store.SpotifyClient.
func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotify.Client) {
auth := spotifyauth.New(
spotifyauth.WithRedirectURL(config.RedirectURL),
@@ -111,14 +90,12 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif
),
)
// check if token exists locally
token, err := LoadSpotifyToken(SPOTIFY_TOKEN_JSON_PATH)
if err != nil || token == nil {
fmt.Println("No Spotify token saved. Authenticate by calling GET /api/spotify/auth as an admin and opening the URL it returns.")
return auth, nil
}
// refresh token and client
client, err := RefreshClient(auth, token)
if err != nil {
fmt.Println("Failed to refresh Spotify token. Re-authenticate by calling GET /api/spotify/auth as an admin and opening the URL it returns.")
@@ -128,18 +105,10 @@ func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotif
return auth, client
}
// RefreshClient exchanges a stored token for a fresh one and returns a client
// using it.
//
// Spotify may issue a new refresh token during this exchange, so the result is
// written straight back to disk; the write error is ignored because a failed
// save only costs a re-authentication later, and failing the whole start-up
// over it would be worse. The returned client also holds the token source
// internally and keeps refreshing on its own — but those later refreshes are
// NOT persisted, so after a long uptime the file on disk can be stale.
// RefreshClient saves the new token, ignoring write errors. The client's
// later automatic refreshes are not persisted.
func RefreshClient(auth *spotifyauth.Authenticator, token *oauth2.Token) (*spotify.Client, error) {
// context.Background rather than a request context: the resulting client
// is long-lived and must outlive whichever request triggered this.
// The client outlives the request that created it.
ctx := context.Background()
token, err := auth.RefreshToken(ctx, token)