-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gorilla.go
52 lines (43 loc) · 1.15 KB
/
gorilla.go
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
43
44
45
46
47
48
49
50
51
52
// Mux vars extension for github.com/gorilla/mux package.
package integration
import (
"mime/multipart"
"net/http"
"github.com/ggicci/httpin/core"
)
// 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:
//
// import httpin_integration "github.com/ggicci/httpin/integration"
//
// func init() {
// httpin_integration.UseGorillaMux("path", mux.Vars)
// }
func UseGorillaMux(name string, fnVars GorillaMuxVarsFunc) {
core.RegisterDirective(
name,
core.NewDirectivePath((&gorillaMuxVarsExtractor{Vars: fnVars}).Execute),
)
}
type gorillaMuxVarsExtractor struct {
Vars GorillaMuxVarsFunc
}
func (mux *gorillaMuxVarsExtractor) Execute(rtm *core.DirectiveRuntime) error {
req := rtm.GetRequest()
kvs := make(map[string][]string)
for key, value := range mux.Vars(req) {
kvs[key] = []string{value}
}
extractor := &core.FormExtractor{
Runtime: rtm,
Form: multipart.Form{
Value: kvs,
},
}
return extractor.Extract()
}