Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking β€œSign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract common websocket logic #626

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Abstract common websocket logic
StevenWeathers committed Sep 24, 2024
commit 5193242c5e7d8836f8c7c96e078b51e945190ce4
32 changes: 15 additions & 17 deletions internal/http/checkin/checkin.go
Original file line number Diff line number Diff line change
@@ -3,8 +3,8 @@ package checkin
import (
"context"
"net/http"
"time"

"github.com/StevenWeathers/thunderdome-planning-poker/internal/wshub"
"github.com/StevenWeathers/thunderdome-planning-poker/thunderdome"
"github.com/uptrace/opentelemetry-go-extra/otelzap"
)
@@ -26,18 +26,6 @@ type Config struct {
WebsocketSubdomain string
}

func (c *Config) WriteWait() time.Duration {
return time.Duration(c.WriteWaitSec) * time.Second
}

func (c *Config) PingPeriod() time.Duration {
return time.Duration(c.PingPeriodSec) * time.Second
}

func (c *Config) PongWait() time.Duration {
return time.Duration(c.PongWaitSec) * time.Second
}

type CheckinDataSvc interface {
CheckinList(ctx context.Context, TeamId string, Date string, TimeZone string) ([]*thunderdome.TeamCheckin, error)
CheckinCreate(ctx context.Context, TeamId string, UserId string, Yesterday string, Today string, Blockers string, Discuss string, GoalsMet bool) error
@@ -68,11 +56,11 @@ type Service struct {
logger *otelzap.Logger
validateSessionCookie func(w http.ResponseWriter, r *http.Request) (string, error)
validateUserCookie func(w http.ResponseWriter, r *http.Request) (string, error)
eventHandlers map[string]func(context.Context, string, string, string) ([]byte, error, bool)
UserService UserDataSvc
AuthService AuthDataSvc
CheckinService CheckinDataSvc
TeamService TeamDataSvc
hub *wshub.Hub
}

// New returns a new retro with websocket hub/client and event handlers
@@ -95,16 +83,26 @@ func New(
TeamService: teamService,
}

c.eventHandlers = map[string]func(context.Context, string, string, string) ([]byte, error, bool){
c.hub = wshub.NewHub(logger, wshub.Config{
AppDomain: config.AppDomain,
WebsocketSubdomain: config.WebsocketSubdomain,
WriteWaitSec: config.WriteWaitSec,
PongWaitSec: config.PongWaitSec,
PingPeriodSec: config.PingPeriodSec,
}, map[string]func(context.Context, string, string, string) ([]byte, error, bool){
"checkin_create": c.CheckinCreate,
"checkin_update": c.CheckinUpdate,
"checkin_delete": c.CheckinDelete,
"comment_create": c.CommentCreate,
"comment_update": c.CommentUpdate,
"comment_delete": c.CommentDelete,
}
},
map[string]struct{}{},
nil,
nil,
)

go h.run()
go c.hub.Run()

return c
}
Loading