-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathactions.go
More file actions
258 lines (212 loc) · 6.44 KB
/
actions.go
File metadata and controls
258 lines (212 loc) · 6.44 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package diecast
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/ghetzel/go-stockutil/httputil"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/stringutil"
"github.com/ghetzel/go-stockutil/timeutil"
"github.com/ghetzel/go-stockutil/typeutil"
)
var DefaultActionStepTimeout = 10 * time.Second
var steps = map[string]Performable{
`shell`: &ShellStep{},
`process`: &ProcessStep{},
`respond`: &RespondStep{},
}
// Register a performable step type to the given type name.
func RegisterActionStep(typeName string, performable Performable) {
if performable != nil {
steps[typeName] = performable
} else {
panic("cannot register nil step for type " + typeName)
}
}
type Performable interface {
Perform(config *StepConfig, w http.ResponseWriter, req *http.Request, prev *StepConfig) (any, error)
}
type StepConfig struct {
Type string `yaml:"type" json:"type"` // The type of step
Data any `yaml:"data" json:"data"` // The data being passed into this step from the previous one
Timeout string `yaml:"timeout,omitempty" json:"timeout,omitempty"` // Timeout for this step
Parser string `yaml:"parser" json:"parser"` // The format the data being passed in is expected to be in
Output any `yaml:"-" json:"-"`
Error error `yaml:"-" json:"-"`
index int
reader io.Reader
}
func (config *StepConfig) String() string {
if t := config.Type; t == `` {
return `(unknown)`
} else {
return t
}
}
func (config *StepConfig) postprocess() {
switch config.Parser {
case ``, `json`:
var out = typeutil.Bytes(config.Output)
if len(out) > 0 {
if stringutil.IsSurroundedBy(out, `[`, `]`) {
var outA []any
if err := json.Unmarshal(out, &outA); err == nil {
config.Output = outA
} else {
config.Error = err
}
} else if stringutil.IsSurroundedBy(out, `[`, `]`) {
var outM map[string]any
if err := json.Unmarshal(out, &outM); err == nil {
config.Output = outM
} else {
config.Error = err
}
} else {
var outI any
if err := json.Unmarshal(out, &outI); err == nil {
config.Output = outI
} else if log.ErrHasPrefix(err, `invalid character`) {
var lines = strings.Split(string(out), "\n")
var outA []any
var asLines bool
for i, line := range lines {
var outM map[string]any
if err := json.Unmarshal([]byte(line), &outM); err == nil {
outA = append(outA, outM)
} else if log.ErrHasPrefix(err, `invalid character`) {
asLines = true
break
} else {
config.logstep("output line=%d: err=%v", i, err)
}
}
if asLines {
outA = sliceutil.Sliceify(sliceutil.CompactString(lines))
}
config.Output = outA
} else {
config.Error = err
}
}
}
case `lines`:
config.Output = sliceutil.CompactString(
strings.Split(typeutil.String(config.Output), "\n"),
)
default:
config.Error = fmt.Errorf("unsupported step parser %q", config.Parser)
}
}
func (config *StepConfig) Read(b []byte) (int, error) {
if r, ok := config.Output.(io.Reader); ok {
return r.Read(b)
} else if config.reader == nil {
if data, err := json.Marshal(config.Output); err == nil {
config.reader = bytes.NewBuffer(data)
} else {
return 0, err
}
}
return config.reader.Read(b)
}
func (config *StepConfig) Close() error {
if config.reader != nil {
config.reader = nil
}
if c, ok := config.Output.(io.Closer); ok {
return c.Close()
} else {
return nil
}
}
func (config *StepConfig) getTimeout() time.Duration {
if config.Timeout != `` {
if timeout, err := timeutil.ParseDuration(config.Timeout); err == nil {
return timeout
}
}
return DefaultActionStepTimeout
}
func (config *StepConfig) Perform(_ *StepConfig, w http.ResponseWriter, req *http.Request, prev *StepConfig) (any, error) {
if step, ok := steps[config.Type]; ok {
return step.Perform(config, w, req, prev)
} else {
return nil, fmt.Errorf("unrecognized action step type %q", config.Type)
}
}
func (config *StepConfig) logstep(format string, args ...any) {
if format != `` {
if !strings.HasPrefix(format, "\u2502") {
format = "\u2502 " + format
}
log.Debugf(format, args...)
}
}
type Action struct {
Name string `yaml:"name,omitempty" json:"name,omitempty"` // The name of this action
Path string `yaml:"path" json:"path"` // The URL path this action is accessible from
Method any `yaml:"method" json:"method"` // The HTTP method(s) this action will respond to
Steps []*StepConfig `yaml:"steps" json:"steps"` // The list of steps that are applied, in order, to the request body in order to generate a response
}
// Performs the action in response to an HTTP request, evaluating all action steps. Steps are
// responsible for generating and manipulating output. The output of the last step will be returned,
// or an error will be returned if not nil.
func (config *Action) ServeHTTP(w http.ResponseWriter, req *http.Request) {
var started = time.Now()
var name = config.Name
if name == `` {
name = fmt.Sprintf("%s %s", req.Method, req.URL.Path)
}
var initData any
if req.ContentLength > 0 {
defer req.Body.Close()
var asMap map[string]any
if err := httputil.ParseRequest(req, &asMap); err == nil {
initData = asMap
} else {
httputil.RespondJSON(w, err)
}
} else {
initData = req.Body
}
var prev = &StepConfig{
Type: `input`,
Output: initData,
index: -1,
}
prev.postprocess()
log.Debugf("\u256d Run action %s", name)
for i, step := range config.Steps {
step.index = i
step.logstep("\u2502 step %d: type=%v data=%T", i, step.Type, step.Data)
out, err := step.Perform(step, w, req, prev)
prev = step
prev.Output = out
prev.Error = err
prev.postprocess()
step.logstep("output=%T err=%v", prev.Output, prev.Error)
if prev.Error != nil && prev.Error.Error() == `stop` {
step.logstep("break early", i)
return
}
}
if prev != nil {
if err := prev.Error; err != nil {
httputil.RespondJSON(w, map[string]any{
`error`: err.Error(),
`output`: prev.Output,
}, http.StatusInternalServerError)
} else {
httputil.RespondJSON(w, prev.Output)
}
} else {
w.WriteHeader(http.StatusNoContent)
}
log.Debugf("\u2570 response sent (took: %v)", time.Since(started))
}