Skip to content

Commit a210f25

Browse files
authored
Merge pull request #47 from muir/endpoint
feat: add Endpoint type to provide access to the path
2 parents b32006e + e5edd18 commit a210f25

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

.github/workflows/go.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
test:
88
strategy:
99
matrix:
10-
go-version: [1.16.x, 1.17.x, 1.18.x]
10+
go-version: [1.16.x, 1.17.x, 1.18.x, 1.19.x, 1.20.x]
1111
os: [ubuntu-latest, macos-latest, windows-latest]
1212
runs-on: ${{ matrix.os }}
1313
steps:

mux.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ import (
1111

1212
type Params = httprouter.Params
1313

14+
// Endpoint is a type that handlers can accepte as an input. It will be the
15+
// combined URL path without path variables substituted. If you have
16+
//
17+
// mux.Get("/thing/:thingID", handler)
18+
//
19+
// and handler takes an nchi.Endpoint argument, and there is a request for
20+
// http://example.com/thing/3802, then the nchi.Endpoint will be "/thing/:thingID".
21+
type Endpoint string
22+
1423
type Mux struct {
1524
providers *nject.Collection // partial set
1625
routes []*Mux
@@ -86,30 +95,34 @@ func (mux *Mux) Bind() error {
8695
for _, opt := range mux.options {
8796
opt(&rtr{router})
8897
}
89-
err := mux.bind(router, "", nject.Sequence("empty"))
98+
err := mux.bind(router, "")
9099
if err != nil {
91100
return err
92101
}
93102
mux.router = router
94103
return nil
95104
}
96105

97-
func (mux *Mux) bind(router *httprouter.Router, path string, providers *nject.Collection) error {
106+
func (mux *Mux) bind(router *httprouter.Router, path string) error {
98107
combinedPath := path + mux.path
99108
for _, route := range mux.routes {
100-
err := route.bind(router, combinedPath, mux.providers)
109+
err := route.bind(router, combinedPath)
101110
if err != nil {
102111
return err
103112
}
104113
}
114+
providers := nject.Sequence(path,
115+
Endpoint(combinedPath),
116+
mux.providers,
117+
)
105118
if mux.special != nil {
106-
return mux.bindSpecial(router, combinedPath, mux.providers)
119+
return mux.bindSpecial(router, combinedPath, providers)
107120
}
108121
if mux.method == "" {
109122
return nil
110123
}
111124
var handle httprouter.Handle
112-
err := mux.providers.Bind(&handle, nil)
125+
err := providers.Bind(&handle, nil)
113126
if err != nil {
114127
return errors.Wrapf(err, "bind router %s %s", mux.method, combinedPath)
115128
}

mux_test.go

+16
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,19 @@ func TestGroup(t *testing.T) {
134134
{path: "/e", want: "a"},
135135
})
136136
}
137+
138+
func TestEndpoint(t *testing.T) {
139+
mux := nchi.NewRouter()
140+
mux.Get("/thing/:thingID", func(endpoint nchi.Endpoint, w http.ResponseWriter) {
141+
_, _ = w.Write([]byte(endpoint))
142+
})
143+
w := httptest.NewRecorder()
144+
145+
r := httptest.NewRequest("GET", "/thing/473", nil)
146+
mux.ServeHTTP(w, r)
147+
body, err := io.ReadAll(w.Result().Body)
148+
assert.NoError(t, err)
149+
got := string(body)
150+
t.Log("->", got)
151+
assert.Equal(t, "/thing/:thingID", got)
152+
}

0 commit comments

Comments
 (0)