Skip to content

Commit

Permalink
Merge pull request #47 from muir/endpoint
Browse files Browse the repository at this point in the history
feat: add Endpoint type to provide access to the path
  • Loading branch information
muir authored Feb 16, 2023
2 parents b32006e + e5edd18 commit a210f25
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.16.x, 1.17.x, 1.18.x]
go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
23 changes: 18 additions & 5 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ import (

type Params = httprouter.Params

// Endpoint is a type that handlers can accepte as an input. It will be the
// combined URL path without path variables substituted. If you have
//
// mux.Get("/thing/:thingID", handler)
//
// and handler takes an nchi.Endpoint argument, and there is a request for
// http://example.com/thing/3802, then the nchi.Endpoint will be "/thing/:thingID".
type Endpoint string

type Mux struct {
providers *nject.Collection // partial set
routes []*Mux
Expand Down Expand Up @@ -86,30 +95,34 @@ func (mux *Mux) Bind() error {
for _, opt := range mux.options {
opt(&rtr{router})
}
err := mux.bind(router, "", nject.Sequence("empty"))
err := mux.bind(router, "")
if err != nil {
return err
}
mux.router = router
return nil
}

func (mux *Mux) bind(router *httprouter.Router, path string, providers *nject.Collection) error {
func (mux *Mux) bind(router *httprouter.Router, path string) error {
combinedPath := path + mux.path
for _, route := range mux.routes {
err := route.bind(router, combinedPath, mux.providers)
err := route.bind(router, combinedPath)
if err != nil {
return err
}
}
providers := nject.Sequence(path,
Endpoint(combinedPath),
mux.providers,
)
if mux.special != nil {
return mux.bindSpecial(router, combinedPath, mux.providers)
return mux.bindSpecial(router, combinedPath, providers)
}
if mux.method == "" {
return nil
}
var handle httprouter.Handle
err := mux.providers.Bind(&handle, nil)
err := providers.Bind(&handle, nil)
if err != nil {
return errors.Wrapf(err, "bind router %s %s", mux.method, combinedPath)
}
Expand Down
16 changes: 16 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,19 @@ func TestGroup(t *testing.T) {
{path: "/e", want: "a"},
})
}

func TestEndpoint(t *testing.T) {
mux := nchi.NewRouter()
mux.Get("/thing/:thingID", func(endpoint nchi.Endpoint, w http.ResponseWriter) {
_, _ = w.Write([]byte(endpoint))
})
w := httptest.NewRecorder()

r := httptest.NewRequest("GET", "/thing/473", nil)
mux.ServeHTTP(w, r)
body, err := io.ReadAll(w.Result().Body)
assert.NoError(t, err)
got := string(body)
t.Log("->", got)
assert.Equal(t, "/thing/:thingID", got)
}

0 comments on commit a210f25

Please sign in to comment.