feat: add basic websocket
This commit is contained in:
parent
404b9a937e
commit
47b757769a
2 changed files with 38 additions and 0 deletions
35
internal/handler/computer.go
Normal file
35
internal/handler/computer.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
// todo: change for security
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
func HandleComputerWebsocket(w http.ResponseWriter, r *http.Request) {
|
||||
connection, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
slog.Error("Error when upgrading websocket connection", slog.Any("error", err))
|
||||
return
|
||||
}
|
||||
defer connection.Close()
|
||||
|
||||
slog.Info("Websocket connection established!")
|
||||
|
||||
for {
|
||||
_, message, err := connection.ReadMessage()
|
||||
if err != nil {
|
||||
slog.Error("Error reading WebSocket message", slog.Any("error", err))
|
||||
break
|
||||
}
|
||||
slog.Info("Recieved message", slog.Any("message", message))
|
||||
}
|
||||
}
|
||||
|
|
@ -38,9 +38,12 @@ func NewRouter() {
|
|||
|
||||
r.Get("/visit-counter", handler.HandleGetVisitCounter)
|
||||
r.With(httprate.LimitByRealIP(1, time.Hour)).Patch("/visit-counter", handler.HandlePatchVisitCounter)
|
||||
|
||||
r.Get("/currently-playing", handler.HandleGetCurrentlyPlaying)
|
||||
r.Get("/status", handler.HandleGetStatus)
|
||||
|
||||
r.Get("/ws/computer", handler.HandleComputerWebsocket)
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if len(port) == 0 {
|
||||
port = "8080"
|
||||
|
|
|
|||
Loading…
Reference in a new issue