From 470aa4459eb01f2c0c54dfcb17de8fdcaf568ba1 Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sat, 27 Apr 2024 16:12:59 +0800 Subject: [PATCH 1/2] interface of backend is duplicating interface of request handler and not needed --- .mockery.yaml | 1 - dispatch/pipe_dispatcher.go | 4 ++-- dispatch/router_dipatcher.go | 10 +++++----- interfaces.go | 4 ---- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/.mockery.yaml b/.mockery.yaml index e5dd330..a5d1c4d 100644 --- a/.mockery.yaml +++ b/.mockery.yaml @@ -7,7 +7,6 @@ packages: github.com/ksysoev/wasabi: interfaces: Dispatcher: - Backend: RequestHandler: Connection: Request: diff --git a/dispatch/pipe_dispatcher.go b/dispatch/pipe_dispatcher.go index 75f0be0..2b7a132 100644 --- a/dispatch/pipe_dispatcher.go +++ b/dispatch/pipe_dispatcher.go @@ -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} } diff --git a/dispatch/router_dipatcher.go b/dispatch/router_dipatcher.go index 4da386d..d8ac17b 100644 --- a/dispatch/router_dipatcher.go +++ b/dispatch/router_dipatcher.go @@ -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 } @@ -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) diff --git a/interfaces.go b/interfaces.go index 5c60637..2323ca9 100644 --- a/interfaces.go +++ b/interfaces.go @@ -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) From 90d387215d7b7dc72c4cea1effa72d4d9afd6f91 Mon Sep 17 00:00:00 2001 From: Kirill Sysoev Date: Sat, 27 Apr 2024 16:13:25 +0800 Subject: [PATCH 2/2] Adds echo implementation of WS server --- examples/echo/main.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 examples/echo/main.go diff --git a/examples/echo/main.go b/examples/echo/main.go new file mode 100644 index 0000000..60d379f --- /dev/null +++ b/examples/echo/main.go @@ -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) +}