-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodels.go
More file actions
56 lines (50 loc) · 1.32 KB
/
models.go
File metadata and controls
56 lines (50 loc) · 1.32 KB
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
package server
import (
"errors"
//log "github.com/Sirupsen/logrus"
"github.com/microbay/server/backends"
"github.com/microbay/server/plugin"
"net/http"
"regexp"
)
type API struct {
Name string `json:"name"`
Portal string `json:"portal"`
Resources []*Resource `json:"resources"`
plugins map[string]map[string]interface{}
}
func (a *API) FindResourceByRequest(req *http.Request) (*Resource, error) {
for _, resource := range a.Resources {
if resource.Regex.MatchString(req.URL.Path) == true {
if stringInSlice(req.Method, resource.Methods) {
return resource, nil
} else {
return resource, errors.New("Method")
}
}
}
return nil, errors.New("Resource")
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
type Resource struct {
Auth string `json:"auth"`
Path string `json:"path"`
Methods []string `json:"methods"`
Micros map[string][]Micro `json:"micros"`
Plugins []map[string]interface{} `json:"plugins"`
Middleware []plugin.Interface
Backends map[string]backends.Backends // Load balancer
Regex *regexp.Regexp
Keys []string
}
type Micro struct {
URL string `json:"url"`
Weight int `json:"weight"`
}