forked from ggicci/httpin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgorilla.go
More file actions
42 lines (35 loc) · 997 Bytes
/
gorilla.go
File metadata and controls
42 lines (35 loc) · 997 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Mux vars extension for github.com/gorilla/mux package.
package httpin
import (
"mime/multipart"
"net/http"
)
// GorillaMuxVarsFunc is mux.Vars
type GorillaMuxVarsFunc func(*http.Request) map[string]string
// UseGorillaMux registers a new directive executor which can extract values
// from `mux.Vars`, i.e. path variables.
// https://ggicci.github.io/httpin/integrations/gorilla
//
// Usage:
//
// func init() {
// httpin.UseGorillaMux("path", mux.Vars)
// }
func UseGorillaMux(executor string, fnVars GorillaMuxVarsFunc) {
RegisterDirectiveExecutor(executor, &gorillaMuxVarsExtractor{Vars: fnVars}, nil)
}
type gorillaMuxVarsExtractor struct {
Vars GorillaMuxVarsFunc
}
func (mux *gorillaMuxVarsExtractor) Execute(ctx *DirectiveContext) error {
var kvs = make(map[string][]string)
for key, value := range mux.Vars(ctx.Request) {
kvs[key] = []string{value}
}
extractor := &extractor{
Form: multipart.Form{
Value: kvs,
},
}
return extractor.Execute(ctx)
}