Skip to content

Commit

Permalink
refactor(routing): routing based on the endpoint not domain x2
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Guerra authored and Gabriel Guerra committed Mar 22, 2024
1 parent 9a13441 commit 0682398
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
14 changes: 10 additions & 4 deletions internal/app/subsystems/aio/queuing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"html/template"
"net/http"
"strings"

"github.com/go-chi/chi/v5"
"github.com/resonatehq/resonate/internal/util"
Expand Down Expand Up @@ -55,16 +56,14 @@ func NewRouter() Router {
}

func (r *RouterImpl) Handle(pattern string, handler *RouteHandler) {
// chi routing pattern must begin with '/' and accept any domain.
pattern = "/" + "{http}://{domain}" + pattern

// chi routing pattern must begin with '/'. This is the behavior we want.
r.patterns.Post(pattern, func(w http.ResponseWriter, r *http.Request) {})
r.handlers[pattern] = handler
}

func (r *RouterImpl) Match(route string) (*MatchResult, error) {
// chi routing pattern must begin with '/'.
route = "/" + route
route = normalizeRoute(route)

rctx := chi.NewRouteContext()

Expand Down Expand Up @@ -110,3 +109,10 @@ func (r *RouterImpl) renderQueue(queueTemplate string, params chi.RouteParams) (

return buf.String(), nil
}

func normalizeRoute(route string) string {
if strings.HasPrefix(route, "/") {
return route
}
return "/" + route
}
45 changes: 14 additions & 31 deletions internal/app/subsystems/aio/queuing/routing_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package queuing

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,49 +17,29 @@ func TestRouting(t *testing.T) {
expectedErr error
}{
{
name: "simple .com",
name: "payment simple",
pattern: "/payments/*",
route: "http://demo.example.com/payments/123",
route: "payments/123", // promise id
expectedResult: &MatchResult{
Route: "/http://demo.example.com/payments/123",
RoutePattern: "/{http}://{domain}/payments/*",
Route: "/payments/123",
RoutePattern: "/payments/*",
Connection: "conn1",
},
},
{
name: "simple .io",
name: "payment simple with slash",
pattern: "/payments/*",
route: "http://demo.example.io/payments/123",
route: "/payments/123", // promise id
expectedResult: &MatchResult{
Route: "/http://demo.example.io/payments/123",
RoutePattern: "/{http}://{domain}/payments/*",
Route: "/payments/123",
RoutePattern: "/payments/*",
Connection: "conn1",
},
},
{
name: "simple localhost",
pattern: "/payments/*",
route: "http://localhost/payments/123",
expectedResult: &MatchResult{
Route: "/http://localhost/payments/123",
RoutePattern: "/{http}://{domain}/payments/*",
Connection: "conn1",
},
},
{
name: "simple localhost https",
pattern: "/payments/*",
route: "https://localhost/payments/123",
expectedResult: &MatchResult{
Route: "/https://localhost/payments/123",
RoutePattern: "/{http}://{domain}/payments/*",
Connection: "conn1",
},
},
{
name: "simple .com",
name: "absolute url does not work",
pattern: "/payments/*",
route: "http://demo.example.com/analytics/123",
route: "http://demo.example.com/payments/123",
expectedErr: ErrRouteDoesNotMatchAnyPattern,
},
}
Expand All @@ -72,11 +53,13 @@ func TestRouting(t *testing.T) {
})

result, err := router.Match(tc.route)
if tc.expectedErr != nil {
if err != nil {
assert.Equal(t, tc.expectedErr, err)
return
}

fmt.Printf("result: %+v\n", result)

assert.Equal(t, tc.expectedResult.Route, result.Route)
assert.Equal(t, tc.expectedResult.RoutePattern, result.RoutePattern)
assert.Equal(t, tc.expectedResult.Connection, result.Connection)
Expand Down

0 comments on commit 0682398

Please sign in to comment.