|
1 | 1 | package router |
2 | 2 |
|
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | +) |
| 7 | + |
| 8 | +// NewEntry 行健条目 |
| 9 | +func NewEntry(path, method, resource string) *Entry { |
| 10 | + return &Entry{ |
| 11 | + Path: path, |
| 12 | + Method: method, |
| 13 | + Resource: resource, |
| 14 | + Labels: map[string]string{}, |
| 15 | + } |
| 16 | +} |
| 17 | + |
3 | 18 | // Entry 路由条目 |
4 | 19 | type Entry struct { |
5 | | - Path string `json:"path,omitempty"` |
6 | | - Method string `json:"method,omitempty"` |
7 | | - FunctionName string `json:"function_name,omitempty"` |
8 | | - Resource string `json:"resource,omitempty"` |
9 | | - AuthEnable bool `json:"auth_enable"` |
10 | | - PermissionEnable bool `json:"permission_enable"` |
11 | | - Labels map[string]string `json:"labels,omitempty"` |
| 20 | + Path string `bson:"path" json:"path,omitempty"` |
| 21 | + Method string `bson:"method" json:"method,omitempty"` |
| 22 | + FunctionName string `bson:"function_name" json:"function_name,omitempty"` |
| 23 | + Resource string `bson:"resource" json:"resource,omitempty"` |
| 24 | + AuthEnable bool `bson:"auth_enable" json:"auth_enable"` |
| 25 | + PermissionEnable bool `bson:"permission_enable" json:"permission_enable"` |
| 26 | + Labels map[string]string `bson:"labels" json:"labels,omitempty"` |
| 27 | +} |
| 28 | + |
| 29 | +func (e *Entry) String() string { |
| 30 | + return fmt.Sprintf("%s [%s] %s %v", e.Path, e.Method, e.Resource, e.Labels) |
12 | 31 | } |
13 | 32 |
|
14 | 33 | // AddLabel 添加Label |
@@ -54,9 +73,20 @@ type EntrySet struct { |
54 | 73 | Items []*Entry `json:"items"` |
55 | 74 | } |
56 | 75 |
|
| 76 | +func (s *EntrySet) String() string { |
| 77 | + strs := []string{} |
| 78 | + for i := range s.Items { |
| 79 | + strs = append(strs, s.Items[i].String()) |
| 80 | + } |
| 81 | + |
| 82 | + return strings.Join(strs, "\n") |
| 83 | +} |
| 84 | + |
57 | 85 | // AddEntry 添加Entry |
58 | | -func (s *EntrySet) AddEntry(e Entry) { |
59 | | - s.Items = append(s.Items, &e) |
| 86 | +func (s *EntrySet) AddEntry(es ...Entry) { |
| 87 | + for i := range es { |
| 88 | + s.Items = append(s.Items, &es[i]) |
| 89 | + } |
60 | 90 | } |
61 | 91 |
|
62 | 92 | // GetEntry 获取条目 |
|
0 commit comments