generated from gatewayd-io/plugin-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
134 lines (117 loc) · 4.25 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
package main
import (
"encoding/base64"
"flag"
"log"
"os"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
"github.com/gatewayd-io/gatewayd-plugin-js/plugin"
sdkConfig "github.com/gatewayd-io/gatewayd-plugin-sdk/config"
"github.com/gatewayd-io/gatewayd-plugin-sdk/logging"
"github.com/gatewayd-io/gatewayd-plugin-sdk/metrics"
p "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin"
v1 "github.com/gatewayd-io/gatewayd-plugin-sdk/plugin/v1"
"github.com/getsentry/sentry-go"
"github.com/hashicorp/go-hclog"
goplugin "github.com/hashicorp/go-plugin"
"github.com/spf13/cast"
pgQuery "github.com/wasilibs/go-pgquery"
"golang.org/x/exp/maps"
)
func main() {
sentryDSN := sdkConfig.GetEnv("SENTRY_DSN", "")
// Initialize Sentry SDK
err := sentry.Init(sentry.ClientOptions{
Dsn: sentryDSN,
TracesSampleRate: 1.0,
})
if err != nil {
log.Fatalf("Failed to initialize Sentry SDK: %s", err.Error())
}
// Parse command line flags, passed by GatewayD via the plugin config
logLevel := flag.String("log-level", "info", "Log level")
flag.Parse()
logger := hclog.New(&hclog.LoggerOptions{
Level: logging.GetLogLevel(*logLevel),
Output: os.Stderr,
JSONFormat: true,
Color: hclog.ColorOff,
})
pluginInstance := plugin.NewJSPlugin(plugin.Plugin{
Logger: logger,
VM: goja.New(),
Bindings: map[string]goja.Callable{},
})
// Enable the VM to require modules.
registry := require.Registry{}
registry.Enable(pluginInstance.Impl.VM)
// Enable the VM to print to stdout and stderr.
printer := console.StdPrinter{
StdoutPrint: func(s string) { pluginInstance.Impl.Logger.Info(s) },
StderrPrint: func(s string) { pluginInstance.Impl.Logger.Error(s) },
}
registry.RegisterNativeModule("console", console.RequireWithPrinter(printer))
console.Enable(pluginInstance.Impl.VM)
// Provide the Value helper to the JS code to make it easier to create
// new values inside v1.Struct.
if err := pluginInstance.Impl.VM.Set("Value", pluginInstance.Impl.VM.ToValue(v1.NewValue)); err != nil {
logger.Error("Failed to set Value helper function", "error", err)
return
}
if cfg := cast.ToStringMap(plugin.PluginConfig["config"]); cfg != nil {
config := metrics.NewMetricsConfig(cfg)
if config != nil && config.Enabled {
go metrics.ExposeMetrics(config, logger)
}
scriptPath := cast.ToString(cfg["scriptPath"])
// Read the JS code from the script file.
script, err := os.ReadFile(scriptPath)
if err != nil {
logger.Error("Failed to read script file", "error", err)
return
}
logger.Debug("Read script file", "bytes", len(script), "path", scriptPath)
_, err = pluginInstance.Impl.VM.RunString(string(script))
if err != nil {
logger.Error("Failed to run JS code", "error", err)
return
}
// Register the JS functions as Go functions.
pluginInstance.Impl.RegisterFunctions(maps.Keys(plugin.Hooks))
// Register helper functions.
if err := pluginInstance.Impl.VM.Set("btoa", func(call goja.FunctionCall) goja.Value {
return pluginInstance.Impl.VM.ToValue(base64.RawStdEncoding.EncodeToString([]byte(call.Arguments[0].String())))
}); err != nil {
logger.Error("Failed to set btoa helper function", "error", err)
return
}
if err := pluginInstance.Impl.VM.Set("atob", func(call goja.FunctionCall) goja.Value {
str, _ := base64.RawStdEncoding.DecodeString(call.Arguments[0].String())
return pluginInstance.Impl.VM.ToValue(string(str))
}); err != nil {
logger.Error("Failed to set atob helper function", "error", err)
return
}
if err := pluginInstance.Impl.VM.Set("parseSQL", func(call goja.FunctionCall) goja.Value {
qStr, _ := pgQuery.ParseToJSON(call.Arguments[0].String())
return pluginInstance.Impl.VM.ToValue(qStr)
}); err != nil {
logger.Error("Failed to set parseSQL helper function", "error", err)
return
}
}
goplugin.Serve(&goplugin.ServeConfig{
HandshakeConfig: goplugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: sdkConfig.GetEnv("MAGIC_COOKIE_KEY", ""),
MagicCookieValue: sdkConfig.GetEnv("MAGIC_COOKIE_VALUE", ""),
},
Plugins: v1.GetPluginSetMap(map[string]goplugin.Plugin{
plugin.PluginID.GetName(): pluginInstance,
}),
GRPCServer: p.DefaultGRPCServer,
Logger: logger,
})
}