From 369093529549e429fb2ce9bf78d972312d69fcfa Mon Sep 17 00:00:00 2001 From: skidoodle Date: Fri, 28 Feb 2025 21:13:36 +0000 Subject: [PATCH] fix the fucky wucky --- Dockerfile | 6 ++++-- docker-compose.yaml | 6 ------ go.mod | 4 ++-- healthcheck/go.mod | 3 +++ healthcheck/healthcheck.go | 14 ++++++++++++++ main.go | 25 +++++++++++++++++++------ readme.md | 2 +- 7 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 healthcheck/go.mod create mode 100644 healthcheck/healthcheck.go diff --git a/Dockerfile b/Dockerfile index 36c1e0f..323a72c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,17 @@ -FROM golang:1.22.4 AS builder +FROM golang:1.24 AS builder ENV CGO_ENABLED=0 GOOS=linux GOARCH=amd64 WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN go build -ldflags="-s -w" -o spotify-ws . +RUN go build -ldflags="-s -w" -o healthcheck healthcheck.go FROM gcr.io/distroless/static:nonroot WORKDIR /app COPY --from=builder --chown=nonroot:nonroot /app/spotify-ws . +COPY --from=builder --chown=nonroot:nonroot /app/healthcheck . EXPOSE 3000 -HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD curl --fail http://localhost:3000/health || exit 1 +HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD ["./healthcheck"] USER nonroot:nonroot CMD ["./spotify-ws"] diff --git a/docker-compose.yaml b/docker-compose.yaml index 4c22cef..700d701 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,9 +12,3 @@ services: #- LOG_LEVEL=DEBUG # ALLOWED_ORIGINS=http://localhost:3000 # SERVER_PORT=3000 - healthcheck: - test: ["CMD", "curl", "-f", "http://localhost:3000/health"] - interval: 30s - timeout: 5s - retries: 3 - start_period: 5s diff --git a/go.mod b/go.mod index 8187cfc..d825486 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ -module spotify-ws +module skidoodle/spotify-ws -go 1.22.4 +go 1.24.0 require ( github.com/gorilla/websocket v1.5.3 diff --git a/healthcheck/go.mod b/healthcheck/go.mod new file mode 100644 index 0000000..8abade9 --- /dev/null +++ b/healthcheck/go.mod @@ -0,0 +1,3 @@ +module healthcheck + +go 1.24.0 diff --git a/healthcheck/healthcheck.go b/healthcheck/healthcheck.go new file mode 100644 index 0000000..c8a494f --- /dev/null +++ b/healthcheck/healthcheck.go @@ -0,0 +1,14 @@ +package main + +import ( + "net/http" + "os" +) + +func main() { + resp, err := http.Get("http://localhost:3000/health") + if err != nil || resp.StatusCode != 200 { + os.Exit(1) + } + os.Exit(0) +} diff --git a/main.go b/main.go index f03041d..f581a20 100644 --- a/main.go +++ b/main.go @@ -159,6 +159,8 @@ func initializeSpotifyClient() { tokenSource = oauthConfig.TokenSource(context.Background(), token) spotifyClient = spotify.NewClient(oauth2.NewClient(context.Background(), tokenSource)) + + logrus.Info("Spotify client initialized successfully") } func startServer(server *http.Server, _ context.Context) { @@ -269,11 +271,14 @@ func sendInitialState(client *websocket.Conn) { lastState.RLock() defer lastState.RUnlock() - if lastState.track != nil { - if err := client.WriteJSON(lastState.track); err != nil { - logrus.Errorf("Failed to send initial state: %v", err) - client.Close() - } + if lastState.track == nil { + logrus.Debug("No initial state available to send") + return + } + + if err := client.WriteJSON(lastState.track); err != nil { + logrus.Errorf("Failed to send initial state: %v", err) + client.Close() } } @@ -323,6 +328,8 @@ func fetchAndBroadcastState() { return } + logrus.Debugf("Fetched playback state: %+v", current) + updateState(current) } @@ -331,10 +338,16 @@ func updateState(current *spotify.CurrentlyPlaying) { defer lastState.Unlock() if current == nil { + logrus.Warn("Received nil playback state from Spotify") return } - stateChanged := lastState.track == nil || + if lastState.track == nil { + lastState.track = &spotify.CurrentlyPlaying{} + } + + stateChanged := lastState.track.Item == nil || + current.Item == nil || lastState.track.Item.ID != current.Item.ID || lastState.playing != current.Playing diff --git a/readme.md b/readme.md index 844103e..df829fe 100644 --- a/readme.md +++ b/readme.md @@ -39,7 +39,7 @@ services: - REFRESH_TOKEN=${REFRESH_TOKEN} - CLIENT_SECRET=${CLIENT_SECRET} - CLIENT_ID=${CLIENT_ID} - #- LOG_LEVEL=DEBUG + #- LOG_LEVEL=DEBUG|WARN|ERROR #- ALLOWED_ORIGINS=http://localhost:3000 #- SERVER_PORT=3000 ```