Readability pass across all four areas of the stack. Comments explain why rather than what, concentrated at the points where behaviour is non-obvious: concurrency, caching, auth, scraping and nginx templating. Backend: doc comments on every exported declaration across handlers, services, models and resolvers. Resolver commentary lives inside function bodies so gqlgen does not strip it on regeneration; schema files use # comments rather than description blocks so introspection output is unchanged. Extracted the duplicated auth-cookie blocks into helpers. Frontend: header comments on every component, store and view, including STATUS: notes marking which of the parallel/legacy views are still live. Documented the non-reactive animation-state pattern, the homeData hub and the WebSocket stale-socket guard. Removed a few dead imports and named the reconnect-backoff and poll-interval constants. Python: module and function docstrings covering the Travelodge scraping contract, the deadline threading, the two-executor concurrency and the NDJSON streaming protocol. Infra: header blocks on every compose file, nginx template, Dockerfile and entrypoint, covering the config selection, envsubst allow-lists, rate-limit semantics and volume persistence. Hoisted the duplicated envsubst variable list in nginx/entrypoint.sh. New: backend/README.md, python/README.md, DEPLOYMENT.md, .env.example, backend/graph/doc.go. Refreshed vue/README.md. Corrected CLAUDE.md, which stated a 24h access token where main.go sets 7 days. No behaviour change. go build/vet, vite build, py_compile and docker compose config all pass. Co-Authored-By: Claude Opus 5 <[email protected]>
155 lines
4.9 KiB
Go
155 lines
4.9 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/zmb3/spotify/v2"
|
|
spotifyauth "github.com/zmb3/spotify/v2/auth"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// SpotifyConfig holds the OAuth app credentials. AuthState is the fixed
|
|
// `state` value used in the authorisation URL; because the flow is only ever
|
|
// driven by hand by the site owner, it is a constant rather than a
|
|
// per-request nonce.
|
|
type SpotifyConfig struct {
|
|
AuthState string
|
|
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.
|
|
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.
|
|
func SaveSpotifyToken(path string, tok *oauth2.Token) error {
|
|
data := struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
TokenType string `json:"token_type"`
|
|
Expiry time.Time `json:"expiry"`
|
|
}{
|
|
AccessToken: tok.AccessToken,
|
|
RefreshToken: tok.RefreshToken,
|
|
TokenType: tok.TokenType,
|
|
Expiry: tok.Expiry,
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
|
|
return fmt.Errorf("creating token directory: %w", err)
|
|
}
|
|
|
|
jsonBytes, err := json.MarshalIndent(data, "", " ")
|
|
if err != nil {
|
|
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 {
|
|
return nil, err
|
|
}
|
|
|
|
var saved struct {
|
|
AccessToken string `json:"access_token"`
|
|
RefreshToken string `json:"refresh_token"`
|
|
TokenType string `json:"token_type"`
|
|
Expiry time.Time `json:"expiry"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &saved); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tok := &oauth2.Token{
|
|
AccessToken: saved.AccessToken,
|
|
RefreshToken: saved.RefreshToken,
|
|
TokenType: saved.TokenType,
|
|
Expiry: saved.Expiry,
|
|
}
|
|
|
|
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 prints the authorisation URL to the container log
|
|
// and returns a nil client. Every Spotify handler and resolver therefore has
|
|
// to nil-check Store.SpotifyClient. Visiting that URL sends the browser to
|
|
// /spotify/callback, which fills the client in at runtime.
|
|
func InitSpotifyAuth(config *SpotifyConfig) (*spotifyauth.Authenticator, *spotify.Client) {
|
|
auth := spotifyauth.New(
|
|
spotifyauth.WithRedirectURL(config.RedirectURL),
|
|
spotifyauth.WithClientID(config.ClientID),
|
|
spotifyauth.WithClientSecret(config.ClientSecret),
|
|
spotifyauth.WithScopes(
|
|
spotifyauth.ScopeUserReadPlaybackState,
|
|
spotifyauth.ScopeUserReadCurrentlyPlaying,
|
|
spotifyauth.ScopeUserReadRecentlyPlayed,
|
|
),
|
|
)
|
|
|
|
// check if token exists locally
|
|
token, err := LoadSpotifyToken(SPOTIFY_TOKEN_JSON_PATH)
|
|
if err != nil || token == nil {
|
|
fmt.Println("No token saved. Authenticate Spotify with:")
|
|
fmt.Println(auth.AuthURL(config.AuthState))
|
|
return auth, nil
|
|
}
|
|
|
|
// refresh token and client
|
|
client, err := RefreshClient(auth, token)
|
|
if err != nil {
|
|
fmt.Println("Failed to refresh token. Authenticate Spotify with:")
|
|
fmt.Println(auth.AuthURL(config.AuthState))
|
|
return auth, nil
|
|
}
|
|
|
|
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.
|
|
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.
|
|
ctx := context.Background()
|
|
|
|
token, err := auth.RefreshToken(ctx, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
SaveSpotifyToken(SPOTIFY_TOKEN_JSON_PATH, token)
|
|
|
|
client := spotify.New(auth.Client(ctx, token))
|
|
|
|
return client, nil
|
|
}
|