-
Notifications
You must be signed in to change notification settings - Fork 78
/
trigger.go
215 lines (165 loc) · 5.05 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
212
213
214
215
package rest
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/julienschmidt/httprouter"
"github.com/TIBCOSoftware/flogo-contrib/trigger/rest/cors"
"github.com/TIBCOSoftware/flogo-lib/core/action"
"github.com/TIBCOSoftware/flogo-lib/core/trigger"
"github.com/TIBCOSoftware/flogo-lib/logger"
)
const (
REST_CORS_PREFIX = "REST_TRIGGER"
)
// log is the default package logger
var log = logger.GetLogger("trigger-tibco-rest")
var validMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
// RestTrigger REST trigger struct
type RestTrigger struct {
metadata *trigger.Metadata
runner action.Runner
server *Server
config *trigger.Config
}
//NewFactory create a new Trigger factory
func NewFactory(md *trigger.Metadata) trigger.Factory {
return &RestFactory{metadata:md}
}
// RestFactory REST Trigger factory
type RestFactory struct{
metadata *trigger.Metadata
}
//New Creates a new trigger instance for a given id
func (t *RestFactory) New(config *trigger.Config) trigger.Trigger {
return &RestTrigger{metadata: t.metadata, config:config}
}
// Metadata implements trigger.Trigger.Metadata
func (t *RestTrigger) Metadata() *trigger.Metadata {
return t.metadata
}
func (t *RestTrigger) Init(runner action.Runner) {
router := httprouter.New()
if t.config.Settings == nil {
panic(fmt.Sprintf("No Settings found for trigger '%s'", t.config.Id))
}
if _,ok := t.config.Settings["port"]; !ok {
panic(fmt.Sprintf("No Port found for trigger '%s' in settings", t.config.Id))
}
addr := ":" + t.config.GetSetting("port")
t.runner = runner
// Init handlers
for _, handlerCfg := range t.config.Handlers {
if handlerIsValid(handlerCfg) {
method := strings.ToUpper(handlerCfg.GetSetting("method"))
path := handlerCfg.GetSetting("path")
log.Debugf("REST Trigger: Registering handler [%s: %s] for Action Id: [%s]", method, path, handlerCfg.ActionId)
router.OPTIONS(path, handleCorsPreflight) // for CORS
router.Handle(method, path, newActionHandler(t, handlerCfg.ActionId, handlerCfg))
} else {
panic(fmt.Sprintf("Invalid handler: %v", handlerCfg))
}
}
log.Debugf("REST Trigger: Configured on port %s", t.config.Settings["port"])
t.server = NewServer(addr, router)
}
func (t *RestTrigger) Start() error {
return t.server.Start()
}
// Stop implements util.Managed.Stop
func (t *RestTrigger) Stop() error {
return t.server.Stop()
}
// Handles the cors preflight request
func handleCorsPreflight(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
log.Infof("Received [OPTIONS] request to CorsPreFlight: %+v", r)
c := cors.New(REST_CORS_PREFIX, log)
c.HandlePreflight(w, r)
}
// IDResponse id response object
type IDResponse struct {
ID string `json:"id"`
}
func newActionHandler(rt *RestTrigger, actionId string, handlerCfg *trigger.HandlerConfig) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
log.Infof("REST Trigger: Received request for id '%s'", rt.config.Id)
c := cors.New(REST_CORS_PREFIX, log)
c.WriteCorsActualRequestHeaders(w)
pathParams := make(map[string]string)
for _, param := range ps {
pathParams[param.Key] = param.Value
}
var content interface{}
err := json.NewDecoder(r.Body).Decode(&content)
if err != nil {
switch {
case err == io.EOF:
// empty body
//todo should handler say if content is expected?
case err != nil:
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
queryValues := r.URL.Query()
queryParams := make(map[string]string, len(queryValues))
for key, value := range queryValues {
queryParams[key] = strings.Join(value, ",")
}
data := map[string]interface{}{
"params": pathParams,
"pathParams": pathParams,
"queryParams": queryParams,
"content": content,
}
//todo handle error
startAttrs, _ := rt.metadata.OutputsToAttrs(data, false)
action := action.Get(actionId)
log.Debugf("Found action' %+x'", action)
context := trigger.NewContext(context.Background(), startAttrs)
replyCode, replyData, err := rt.runner.Run(context, action, actionId, nil)
if err != nil {
log.Debugf("REST Trigger Error: %s", err.Error())
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if replyData != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(replyCode)
if err := json.NewEncoder(w).Encode(replyData); err != nil {
log.Error(err)
}
}
if replyCode > 0 {
w.WriteHeader(replyCode)
} else {
w.WriteHeader(http.StatusOK)
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// Utils
func handlerIsValid(handler *trigger.HandlerConfig) bool {
if handler.Settings == nil {
return false
}
if handler.Settings["method"] == "" {
return false
}
if !stringInList(strings.ToUpper(handler.GetSetting("method")), validMethods) {
return false
}
//validate path
return true
}
func stringInList(str string, list []string) bool {
for _, value := range list {
if value == str {
return true
}
}
return false
}