Skip to content

Commit

Permalink
Merge pull request #23 from ksysoev/refactor_backend_inteface
Browse files Browse the repository at this point in the history
Refactor backend inteface
  • Loading branch information
ksysoev committed Apr 27, 2024
2 parents bc9b61d + 90d3872 commit 614970b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 12 deletions.
1 change: 0 additions & 1 deletion .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ packages:
github.com/ksysoev/wasabi:
interfaces:
Dispatcher:
Backend:
RequestHandler:
Connection:
Request:
Expand Down
4 changes: 2 additions & 2 deletions dispatch/pipe_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
// PipeDispatcher is a dispatcher that does not support any routing of requests
// but for single backend API gateways is enough
type PipeDispatcher struct {
backend wasabi.Backend
backend wasabi.RequestHandler
middlewares []RequestMiddlewere
}

// NewPipeDispatcher creates new instance of PipeDispatcher
func NewPipeDispatcher(backend wasabi.Backend) *PipeDispatcher {
func NewPipeDispatcher(backend wasabi.RequestHandler) *PipeDispatcher {
return &PipeDispatcher{backend: backend}
}

Expand Down
10 changes: 5 additions & 5 deletions dispatch/router_dipatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
)

type RouterDispatcher struct {
defaultBackend wasabi.Backend
backendMap map[string]wasabi.Backend
defaultBackend wasabi.RequestHandler
backendMap map[string]wasabi.RequestHandler
parser RequestParser
middlewares []RequestMiddlewere
}
Expand All @@ -18,17 +18,17 @@ type RouterDispatcher struct {
// It takes a defaultBackend and a request parser as parameters and returns a pointer to RouterDispatcher.
// The defaultBackend parameter is the default backend to be used when no specific backend is found.
// The parser parameter is used to parse incoming requests.
func NewRouterDispatcher(defaultBackend wasabi.Backend, parser RequestParser) *RouterDispatcher {
func NewRouterDispatcher(defaultBackend wasabi.RequestHandler, parser RequestParser) *RouterDispatcher {
return &RouterDispatcher{
defaultBackend: defaultBackend,
backendMap: make(map[string]wasabi.Backend),
backendMap: make(map[string]wasabi.RequestHandler),
parser: parser,
}
}

// AddBackend adds a backend to the RouterDispatcher for the specified routing keys.
// If a backend already exists for any of the routing keys, an error is returned.
func (d *RouterDispatcher) AddBackend(backend wasabi.Backend, routingKeys []string) error {
func (d *RouterDispatcher) AddBackend(backend wasabi.RequestHandler, routingKeys []string) error {
for _, key := range routingKeys {
if _, ok := d.backendMap[key]; ok {
return fmt.Errorf("backend for routing key %s already exists", key)
Expand Down
41 changes: 41 additions & 0 deletions examples/echo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"context"
"fmt"
"log/slog"
_ "net/http/pprof"
"os"

"github.com/ksysoev/wasabi"
"github.com/ksysoev/wasabi/channel"
"github.com/ksysoev/wasabi/dispatch"
"github.com/ksysoev/wasabi/server"
)

const (
Addr = ":8080"
)

func main() {

slog.LogAttrs(context.Background(), slog.LevelDebug, "")

backend := dispatch.RequestHandlerFunc(func(conn wasabi.Connection, req wasabi.Request) error {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

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

server := server.NewServer(Addr, server.WithBaseContext(context.Background()))
server.AddChannel(channel)

if err := server.Run(); err != nil {
slog.Error("Fail to start app server", "error", err)
os.Exit(1)
}

fmt.Println("Server is stopped")
os.Exit(0)
}
4 changes: 0 additions & 4 deletions interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ type Request interface {
WithContext(ctx context.Context) Request
}

type Backend interface {
Handle(conn Connection, r Request) error
}

// Dispatcher is interface for dispatchers
type Dispatcher interface {
Dispatch(conn Connection, msgType MessageType, data []byte)
Expand Down

0 comments on commit 614970b

Please sign in to comment.