Skip to content

Commit

Permalink
Merge pull request #20 from ksysoev/pass_conn_registry_to_chan_as_param
Browse files Browse the repository at this point in the history
Returns back connection registry
  • Loading branch information
ksysoev committed Apr 24, 2024
2 parents 29c4119 + 639e978 commit 8f165ce
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 15 deletions.
1 change: 1 addition & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ packages:
Connection:
Request:
Channel:
ConnectionRegistry:
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ test:
go test -v --race ./...

lint:
golangci-lint run
golangci-lint run

mocks:
mockery --all --keeptree
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Wasabi: A Toolkit for Creating WebSocket API Gateway

[![WaSAbi](https://github.com/ksysoev/wasabi/actions/workflows/main.yml/badge.svg)](https://github.com/ksysoev/wasabi/actions/workflows/main.yml)
[![codecov](https://codecov.io/gh/ksysoev/wasabi/graph/badge.svg?token=3KGTO1UINI)](https://codecov.io/gh/ksysoev/wasabi)
[![CodeCov](https://codecov.io/gh/ksysoev/wasabi/graph/badge.svg?token=3KGTO1UINI)](https://codecov.io/gh/ksysoev/wasabi)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)

Wasabi is a Go package that provides a comprehensive toolkit for creating WebSocket API gateways. It provides a simple and intuitive API to build robust and scalable WebSocket applications.
Expand Down Expand Up @@ -67,9 +67,14 @@ func main() {
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(10))

// We create a new connection registry with channel.NewConnectionRegistry.
// This registry keeps track of all active connections
// and responsible for managing connection's settings.
connRegistry := channel.NewConnectionRegistry()

// We create a new server with wasabi.NewServer and add a channel to it with server.AddChannel.
// The server listens on port 8080 and the channel handles all requests to the / path.
channel := channel.NewChannel("/", dispatcher)
channel := channel.NewChannel("/", dispatcher, connRegistry)
server := server.NewServer(Port)
server.AddChannel(channel)

Expand Down
5 changes: 3 additions & 2 deletions channel/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
type Channel struct {
path string
disptacher wasabi.Dispatcher
connRegistry *ConnectionRegistry
connRegistry wasabi.ConnectionRegistry
ctx context.Context
middlewares []Middlewere
config channelConfig
Expand All @@ -33,6 +33,7 @@ type Option func(*channelConfig)
func NewChannel(
path string,
dispatcher wasabi.Dispatcher,
connRegistry wasabi.ConnectionRegistry,
opts ...Option,
) *Channel {
config := channelConfig{
Expand All @@ -46,7 +47,7 @@ func NewChannel(
return &Channel{
path: path,
disptacher: dispatcher,
connRegistry: NewConnectionRegistry(),
connRegistry: connRegistry,
middlewares: make([]Middlewere, 0),
config: config,
}
Expand Down
18 changes: 9 additions & 9 deletions channel/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestNewChannel(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if channel.path != path {
t.Errorf("Unexpected path: got %q, expected %q", channel.path, path)
Expand All @@ -31,7 +31,7 @@ func TestChannel_Path(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if channel.Path() != path {
t.Errorf("Unexpected path: got %q, expected %q", channel.Path(), path)
Expand All @@ -41,7 +41,7 @@ func TestChannel_Handler(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())
channel.SetContext(context.Background())

// Call the Handler method
Expand All @@ -55,7 +55,7 @@ func TestChannel_SetContext(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

ctx := context.Background()
channel.SetContext(ctx)
Expand All @@ -68,7 +68,7 @@ func TestChannel_Use(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -87,7 +87,7 @@ func TestChannel_wrapMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Create a mock handler
mockHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestChannel_SetContextMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

// Create a mock handler
var ctx context.Context
Expand Down Expand Up @@ -156,7 +156,7 @@ func TestChannel_WithOriginPatterns(t *testing.T) {
path := "/test/path"
dispatcher := mocks.NewMockDispatcher(t)

channel := NewChannel(path, dispatcher)
channel := NewChannel(path, dispatcher, NewConnectionRegistry())

if len(channel.config.originPatterns) != 1 {
t.Errorf("Unexpected number of origin patterns: got %d, expected %d", len(channel.config.originPatterns), 1)
Expand All @@ -166,7 +166,7 @@ func TestChannel_WithOriginPatterns(t *testing.T) {
t.Errorf("Unexpected to get default origin pattern: got %s, expected %s", channel.config.originPatterns[0], "*")
}

channel = NewChannel(path, dispatcher, WithOriginPatterns("test", "test2"))
channel = NewChannel(path, dispatcher, NewConnectionRegistry(), WithOriginPatterns("test", "test2"))

if len(channel.config.originPatterns) != 2 {
t.Errorf("Unexpected number of origin patterns: got %d, expected %d", len(channel.config.originPatterns), 1)
Expand Down
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(100))

channel := channel.NewChannel("/", dispatcher, channel.WithOriginPatterns("*"))
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))

server := server.NewServer(Port)
server.AddChannel(channel)
Expand Down
10 changes: 10 additions & 0 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ type Channel interface {
SetContext(ctx context.Context)
Handler() http.Handler
}

// ConnectionRegistry is interface for connection registries
type ConnectionRegistry interface {
AddConnection(
ctx context.Context,
ws *websocket.Conn,
cb OnMessage,
) Connection
GetConnection(id string) Connection
}
139 changes: 139 additions & 0 deletions mocks/mock_ConnectionRegistry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f165ce

Please sign in to comment.