Files
spotify-ws/internal/websocket/handler.go
2025-10-03 03:45:29 +02:00

37 lines
919 B
Go

package websocket
import (
"log/slog"
"net/http"
"golang.org/x/net/websocket"
)
// newWebsocketHandler creates the handler that upgrades connections.
func (s *Server) newWebsocketHandler() websocket.Handler {
return func(ws *websocket.Conn) {
origin := ws.Config().Origin.String()
if !s.originChecker(origin) {
slog.Warn("origin not allowed, rejecting connection", "origin", origin)
return
}
slog.Debug("client connected, upgrading connection", "remoteAddr", ws.RemoteAddr())
client := &Client{hub: s.hub, conn: ws, send: make(chan []byte, 256)}
client.hub.register <- client
go client.writePump()
client.readPump()
}
}
// healthHandler responds to Docker health checks.
func healthHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte("OK")); err != nil {
slog.Warn("failed to write health check response", "error", err)
}
}