forked from project-flogo/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (86 loc) · 2.87 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/project-flogo/rules/examples/ordermanagement/audittrail"
"github.com/project-flogo/rules/ruleapi"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/rules/common"
"github.com/project-flogo/rules/common/model"
)
//go:generate go run $GOPATH/src/github.com/TIBCOSoftware/flogo-lib/flogo/gen/gen.go $GOPATH
const (
msgValueField = "message"
tupleSchemaPath = "src/github.com/project-flogo/rules/examples/ordermanagement/schema/oms_schema.json"
ruleDefinitionPath = "src/github.com/project-flogo/rules/examples/ordermanagement/schema/rule_definition.json"
)
// cli arguments
var (
awsAccesskey = flag.String("accesskey", "", "Access Key for AWS profile")
awsSecretkey = flag.String("secretkey", "", "Secret Key for AWS profile")
awsRegion = flag.String("region", "", "AWS region")
awsStreamName = flag.String("streamName", "oms_audittrail", "AWS kinesis stream name")
)
// RuleSession to maintain all the rule definitions
var ruleSession model.RuleSession
func main() {
flag.Parse()
err := validate()
if err != nil {
panic(err)
}
config := audittrail.ConnectionConfig{
AccessKey: *awsAccesskey,
SecretKey: *awsSecretkey,
RegionName: *awsRegion,
}
log.RootLogger().Info("***** Order Management App *****")
log.RootLogger().Info("--- Loading Tuple Schema ---")
err = loadTupleSchema()
if err != nil {
panic(err)
}
log.RootLogger().Info("--- Adding Rules ---")
ruleSession, err = createAndLoadRuleSession()
if err != nil {
panic(err)
}
log.RootLogger().Info("--- Starting Kinesis Publisher ---")
audittrail.SetupKinesisPubSub(config, *awsStreamName)
log.RootLogger().Info("--- Starting WebSocket Publisher ---")
wsPublisher := audittrail.Create(8686, *awsStreamName)
go wsPublisher.Start()
log.RootLogger().Info("--- Starting Rules Engine ---")
ruleSession.Start(nil)
log.RootLogger().Info("--- Starting MQTT Subscriber ---")
setupFlogoMQTTTriggers()
}
// loads the tuple schema
func loadTupleSchema() error {
content := getFileContent(tupleSchemaPath)
err := model.RegisterTupleDescriptors(string(content))
if err != nil {
return err
}
return nil
}
// create rulesession and load rules in it
func createAndLoadRuleSession() (model.RuleSession, error) {
content := getFileContent(ruleDefinitionPath)
return ruleapi.GetOrCreateRuleSessionFromConfig("oms_session", string(content))
}
// Get file content
func getFileContent(filePath string) string {
absPath := common.GetAbsPathForResource(filePath)
return common.FileToString(absPath)
}
// validates for any missing variables
func validate() error {
if *awsAccesskey == "" || *awsSecretkey == "" || *awsRegion == "" {
return fmt.Errorf("One or more of the required AWS credential details are missing (AccessKey/SecretKey/Region)")
}
if *awsStreamName == "" {
return fmt.Errorf("Missing Kinesis stream name")
}
return nil
}