fix: align SSE architecture with portigo for reliable connections
- Reorder HandleGameEvents to create NATS subscriptions before SSE - Use chi's middleware.NewWrapResponseWriter for proper http.Flusher support - Add slog-zerolog adapter for unified logging - Add ErrorLog to HTTP server for better error visibility - Change session Cookie.Secure to false for HTTP support - Change heartbeat from 15s to 10s - Remove ConnectionIndicator patching (was causing PatchElementsNoTargetsFound) The key fix was using chi's response writer wrapper which properly implements http.Flusher, allowing SSE data to be flushed immediately instead of being buffered.
This commit is contained in:
22
main.go
22
main.go
@@ -11,6 +11,12 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
slogzerolog "github.com/samber/slog-zerolog/v2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"github.com/ryanhamamura/games/config"
|
||||
"github.com/ryanhamamura/games/connect4"
|
||||
"github.com/ryanhamamura/games/db"
|
||||
@@ -21,11 +27,6 @@ import (
|
||||
"github.com/ryanhamamura/games/sessions"
|
||||
"github.com/ryanhamamura/games/snake"
|
||||
"github.com/ryanhamamura/games/version"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
//go:embed assets
|
||||
@@ -36,7 +37,12 @@ func main() {
|
||||
defer cancel()
|
||||
|
||||
cfg := config.Global
|
||||
logging.SetupLogger(cfg.Environment, cfg.LogLevel)
|
||||
zerologLogger := logging.SetupLogger(cfg.Environment, cfg.LogLevel)
|
||||
slog.SetDefault(slog.New(slogzerolog.Option{
|
||||
Level: slogzerolog.ZeroLogLeveler{Logger: zerologLogger},
|
||||
Logger: zerologLogger,
|
||||
NoTimestamp: true,
|
||||
}.NewZerologHandler()))
|
||||
|
||||
if err := run(ctx); err != nil && err != http.ErrServerClosed {
|
||||
log.Fatal().Err(err).Msg("server error")
|
||||
@@ -101,6 +107,10 @@ func run(ctx context.Context) error {
|
||||
BaseContext: func(l net.Listener) context.Context {
|
||||
return egctx
|
||||
},
|
||||
ErrorLog: slog.NewLogLogger(
|
||||
slog.Default().Handler(),
|
||||
slog.LevelError,
|
||||
),
|
||||
}
|
||||
|
||||
eg.Go(func() error {
|
||||
|
||||
Reference in New Issue
Block a user