-
Notifications
You must be signed in to change notification settings - Fork 35
/
mux.go
60 lines (54 loc) · 1.15 KB
/
mux.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
53
54
55
56
57
58
59
60
package parse
import (
"net/http"
"github.com/gorilla/mux"
"github.com/rancher/apiserver/pkg/types"
)
type Vars struct {
Type string
Name string
Namespace string
Link string
Prefix string
Action string
}
func Set(v Vars) mux.MatcherFunc {
return func(request *http.Request, match *mux.RouteMatch) bool {
if match.Vars == nil {
match.Vars = map[string]string{}
}
if v.Type != "" {
match.Vars["type"] = v.Type
}
if v.Name != "" {
match.Vars["name"] = v.Name
}
if v.Link != "" {
match.Vars["link"] = v.Link
}
if v.Prefix != "" {
match.Vars["prefix"] = v.Prefix
}
if v.Action != "" {
match.Vars["action"] = v.Action
}
if v.Namespace != "" {
match.Vars["namespace"] = v.Namespace
}
return true
}
}
func MuxURLParser(rw http.ResponseWriter, req *http.Request, schemas *types.APISchemas) (ParsedURL, error) {
vars := mux.Vars(req)
url := ParsedURL{
Type: vars["type"],
Name: vars["name"],
Namespace: vars["namespace"],
Link: vars["link"],
Prefix: vars["prefix"],
Method: req.Method,
Action: vars["action"],
Query: req.URL.Query(),
}
return url, nil
}