Skip to content

Commit

Permalink
Adds code example
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 7, 2024
1 parent 009b9fe commit 8a7d9aa
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion cmd/web/main.go → examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"

"github.com/ksysoev/wasabi"
"github.com/ksysoev/wasabi/middleware/request"
)

const (
Expand All @@ -19,7 +20,7 @@ func main() {

backend := wasabi.NewBackend(func(req wasabi.Request) (*http.Request, error) {
bodyReader := bytes.NewBufferString(string(req.Data()))
httpReq, err := http.NewRequest("GET", "http://localhost:8080/", bodyReader)
httpReq, err := http.NewRequest("GET", "http://localhost:8081/", bodyReader)
if err != nil {
return nil, err
}
Expand All @@ -29,8 +30,24 @@ func main() {
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 := wasabi.NewDefaultConnectionRegistry()
dispatcher := wasabi.NewPipeDispatcher(backend)
dispatcher.Use(ErrHandler)

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

Expand Down

0 comments on commit 8a7d9aa

Please sign in to comment.