-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeployment-get-demo.go
149 lines (119 loc) · 3.18 KB
/
deployment-get-demo.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"github.com/kataras/iris"
"sort"
)
type lessFunc func(d1, d2 *Deployment) bool
type deploymentSorter struct {
deployments []Deployment
less []lessFunc
}
func (ds *deploymentSorter) Sort(deployments []Deployment) {
ds.deployments = deployments
sort.Sort(ds)
}
func (ds *deploymentSorter) Len() int {
return len(ds.deployments)
}
func (ds *deploymentSorter) Swap(i, j int) {
ds.deployments[i], ds.deployments[j] = ds.deployments[j], ds.deployments[i]
}
func (ds *deploymentSorter) Less(i, j int) bool {
p, q := &ds.deployments[i], &ds.deployments[j]
var k int
// why don't len(ds.less) ?
for k = 0; k < len(ds.less)-1; k++ {
less := ds.less[k]
switch {
case less(p, q):
return true
case less(q, p):
return false
}
}
return ds.less[k](p, q)
}
type ListDeploymentController struct {
*deploymentSorter
*iris.Context
}
func (ldc *ListDeploymentController) OrderedBy(less ...lessFunc) *deploymentSorter {
return &deploymentSorter{
less: less,
}
}
type Deployment struct {
userName string
dcId int
deploymentName string
updateTime string
}
type Deployments struct {
deployments []Deployment
}
func (lc *ListDeploymentController) GetDeploymentListAndSort() {
data := new(Deployments)
data.deployments = []Deployment{
{"maxwell", 1, "nginx", "20170207"},
{"maxwell", 2, "busybox", "20170209"},
{"sheep", 1, "busybox", "20170206"},
{"magic", 1, "yce", "20170301"},
}
userName := func(d1, d2 *Deployment) bool {
return d1.userName < d2.userName
}
deploymentName := func(d1, d2 *Deployment) bool {
return d1.deploymentName < d2.deploymentName
}
dcId := func(d1, d2 *Deployment) bool {
return d1.dcId < d2.dcId
}
updateTime := func(d1, d2 *Deployment) bool {
return d1.updateTime > d2.updateTime
}
//lc := new(ListDeploymentController)
lc.OrderedBy(userName).Sort(data.deployments)
fmt.Println("by userName:", data.deployments)
lc.OrderedBy(dcId).Sort(data.deployments)
fmt.Println("by dcId:", data.deployments)
lc.OrderedBy(userName, deploymentName).Sort(data.deployments)
fmt.Println("by userName, deploymentName:", data.deployments)
lc.OrderedBy(updateTime, deploymentName).Sort(data.deployments)
fmt.Println("by updateTime, deploymentName:", data.deployments)
}
func (lc ListDeploymentController) ValidateSession() {
fmt.Println(lc.RequestHeader("Authorization"))
}
/*
type DeploymentData struct {
UserId int32 `json:"userId"`
OrgId int32 `json:"orgId"`
OrgName string `json:"orgName"`
}
*/
func (lc ListDeploymentController) GetNamespace() {
/*
data := new(DeploymentData)
err := lc.ReadJSON(data)
if err != nil {
fmt.Println(err)
}
fmt.Printf("%v\n", data)
*/
fmt.Println(lc.RequestHeader("userId"))
fmt.Println(lc.RequestHeader("orgId"))
fmt.Println(lc.RequestHeader("orgName"))
}
func (lc ListDeploymentController) Get() {
lc.ValidateSession()
lc.GetNamespace()
lc.GetDeploymentListAndSort()
}
func main() {
lc := new(ListDeploymentController)
iris.API("/api/v2/deployments", *lc)
iris.Listen(":8081")
}
// test curl:
// curl -XGET -H "Authorization":"abcdefg-1234567" -H "userId":"1" -H "orgId":"2" -H "orgName":"dev" localhost:8081/api/v2/deployments