-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (126 loc) · 3.86 KB
/
main.go
File metadata and controls
138 lines (126 loc) · 3.86 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
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
package main
import (
"fmt"
"math/rand"
"net/http"
"github.com/flaviostutz/ruller"
"github.com/sirupsen/logrus"
)
func main() {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("====Starting Ruller Sample====")
err := ruller.Add("test", "rule1", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["opt1"] = "Some tests rule 1"
output["rule1-opt2"] = 129.99
rnd := fmt.Sprintf("v%d", rand.Int())
if ctx.Input["menu"] == true {
child := make(map[string]interface{})
child["rule1-c1"] = "123"
child["rule1-c2"] = rnd
output["menu"] = child
}
output["rule1"] = true
return output, nil
})
if err != nil {
panic(err)
}
ruller.AddRequiredInput("test", "age", ruller.Float64)
ruller.AddRequiredInput("test", "children", ruller.Bool)
err = ruller.AddChild("test", "rule1.1", "rule1", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["rule1.1-mydata"] = "myvalue"
output["rule1.1"] = true
return output, nil
})
if err != nil {
panic(err)
}
err = ruller.Add("test", "rule2", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["opt1"] = "Lots of tests rule 2"
output["rule2"] = true
// logrus.Debugf("children output from rule 2.1 is %s", ctx.ChildrenOutput)
return output, nil
})
if err != nil {
panic(err)
}
err = ruller.AddChild("test", "rule2.1", "rule2", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
age, ok := ctx.Input["age"].(float64)
if !ok {
return nil, fmt.Errorf("Invalid 'age' detected. age=%s", ctx.Input["age"])
}
if age > 60 {
output["category"] = "elder rule2.1"
} else {
output["category"] = "young rule2.1"
}
output["geo-city"] = ctx.Input["_ip_city"]
output["geo-state"] = ctx.Input["_ip_state"]
output["rule2.1"] = true
return output, nil
})
if err != nil {
panic(err)
}
ruller.SetRequestFilter(func(r *http.Request, input map[string]interface{}) error {
logrus.Debugf("filtering request. input=%s", input)
input["_something"] = "test"
return nil
})
ruller.SetResponseFilter(func(w http.ResponseWriter, input map[string]interface{}, output map[string]interface{}, outBytes []byte) (bool, error) {
logrus.Debugf("filtering response. input=%s", input)
output["filter-attribute"] = "added by sample filter"
if input["_something"] == "test" {
w.Write([]byte("{\"a\":"))
w.Write(outBytes)
w.Write([]byte("}"))
}
return true, nil
})
err = ruller.AddChild("test", "rule2.2", "rule2", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["rule2.2-type"] = "any"
output["rule2.2"] = true
return output, nil
})
if err != nil {
panic(err)
}
for a := 0; a < 10; a++ {
err = ruller.AddChild("test", fmt.Sprintf("rule2.2-%d", a), "rule2.2", func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["opt1"] = "any1"
return output, nil
})
if err != nil {
panic(err)
}
for b := 0; b < 1; b++ {
err = ruller.AddChild("test", fmt.Sprintf("rule2.2-%d-%d", a, b), fmt.Sprintf("rule2.2-%d", a), func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["opt2"] = "any2"
return output, nil
})
if err != nil {
panic(err)
}
for c := 0; c < 1; c++ {
err = ruller.AddChild("test", fmt.Sprintf("rule2.2-%d-%d-%d", a, b, c), fmt.Sprintf("rule2.2-%d-%d", a, b), func(ctx ruller.Context) (map[string]interface{}, error) {
output := make(map[string]interface{})
output["opt3"] = "any3"
return output, nil
})
if err != nil {
panic(err)
}
}
}
}
if err := ruller.StartServer(); err != nil {
logrus.Errorf("error starting server %s", err.Error())
}
}