fix: resolve all linting errors and add SSE compression
- Add brotli compression (level 5) to long-lived SSE event streams (HandleGameEvents, HandleSnakeEvents) to reduce wire payload - Fix all errcheck violations with nolint annotations for best-effort calls - Fix goimports: separate stdlib, third-party, and local import groups - Fix staticcheck: add package comments, use tagged switch - Zero lint issues remaining
This commit is contained in:
@@ -6,10 +6,11 @@ import (
|
||||
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
|
||||
"github.com/ryanhamamura/c4/auth"
|
||||
"github.com/ryanhamamura/c4/db/repository"
|
||||
"github.com/ryanhamamura/c4/features/auth/pages"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
)
|
||||
|
||||
type LoginSignals struct {
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
// Package auth handles user authentication routes and handlers.
|
||||
package auth
|
||||
|
||||
import (
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"github.com/ryanhamamura/c4/db/repository"
|
||||
)
|
||||
|
||||
|
||||
@@ -12,11 +12,12 @@ import (
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
|
||||
"github.com/ryanhamamura/c4/db/repository"
|
||||
"github.com/ryanhamamura/c4/features/c4game/components"
|
||||
"github.com/ryanhamamura/c4/features/c4game/pages"
|
||||
"github.com/ryanhamamura/c4/game"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
)
|
||||
|
||||
func HandleGamePage(store *game.GameStore, sessions *scs.SessionManager, queries *repository.Queries) http.HandlerFunc {
|
||||
@@ -102,7 +103,9 @@ func HandleGameEvents(store *game.GameStore, nc *nats.Conn, sessions *scs.Sessio
|
||||
|
||||
myColor := gi.GetPlayerColor(playerID)
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse := datastar.NewSSE(w, r, datastar.WithCompression(
|
||||
datastar.WithBrotli(datastar.WithBrotliLevel(5)),
|
||||
))
|
||||
|
||||
// Load initial chat messages
|
||||
chatMsgs := loadChatMessages(queries, gameID)
|
||||
@@ -118,7 +121,7 @@ func HandleGameEvents(store *game.GameStore, nc *nats.Conn, sessions *scs.Sessio
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer gameSub.Unsubscribe()
|
||||
defer gameSub.Unsubscribe() //nolint:errcheck
|
||||
|
||||
// Subscribe to chat messages
|
||||
chatCh := make(chan *nats.Msg, 64)
|
||||
@@ -126,7 +129,7 @@ func HandleGameEvents(store *game.GameStore, nc *nats.Conn, sessions *scs.Sessio
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer chatSub.Unsubscribe()
|
||||
defer chatSub.Unsubscribe() //nolint:errcheck
|
||||
|
||||
ctx := r.Context()
|
||||
for {
|
||||
@@ -263,7 +266,7 @@ func HandleSendChat(store *game.GameStore, nc *nats.Conn, sessions *scs.SessionM
|
||||
datastar.NewSSE(w, r)
|
||||
return
|
||||
}
|
||||
nc.Publish("game.chat."+gameID, data)
|
||||
nc.Publish("game.chat."+gameID, data) //nolint:errcheck
|
||||
|
||||
// Clear the chat input
|
||||
sse := datastar.NewSSE(w, r)
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// Package c4game handles Connect 4 game routes, SSE event streaming, and chat.
|
||||
package c4game
|
||||
|
||||
import (
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/ryanhamamura/c4/db/repository"
|
||||
"github.com/ryanhamamura/c4/game"
|
||||
)
|
||||
|
||||
@@ -91,7 +91,7 @@ func HandleCreateGame(store *game.GameStore, sessions *scs.SessionManager) http.
|
||||
|
||||
gi := store.Create()
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse.ExecuteScript(fmt.Sprintf("window.location.href='/game/%s'", gi.ID()))
|
||||
sse.ExecuteScript(fmt.Sprintf("window.location.href='/game/%s'", gi.ID())) //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
@@ -104,10 +104,10 @@ func HandleDeleteGame(store *game.GameStore, sessions *scs.SessionManager) http.
|
||||
return
|
||||
}
|
||||
|
||||
store.Delete(gameID)
|
||||
store.Delete(gameID) //nolint:errcheck
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse.ExecuteScript("window.location.href='/'")
|
||||
sse.ExecuteScript("window.location.href='/'") //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ func HandleCreateSnakeGame(snakeStore *snake.SnakeStore, sessions *scs.SessionMa
|
||||
si := snakeStore.Create(preset.Width, preset.Height, mode, speed)
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse.ExecuteScript(fmt.Sprintf("window.location.href='/snake/%s'", si.ID()))
|
||||
sse.ExecuteScript(fmt.Sprintf("window.location.href='/snake/%s'", si.ID())) //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +163,6 @@ func HandleLogout(sessions *scs.SessionManager) http.HandlerFunc {
|
||||
}
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse.ExecuteScript("window.location.href='/'")
|
||||
sse.ExecuteScript("window.location.href='/'") //nolint:errcheck
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// Package lobby handles the game lobby page, game creation, and navigation.
|
||||
package lobby
|
||||
|
||||
import (
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
|
||||
"github.com/ryanhamamura/c4/features/snakegame/components"
|
||||
"github.com/ryanhamamura/c4/features/snakegame/pages"
|
||||
"github.com/ryanhamamura/c4/game"
|
||||
"github.com/ryanhamamura/c4/snake"
|
||||
"github.com/starfederation/datastar-go/datastar"
|
||||
)
|
||||
|
||||
func getPlayerID(sessions *scs.SessionManager, r *http.Request) snake.PlayerID {
|
||||
@@ -90,7 +91,9 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sessions *sc
|
||||
playerID := getPlayerID(sessions, r)
|
||||
mySlot := si.GetPlayerSlot(playerID)
|
||||
|
||||
sse := datastar.NewSSE(w, r)
|
||||
sse := datastar.NewSSE(w, r, datastar.WithCompression(
|
||||
datastar.WithBrotli(datastar.WithBrotliLevel(5)),
|
||||
))
|
||||
|
||||
// Send initial render
|
||||
sg := si.GetGame()
|
||||
@@ -110,7 +113,7 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sessions *sc
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer gameSub.Unsubscribe()
|
||||
defer gameSub.Unsubscribe() //nolint:errcheck
|
||||
|
||||
// Chat subscription (multiplayer only)
|
||||
var chatCh chan *nats.Msg
|
||||
@@ -124,7 +127,7 @@ func HandleSnakeEvents(snakeStore *snake.SnakeStore, nc *nats.Conn, sessions *sc
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer chatSub.Unsubscribe()
|
||||
defer chatSub.Unsubscribe() //nolint:errcheck
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// Package snakegame handles snake game routes, SSE event streaming, and chat.
|
||||
package snakegame
|
||||
|
||||
import (
|
||||
"github.com/alexedwards/scs/v2"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/nats-io/nats.go"
|
||||
|
||||
"github.com/ryanhamamura/c4/snake"
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user