-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrigger.go
211 lines (183 loc) · 4.33 KB
/
trigger.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package trigger
import (
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"github.com/project-flogo/core/data/metadata"
"github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/trigger"
"github.com/project-flogo/eftl/lib"
)
var triggerMetadata = trigger.NewMetadata(&Settings{}, &HandlerSettings{}, &Output{}, &Reply{})
func init() {
trigger.Register(&Trigger{}, &Factory{})
}
// Handler is a EFTL handler
type Handler struct {
handler trigger.Handler
settings HandlerSettings
}
// Trigger is a simple EFTL trigger
type Trigger struct {
connection *lib.Connection
stop chan bool
config *trigger.Config
settings *Settings
handlers map[string]Handler
logger log.Logger
}
// Factory is a EFTL trigger factory
type Factory struct {
}
// New creates a new EFTL trigger
func (*Factory) New(config *trigger.Config) (trigger.Trigger, error) {
s := &Settings{}
err := metadata.MapToStruct(config.Settings, s, true)
if err != nil {
return nil, err
}
return &Trigger{config: config, settings: s}, nil
}
// Metadata returns the EFTL trigger metadata
func (f *Factory) Metadata() *trigger.Metadata {
return triggerMetadata
}
// Initialize initializes the trigger
func (t *Trigger) Initialize(ctx trigger.InitContext) error {
logger := ctx.Logger()
t.logger = logger
handlers := make(map[string]Handler)
for _, handler := range ctx.GetHandlers() {
settings := HandlerSettings{}
err := metadata.MapToStruct(handler.Settings(), &settings, true)
if err != nil {
return err
}
handlers[settings.Dest] = Handler{
handler: handler,
settings: settings,
}
}
t.handlers = handlers
return nil
}
// Start implements trigger start
func (t *Trigger) Start() error {
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
}
ca := t.settings.CA
if ca != "" {
certificate, err := ioutil.ReadFile(ca)
if err != nil {
t.logger.Error("can't open certificate", err)
return err
}
pool := x509.NewCertPool()
pool.AppendCertsFromPEM(certificate)
tlsConfig = &tls.Config{
RootCAs: pool,
}
}
id := t.settings.ID
user := t.settings.User
password := t.settings.Password
options := &lib.Options{
ClientID: id,
Username: user,
Password: password,
TLSConfig: tlsConfig,
}
url := t.settings.URL
errorsChannel := make(chan error, 1)
var err error
t.connection, err = lib.Connect(url, options, errorsChannel)
if err != nil {
t.logger.Errorf("connection failed: %s", err)
return err
}
messages := make(chan lib.Message, 1000)
for dest := range t.handlers {
matcher := fmt.Sprintf("{\"_dest\":\"%s\"}", dest)
_, err = t.connection.Subscribe(matcher, "", messages)
if err != nil {
t.logger.Errorf("subscription failed: %s", err)
return err
}
}
t.stop = make(chan bool, 1)
go func() {
for {
select {
case message := <-messages:
value := message["_dest"]
dest, ok := value.(string)
if !ok {
t.logger.Errorf("dest is required for valid message")
continue
}
handler, ok := t.handlers[dest]
if !ok {
t.logger.Error("no handler for dest ", dest)
continue
}
value = message["content"]
content, ok := value.([]byte)
if !ok {
content = []byte{}
}
err := t.RunAction(handler, dest, content)
if err != nil {
t.logger.Errorf("action error: %s", err)
}
case err := <-errorsChannel:
t.logger.Errorf("connection error: %s", err)
case <-t.stop:
return
}
}
}()
return nil
}
// Stop implements trigger stop
func (t *Trigger) Stop() error {
if t.connection != nil {
t.connection.Disconnect()
}
if t.stop != nil {
t.stop <- true
}
return nil
}
// RunAction starts a new Process Instance
func (t *Trigger) RunAction(handler Handler, dest string, content []byte) error {
t.logger.Infof("EFTL Trigger: Received request for id '%s'", t.config.Id)
var data map[string]interface{}
err := json.Unmarshal(content, &data)
if err != nil {
return err
}
replyTo := ""
if value, ok := data["replyTo"]; ok && value != "" {
replyTo = value.(string)
}
output := Output{
Content: data,
}
results, err := handler.handler.Handle(context.Background(), &output)
if err != nil {
return err
}
if replyTo == "" {
return nil
}
reply := &Reply{}
reply.FromMap(results)
return t.connection.Publish(lib.Message{
"_dest": replyTo,
"content": reply.Data,
})
}