httprouter icon indicating copy to clipboard operation
httprouter copied to clipboard

Flexible Handle Type

Open rkusa opened this issue 9 years ago • 8 comments

What do you think about making the Lookup method (or a similar one) more generic in terms of not enforcing a specific function footprint?

Currently, when building something on top of httprouter, each handler has to be a func(w http.ResponseWriter, r *http.Request, _ httprouter.Params). It would be nice if the Lookup method (or a similar one) returns an interface{}, while httprouter itself still enforces the httprouter.Handle type for its own public API. This would allow using custom handle types for modules build on top of httprouter.

rkusa avatar Oct 14 '14 10:10 rkusa

I think matching the method signature with the standard http.Handler interface would be ideal. Possibly when there are parameters you could do something similar to other packages and use the querystring with keys like ":querystringkey" ?

CoreyKaylor avatar Oct 26 '14 16:10 CoreyKaylor

+1 for matching http.Handler

tj avatar Nov 27 '14 06:11 tj

We implemented this on top of httprouter here: https://github.com/nbio/hitch

It uses per-request contexts from: https://github.com/nbio/httpcontext

ydnar avatar Jan 06 '15 02:01 ydnar

I like it the way it is. With closures, it's already plenty flexible enough IMHO.

rogpeppe avatar Feb 05 '15 14:02 rogpeppe

Another thought: if httprouter mirrored the http package and used a Handler type instead of the Handle function type:

type Handler interface { ServeHTTPRoute(http.RequestWriter, *http.Request, Params) }

and the underlying storage was changed to store Handler rather than Handle, then we'd get the above desired functionality automatically, because it would be possible for callers to type-assert the returned Handler to some other type.

Interface types can be nicer to debug too, being less opaque than closures.

rogpeppe avatar Feb 06 '15 07:02 rogpeppe

I'd love this implemented too.

microamp avatar Jul 21 '15 15:07 microamp

It is possible to wrap request body as interface. Allocation could be avoided if parameters would be built with each route on initialization

See the example:

package vanilla

// based on https://github.com/nbio/httpcontext
// authored by http://nb.io https://github.com/nbio.
// MIT License

import (
    "io"
    "net/http"
)

// Param is a single URL parameter, consisting of a key and a value.
type Param struct {
    Key   string
    Value string
}

// Params is a Param-slice, as returned by the router.
// The slice is ordered, the first URL parameter is also the first slice value.
// It is therefore safe to read values by the index.
type Params []Param

// ByName returns the value of the first Param which key matches the given name.
// If no matching Param is found, an empty string is returned.
func (ps Params) ByName(name string) string {
    for i := range ps {
        if ps[i].Key == name {
            return ps[i].Value
        }
    }
    return ""
}

// Parameters returns all path parameters for given
// request.
//
// If there were no parameters and route is static
// then nil is returned.
func Parameters(req *http.Request) Params {
    return parameterized(req).get()
}

type paramReadCloser interface {
    io.ReadCloser
    get() Params
    set(Params)
}

type parameters struct {
    io.ReadCloser
    all Params
}

func (p *parameters) get() Params {
    return p.all
}

func (p *parameters) set(params Params) {
    p.all = params
}

func parameterized(req *http.Request) paramReadCloser {
    p, ok := req.Body.(paramReadCloser)
    if !ok {
        p = &parameters{ReadCloser: req.Body}
        req.Body = p
    }
    return p
}

And usage example would be:

router.GET("/all/*node", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
        fmt.Fprintf(w, "got var: "+Parameters(req).ByName("node"))
}))

l3pp4rd avatar Dec 09 '15 17:12 l3pp4rd

@tj guess you should be very excited to see this https://github.com/DATA-DOG/fastroute have a look at Router interface

maybe it is not the right place to add such a link, but well, worth it..

l3pp4rd avatar May 19 '17 08:05 l3pp4rd