Is there a mux that handles functions with extra parameters? I don't want to do the boilerplate code to convert MyHandler to http.Handler if I can avoid it. This code would probably be similar to how Spring Boot controllers and Django urldispatchers work.
package main
import (
"custom"
"fmt"
)
type MyParam struct {
Name string `param:"name", loc:"url"`
Date time.Time `param:"date"`
Custom custom.Type `param:"custom" loc:"query"` // custom.Type implements something like encoding.TextUnmarshaler
}
func MyHandler(w http.ResponseWriter, r *http.Request, p MyParam) {
}
var mux MuxIamAskingFor
func main() {
fmt.Println("Hello, playground")
mux.Handle("/asdf/(?P<Name>)/(?P<Date>)/", MyHandler) // With ?custom=parameter
}
The server generator from goswagger generates code with the parameter as struct. Is there a net/http(ish) mux that does the same. Reflection and/or code generators would be acceptable.
评论:
aboukirev:
hannson:Here's a couple that stick to
net/http
standard but provide flexible routing. Both are quality libraries.
metamatic:I've been using chi but a lot of my handlers share similar code just to convert parameters to their intended types.
I'm looking for a router that does that for me and gives me the params in a struct.
dean_karn:parth will probably handle your parameter conversion needs.
if using https://github.com/go-playground/pure (disclaimer I am author) it has a helper to decode these SEO params as I call them into a struct https://gist.github.com/joeybloggs/c610086166130d4f5d78ea082916b6c2
Please note this will decode non-SEO query params as well such as ?id=13; I will be adding another helper today just to decode the SEO query params.
