Skip to content

Commit

Permalink
Adds repo description
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 8, 2024
1 parent c3913f8 commit 341328f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 18 deletions.
95 changes: 93 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,93 @@
# wsapi-gw
Simple WebSocket API Gateway
# 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)

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.

## Installation

To install Wasabi, use the go get command:

```sh
go get github.com/ksysoev/wasabi
```

# Usage

Here's a basic example of how to use Wasabi to create a WebSocket API gateway:

```go
package main

import (
"bytes"
"context"
"net/http"
"os"

"github.com/ksysoev/wasabi"
"github.com/ksysoev/wasabi/backend"
"github.com/ksysoev/wasabi/channel"
"github.com/ksysoev/wasabi/dispatch"
"github.com/ksysoev/wasabi/middleware/request"
"github.com/ksysoev/wasabi/server"
)

const (
Port = 8080
)

func main() {
// We create a new backend with backend.NewBackend.
// This backend creates a new HTTP request for each incoming WebSocket message.
// The requests are sent to http://localhost:8081/.
backend := backend.NewBackend(func(req wasabi.Request) (*http.Request, error) {
httpReq, err := http.NewRequest("GET", "http://localhost:8081/", bytes.NewBuffer(req.Data()))
if err != nil {
return nil, err
}

return httpReq, nil
})

// We create an error handling middleware with request.NewErrorHandlingMiddleware.
// This middleware logs any errors that occur when handling a request and sends a response back to the client.
ErrHandler := request.NewErrorHandlingMiddleware(func(conn wasabi.Connection, req wasabi.Request, err error) error {
conn.Send([]byte("Failed to process request: " + err.Error()))
return nil
})

// We create a new dispatcher with dispatch.NewPipeDispatcher.
// This dispatcher sends/routes each incoming WebSocket message to the backend.
dispatcher := dispatch.NewPipeDispatcher(backend)
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(10))

// We create a new connection registry with channel.NewDefaultConnectionRegistry.
// This registry keeps track of all active connections.
connRegistry := channel.NewDefaultConnectionRegistry()

// 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.NewDefaultChannel("/", dispatcher, connRegistry)
server := server.NewServer(Port)
server.AddChannel(channel)

// Finally, we start the server with server.Run.
// If the server fails to start, we log the error and exit the program.
if err := server.Run(context.Background()); err != nil {
slog.Error("Fail to start app server", "error", err)
os.Exit(1)
}

os.Exit(0)
}
```

## Contributing

Contributions to Wasabi are welcome! Please submit a pull request or create an issue to contribute.

# License

Wasabi is licensed under the MIT License. See the LICENSE file for more details.
19 changes: 3 additions & 16 deletions examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,26 @@ func main() {
slog.LogAttrs(context.Background(), slog.LevelDebug, "")

backend := backend.NewBackend(func(req wasabi.Request) (*http.Request, error) {
bodyReader := bytes.NewBufferString(string(req.Data()))
httpReq, err := http.NewRequest("GET", "http://localhost:8081/", bodyReader)
httpReq, err := http.NewRequest("GET", "http://localhost:8081/", bytes.NewBuffer(req.Data()))
if err != nil {
return nil, err
}

httpReq.Header.Set("Content-Type", "application/json")

return httpReq, nil
})

ErrHandler := request.NewErrorHandlingMiddleware(func(conn wasabi.Connection, req wasabi.Request, err error) error {

if conn.Context().Err() != nil {
return nil
}

if req.Context().Err() == nil {
slog.Error("Error handling request", "error", err)
}

conn.Send([]byte("Failed to process request: " + err.Error()))
return nil
})

connRegistry := channel.NewDefaultConnectionRegistry()
dispatcher := dispatch.NewPipeDispatcher(backend)
dispatcher.Use(ErrHandler)
dispatcher.Use(request.NewTrottlerMiddleware(10))

server := server.NewServer(Port)
channel := channel.NewDefaultChannel("/", dispatcher, connRegistry)
channel := channel.NewDefaultChannel("/", dispatcher, channel.NewDefaultConnectionRegistry())

server := server.NewServer(Port)
server.AddChannel(channel)

if err := server.Run(context.Background()); err != nil {
Expand Down

0 comments on commit 341328f

Please sign in to comment.