1
0
mirror of https://github.com/ggicci/httpin.git synced 2024-12-02 09:01:33 +02:00
httpin/gochi.go
2021-07-19 14:31:31 +08:00

30 lines
687 B
Go

// URLParam extension for github.com/go-chi/chi.
package httpin
import "net/http"
// GochiURLParamFunc is chi.URLParam
type GochiURLParamFunc func(r *http.Request, key string) string
func UseGochiURLParam(executor string, fn GochiURLParamFunc) {
RegisterDirectiveExecutor(executor, &gochiURLParamExtractor{URLParam: fn}, nil)
}
type gochiURLParamExtractor struct {
URLParam GochiURLParamFunc
}
func (chi *gochiURLParamExtractor) Execute(ctx *DirectiveContext) error {
var kvs = make(map[string][]string)
for _, key := range ctx.Argv {
value := chi.URLParam(ctx.Request, key)
if value != "" {
kvs[key] = []string{value}
}
}
return extractFromKVS(ctx, kvs, false)
}