Comments (6)
hey @lofcek, it's a good point and the trailing slash has been a bit of a pain. I added a test case 53adbc4 that can show you how to do it now. The reason this works is because routes are layered on top of each other when build the routing tree.
I do agree I should build something standard into the mux. However, there are usually two options, many people will want to redirect /accounts/123/ to /accounts/123 and others will want the trailing slash to be treated to the same handler.
one idea is instead of changing the Mux{} object, because, its more of an underlying struct and its best to use the Router interface, and we don't really support configuration options (not that we couldn't), but it limits extensibility..
I think instead... I could update the test code to support something like..
func TestMuxTrailingSlash(t *testing.T) {
r := NewRouter()
r.NotFound(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
w.Write([]byte("nothing here"))
})
subRoutes := NewRouter()
indexHandler := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
accountID := URLParam(ctx, "accountID")
w.Write([]byte(accountID))
}
subRoutes.Get("/", indexHandler)
// ***NOTE*** the middleware before the mount
r.Mount("/accounts/:accountID", middleware.StripSlashes, subRoutes)
// or..
// r.Mount("/accounts/:accountID", middleware.RedirectSlashes, subRoutes)
ts := httptest.NewServer(r)
defer ts.Close()
if resp := testRequest(t, ts, "GET", "/accounts/admin", nil); resp != "admin" {
t.Fatalf(resp)
}
if resp := testRequest(t, ts, "GET", "/accounts/admin/", nil); resp != "admin" {
t.Fatalf(resp)
}
if resp := testRequest(t, ts, "GET", "/nothing-here", nil); resp != "nothing here" {
t.Fatalf(resp)
}
}
so, the introduction is to add middleware.StripSlashes and middleware.RedirectSlashes
...alternatively, there could be an option on the chi routing Context, and a middleware could be used to flip the flags on the routing context, and pass along the request
from chi.
btw.. this works..
func StripSlashes(next Handler) Handler {
fn := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
if path[len(path)-1] == '/' {
rctx := RouteContext(ctx)
rctx.RoutePath = path[:len(path)-1]
}
next.ServeHTTPC(ctx, w, r)
}
return HandlerFunc(fn)
}
I'll be packaging this into middleware pkg soon enuf once I write the RedirectSlashes middleware, but I gotta head out for now
from chi.
That is nearly what I had, with few notes.
- Correct name should be StripTrailingSlash, because is it strips only one slash and only at the of the path.
- You should not simply use rctx.RoutePath = path[:len(path)-1]. That work correctly on root path, but not if RouteContext was already used (in subrouter after Mount).
- in if statement you forgot ctx = rctx
- I'm not sure, but is it correct if path refers to root (path = "/")
from chi.
And I also didn't realize it immediately- I think change data inside Context during execution is a bit confusing. I think it should be better to create a new chi.Context, don't change previous one.
from chi.
Thanks for the thoughts but I don't fully agree. You're right about the root path, I'll make a check but if you have other opinions, please submit a PR or a breaking test case. Ie. rctx is a pointer and doesn't need to set to ctx again, since it already on the context chain, and we don't need a new chi.Context
On Jun 20, 2016, at 7:30 AM, lofcek [email protected] wrote:
And I also didn't realize it immediately- I think change data inside Context during execution is a bit confusing. I think it should be better to create a new chi.Context, don't change previous one.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub, or mute the thread.
from chi.
thanks for the feedback. I've added support with b74aedb
from chi.
Related Issues (20)
- Feature: on Go 1.22 pattern can start with an optional HTTP Method HOT 5
- bug: HandlerFunc does not support HTTP methods in patterns like #897
- Huma not respecting API Gateway path? HOT 1
- r.With(paginate) in documentation HOT 1
- When registering a custom Middleware and a custom NotFoundHandler, both are executed when a 404 URL is loaded HOT 1
- Custom middleware recovery having to be duplicated HOT 1
- How to pass information back to middleware? HOT 3
- v5 response does not implement http.Hijacker HOT 2
- High CPU usage by chi.setPathValue HOT 2
- middleware.Compress should by default compress `text/xml`, `application/xml`, and arguably `text/*` HOT 3
- Special characters added to pages due to compression on v5.0.13 HOT 11
- Coraza WAF Middleware
- weird conflict of router HOT 2
- How to write 500 status code after `basicWriter.Write` returns error?
- Not all middlewares are present in `chi.Walk`
- Wildcard matching with regex pattern is not correctly parsing the path values HOT 1
- Chi router not invoking custom not found handler
- OpenTelemetry Middleware Proposal HOT 2
- Broken chi.URLParam return empty string
- Sub-routers returning 404 when using `middleware.SupressNotFound` on parent router
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chi.