Chi: Route http.Handler on specific HTTP method?

Created on 7 Apr 2017  路  5Comments  路  Source: go-chi/chi

Hey,

I love chi and how it works with regular the regular http.HandlerFunc function signature, however I'd love to be able to return error from my HTTP handlers.

Since the Get/Post/... methods on the router require the rather limiting http.HandlerFunc function and the generic Handle method (which does accept the http.Handler interface) only binds to all HTTP methods, I can't seem to figure out how to do this though.

Maybe I'm overseeing something, but wouldn't chi be a whole lot more flexible if the HTTP-method routing methods accepted a http.Handler interface instead, just like the middleware?

// HTTP-method routing along `pattern`
Connect(pattern string, h http.Handler)
Delete(pattern string, h http.Handler)
...

Here's what I'm trying to achieve, basically the same as described here.

type Handler func(w http.ResponseWriter, r *http.Request) error

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if err := h(w, r); err != nil {
        // handle returned error here.
    }
}

func main() {
   r := chi.NewRouter()
   r.Get("/foo", Handler(fooHandler))
}   

Most helpful comment

hey @dannyvankooten I really appreciate your gentle suggestions, and I finally see the point everyone is requesting. I'll see what I can come up with the support the pattern you described above.

All 5 comments

After searching through the issue list I see this is brought up more often, so this is probably a duplicate of #79 and #94.

To answer my own question, with chi's current interface it seems that we can do this.

type Handler func(w http.ResponseWriter, r *http.Request) error

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    if err := h(w, r); err != nil {
        // handle returned error here.
    }
}

func main() {
   r := chi.NewRouter()
   r.Get("/foo", http.HandlerFunc(Handler(fooHandler).ServeHTTP))
}   

I'd like to echo the sentiments in the other two issues about this though, it would make a lot of sense to use an interface instead of func in the router method signatures. Fingers crossed that will make it into a future version of chi! :crossed_fingers:

hey @dannyvankooten I really appreciate your gentle suggestions, and I finally see the point everyone is requesting. I'll see what I can come up with the support the pattern you described above.

That is great news @pkieltyka! I've slightly edited my initial message in this issue as I mixed up "function" and "interface" a few times.

solved by v3 release 02e6bbdfd14640d8c1beb15ec3a38140400fb5c2 - specifically, check out the new r.Method() and r.MethodFunc() routing methods

See complete CHANGELOG for v3: https://github.com/go-chi/chi/blob/master/CHANGELOG.md

Was this page helpful?
0 / 5 - 0 ratings

Related issues

leighmcculloch picture leighmcculloch  路  8Comments

gpopovic picture gpopovic  路  5Comments

rocanion picture rocanion  路  4Comments

jsadwith picture jsadwith  路  6Comments

rickb777 picture rickb777  路  12Comments