mirror of
https://github.com/skidoodle/spotify-ws
synced 2025-10-09 05:22:43 +02:00
fix the fucky wucky
This commit is contained in:
@@ -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"]
|
||||
|
||||
@@ -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
4
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
|
||||
|
||||
3
healthcheck/go.mod
Normal file
3
healthcheck/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module healthcheck
|
||||
|
||||
go 1.24.0
|
||||
14
healthcheck/healthcheck.go
Normal file
14
healthcheck/healthcheck.go
Normal 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)
|
||||
}
|
||||
25
main.go
25
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user