-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
149 lines (122 loc) · 3.7 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
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
//go:build js && wasm
// +build js,wasm
package main
import (
"fmt"
"os"
"strings"
"syscall/js"
"github.com/cgi-fr/pimo/internal/app/pimo"
"github.com/cgi-fr/pimo/pkg/flow"
"github.com/cgi-fr/pimo/pkg/jsonline"
"github.com/cgi-fr/pimo/pkg/model"
"github.com/rs/zerolog/log"
)
func play(yaml string, data string) (result interface{}, err error) {
config := pimo.Config{
EmptyInput: false,
RepeatUntil: "",
RepeatWhile: "",
Iteration: 1,
SkipLineOnError: false,
SkipFieldOnError: false,
CachesToDump: map[string]string{},
CachesToLoad: map[string]string{},
}
pdef, pipe_err := model.LoadPipelineDefinitionFromYAML([]byte(yaml))
if pipe_err != nil {
log.Err(pipe_err).Msg("Cannot load pipeline definition")
return
}
input, json_err := jsonline.JSONToDictionary([]byte(data))
if json_err != nil {
log.Err(json_err).Msg("Cannot load pipeline definition")
return nil, json_err
}
config.SingleInput = &input
context := pimo.NewContext(pdef)
if ctx_err := context.Configure(config); ctx_err != nil {
log.Err(ctx_err).Msg("Cannot configure pipeline")
return nil, ctx_err
}
output := &strings.Builder{}
stats, err := context.Execute(output)
if err != nil {
log.Err(err).Msg("Cannot execute pipeline")
}
log.Info().Interface("stats", stats).Msg("Input masked")
return output.String(), err
}
func main() {
// Environement variable for FF1 encryption
os.Setenv("FF1_ENCRYPTION_KEY", "LW4gInNlY3JldDEyc2VjcmV0MTIiIA0K")
pimo := map[string]interface{}{}
// JS Constructor
errorConstructor := js.Global().Get("Error")
promiseConstructor := js.Global().Get("Promise")
pimo["play"] = js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
yaml := fmt.Sprintf("%v", inputs[0]) // masking
data := fmt.Sprintf("%v", inputs[1]) // data
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
reject := args[1]
go func() {
// dont't panic
defer func() {
if r := recover(); r != nil {
log.Error().AnErr("panic", r.(error)).Msg("Recovering from panic in play.")
errorObject := errorConstructor.New(r.(error).Error())
reject.Invoke(errorObject)
}
}()
data, err := play(yaml, data)
if err != nil {
// err should be an instance of `error`, eg `errors.New("some error")`
errorObject := errorConstructor.New(err.Error())
reject.Invoke(errorObject)
} else {
resolve.Invoke(js.ValueOf(data))
}
}()
return nil
})
return promiseConstructor.New(handler)
})
pimo["flow"] = js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
yaml := fmt.Sprintf("%v", inputs[0]) // masking
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
reject := args[1]
go func() {
// dont't panic
defer func() {
if r := recover(); r != nil {
log.Error().AnErr("panic", r.(error)).Msg("Recovering from panic in flow")
errorObject := errorConstructor.New(r.(error).Error())
reject.Invoke(errorObject)
}
}()
pdef, err := model.LoadPipelineDefinitionFromYAML([]byte(yaml))
if err != nil {
log.Err(err).Msg("Cannot load pipeline definition")
errorObject := errorConstructor.New(err.Error())
reject.Invoke(errorObject)
return
}
data, err := flow.Export(pdef)
if err != nil {
// err should be an instance of `error`, eg `errors.New("some error")`
errorObject := errorConstructor.New(err.Error())
reject.Invoke(errorObject)
} else {
resolve.Invoke(js.ValueOf(data))
}
}()
return nil
})
return promiseConstructor.New(handler)
})
// Expose pimo object
js.Global().Set("pimo", pimo)
select {} // sleep forever
}