Comments (9)
Since routing is now performed before the middleware stack, you can now do this yourself! Just take a peek at the matched Handler
(by calling https://godoc.org/goji.io/middleware#Handler), and if it's nil
, no route was matched, and a 404 should be served.
from goji.
Cool. thanks!
from goji.
This does not work well when you have SubMix. For example:
rootMux := goji.NewMux()
v1Mux :=goji.SubMux()
rootMux.HandleC(pat.New("/v1/*"), v1Mux)
v1Mux.HandleC(pat.Get("/foo"), FooHandler)
Then there seems no way to return a customized 404 for /v1/bar
since middleware.Handler
will return a non-nil handler (which is v1Mux
);
Any ideas?
from goji.
looks like the hacky way around the problem is to apply the 404 middleware to your sub muxes as well
rootMux := goji.NewMux()
v1Mux :=goji.SubMux()
rootMux.UseC(middleware.Apply404)
v1Mux.UseC(middleware.Apply404)
rootMux.HandleC(pat.New("/v1/*"), v1Mux)
v1Mux.HandleC(pat.Get("/foo"), FooHandler)
Like you mentioned, while
Line 14 in 9798f39
Line 52 in 9798f39
Let me know if you found a better way to tackle this?
@zenazn thoughts?
from goji.
@saj1th The workaround is rather ugly since you need to apply the middleware to all sub muxes. It seems the right way to do it is to enhance Mux
to provide a hook so we can supply a function to override the default NotFound
behavior.
@zenazn Any other suggestions?
from goji.
You can write an abstraction to create new Mux
's with a NotFound
middleware attached, instead of using goji.NewMux
. If you want to cast a wider net, you can also write a http.ResponseWriter
implementation that catches 404 status codes and instead renders a different 404.
Trying to auto-detect SubMux
es and do something more clever sacrifices the principle of least surprise, however, since it means the two snippets below would behave differently—there is no way to determine if a given goji.Handler
logically implements a SubMux
:
rootMux := goji.NewMux()
v1Mux := goji.SubMux()
rootMux.HandleC(pat.New("/v1/*"), v1Mux)
v1Mux.HandleC(pat.Get("/foo"), FooHandler)
rootMux := goji.NewMux()
v1Mux := goji.SubMux()
shim := func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
v1Mux.ServeHTTPC(ctx, w, r)
}
rootMux.HandleC(pat.New("/v1/*"), goji.HandlerFunc(shim))
v1Mux.HandleC(pat.Get("/foo"), FooHandler)
from goji.
For prior art on a http.ResponseWriter
shim, I wrote a reasonable implementation as part of the previous version of Goji: https://godoc.org/github.com/zenazn/goji/web/mutil. The code is based entirely on the net/http
interfaces, however, and can be reused with any mux of your choice, including this version of Goji.
from goji.
@zenazn I understand your concerns, however, it feels to me that such a basic need should be directly supported by the framework, instead of asking everyone to duplicate the work.
from goji.
Even if Goji provided some kind of NotFound
capability, it would have to be manually applied to every Mux
, which doesn't seem like a win to me. Is there something I'm missing?
from goji.
Related Issues (20)
- Problem in running goji "Hello World" HOT 2
- How to get all request parameters in handler?
- Please tag/release the 1.7+ master branch HOT 1
- middleware.Handler(rq.Context()) returns nil HOT 1
- Can't install goji HOT 3
- Missing Object.assing method HOT 1
- go1.7.4 and go1.8: I try to unit test, but panic on pat.Param HOT 1
- Cannot install using glide or gb HOT 7
- go get issue HOT 2
- dispatch.go:6:2: use of internal package not allowed HOT 1
- Rate Limit HOT 1
- Handling application panics and serving a 500 error page HOT 2
- Could pat.Param not panic when it the interface is nil HOT 4
- go modules support ? HOT 5
- [Proposal] consider making `newWithMethods` accessible function HOT 3
- git tags suggest v2, but go.mod disagrees HOT 4
- Nicer errors on requests? HOT 1
- Are patterns broken inside middleware? HOT 1
- how to HTTPS/TLS Web Server with goji ? HOT 1
- Is there a way to pass the regexp that was available in the old goji? HOT 2
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 goji.