GithubHelp home page GithubHelp logo

NotFound Handler about goji HOT 9 CLOSED

goji avatar goji commented on June 5, 2024
NotFound Handler

from goji.

Comments (9)

zenazn avatar zenazn commented on June 5, 2024

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.

saj1th avatar saj1th commented on June 5, 2024

Cool. thanks!

from goji.

alexguan avatar alexguan commented on June 5, 2024

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.

saj1th avatar saj1th commented on June 5, 2024

@alexguan

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

if h == nil {
would get a nil handler,
if h == nil {
would not get nil as a *goji.Mux would be present.

Let me know if you found a better way to tackle this?

@zenazn thoughts?

from goji.

alexguan avatar alexguan commented on June 5, 2024

@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.

zenazn avatar zenazn commented on June 5, 2024

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 SubMuxes 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.

zenazn avatar zenazn commented on June 5, 2024

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.

alexguan avatar alexguan commented on June 5, 2024

@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.

zenazn avatar zenazn commented on June 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.