diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 788943d..558a551 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -25,6 +25,9 @@ jobs: - name: test run: go test ./... + - name: test ui + run: go test ./... -tags=test_ui + build: runs-on: ubuntu-latest diff --git a/handlers.go b/handlers.go index d3f61be..72bf6c4 100644 --- a/handlers.go +++ b/handlers.go @@ -127,10 +127,14 @@ func wsHandler(w http.ResponseWriter, r *http.Request) { return } + if data.Type == common.CdPing { + continue + } + var joinData common.JoinData err = json.Unmarshal([]byte(data.Message), &joinData) if err != nil { - common.LogInfof("[handler] Could not unmarshal join data %#v: %v\n", data.Message, err) + common.LogInfof("[handler] Could not unmarshal websocket %d data %#v: %v\n", data.Type, data.Message, err) continue } @@ -315,14 +319,6 @@ func handleEmoteTemplate(w http.ResponseWriter, r *http.Request) { } func handleIndexTemplate(w http.ResponseWriter, r *http.Request) { - if settings.RoomAccess != AccessOpen { - if !checkRoomAccess(w, r) { - common.LogDebugln("Denied access") - return - } - common.LogDebugln("Granted access") - } - type Data struct { Video, Chat bool MessageHistoryCount int @@ -473,3 +469,16 @@ func handleDefault(w http.ResponseWriter, r *http.Request) { handleIndexTemplate(w, r) } } + +func wrapAuth(next http.HandlerFunc) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if settings.RoomAccess != AccessOpen { + if !checkRoomAccess(w, r) { + common.LogDebugln("Denied access") + return + } + common.LogDebugln("Granted access") + } + next.ServeHTTP(w, r) + }) +} diff --git a/main.go b/main.go index d808005..5f13167 100644 --- a/main.go +++ b/main.go @@ -142,16 +142,17 @@ func run(args args) { router := http.NewServeMux() - router.HandleFunc("/ws", wsHandler) // Chat websocket router.Handle("/static/", http.FileServer(http.FS(staticFsys))) router.HandleFunc("/emotes/", wsEmotes) - router.HandleFunc("/chat", handleIndexTemplate) - router.HandleFunc("/video", handleIndexTemplate) - router.HandleFunc("/help", handleHelpTemplate) - router.HandleFunc("/emotes", handleEmoteTemplate) - router.HandleFunc("/live", handleLive) - router.HandleFunc("/", handleDefault) + router.HandleFunc("/ws", wrapAuth(wsHandler)) // Chat websocket + router.HandleFunc("/chat", wrapAuth(handleIndexTemplate)) + router.HandleFunc("/video", wrapAuth(handleIndexTemplate)) + router.HandleFunc("/help", wrapAuth(handleHelpTemplate)) + router.HandleFunc("/emotes", wrapAuth(handleEmoteTemplate)) + + router.HandleFunc("/live", wrapAuth(handleLive)) + router.HandleFunc("/", wrapAuth(handleDefault)) httpServer := &http.Server{ Addr: args.Addr, diff --git a/main_test.go b/ui_test.go similarity index 99% rename from main_test.go rename to ui_test.go index cafe85d..b75c8d7 100644 --- a/main_test.go +++ b/ui_test.go @@ -1,3 +1,5 @@ +//go:build test_ui + package main import (