fix the fucky wucky

This commit is contained in:
2025-02-28 21:13:36 +00:00
parent dbe9e1a5a7
commit 3690935295
7 changed files with 43 additions and 17 deletions

View File

@@ -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"]

View File

@@ -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

4
go.mod
View File

@@ -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

3
healthcheck/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module healthcheck
go 1.24.0

View File

@@ -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)
}

19
main.go
View File

@@ -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,13 +271,16 @@ func sendInitialState(client *websocket.Conn) {
lastState.RLock()
defer lastState.RUnlock()
if lastState.track != nil {
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()
}
}
}
func messageHandler(ctx context.Context) {
for {
@@ -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

View File

@@ -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
```