Skip to content

Commit

Permalink
add environment var and hostname to the process in the runtime module
Browse files Browse the repository at this point in the history
  • Loading branch information
bubbajoe committed May 31, 2024
1 parent 53ceb64 commit be5a2bf
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
19 changes: 10 additions & 9 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ import (

type (
DGateConfig struct {
Version string `koanf:"version"`
LogLevel string `koanf:"log_level,string"`
LogJson bool `koanf:"log_json"`
LogColor bool `koanf:"log_color"`
Logging *LoggingConfig `koanf:"Logger"`
Version string `koanf:"version"`
LogLevel string `koanf:"log_level,string"`
LogJson bool `koanf:"log_json"`
LogColor bool `koanf:"log_color"`

NodeId string `koanf:"node_id"`
Logging *LoggingConfig `koanf:"Logger"`
Storage DGateStorageConfig `koanf:"storage"`
ProxyConfig DGateProxyConfig `koanf:"proxy"`
AdminConfig *DGateAdminConfig `koanf:"admin"`
TestServerConfig *DGateTestServerConfig `koanf:"test_server"`
Debug bool `koanf:"debug"`
Tags []string `koanf:"tags"`

DisableMetrics bool `koanf:"disable_metrics"`
DisableDefaultNamespace bool `koanf:"disable_default_namespace"`
DisableMetrics bool `koanf:"disable_metrics"`
DisableDefaultNamespace bool `koanf:"disable_default_namespace"`
Debug bool `koanf:"debug"`
Tags []string `koanf:"tags"`
}

LoggingConfig struct {
Expand Down
53 changes: 35 additions & 18 deletions pkg/modules/extractors/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package extractors

import (
"errors"
"os"
"reflect"
"strings"

"github.com/dgate-io/dgate/pkg/modules"
"github.com/dgate-io/dgate/pkg/modules/dgate"
Expand All @@ -26,21 +28,37 @@ type RuntimeOptions struct {
Env map[string]string
}

func prepareRuntime(rt *goja.Runtime) {
rt.SetFieldNameMapper(&smartFieldNameMapper{})
var EnvVarMap = getEnvVarMap()

exports := rt.NewObject()
func getEnvVarMap() map[string]string {
env := os.Environ()
envMap := make(map[string]string, len(env))
for _, e := range env {
pair := strings.SplitN(e, "=", 2)
if len(pair) == 2 {
envMap[pair[0]] = pair[1]
}
}
return envMap
}

func prepareRuntime(rt *goja.Runtime) {
rt.SetFieldNameMapper(&smartMapper{})
module := rt.NewObject()
exports := rt.NewObject()
module.Set("exports", exports)
rt.Set("exports", exports)
rt.GlobalObject().Set("process", processObject(rt))
po := processObject(rt)
rt.GlobalObject().
Set("process", po)
rt.Set("module", module)
rt.Set("exports", exports)
}

func processObject(rt *goja.Runtime) *goja.Object {
obj := rt.NewObject()
obj.Set("env", rt.NewObject())
obj.Set("args", rt.NewObject())
obj.Set("env", EnvVarMap)
hostname, _ := os.Hostname()
obj.Set("host", hostname)
return obj
}

Expand All @@ -50,19 +68,18 @@ func SetupModuleEventLoop(
programs ...*goja.Program,
) error {
loop := rtCtx.EventLoop()

rt := loop.Runtime()
prepareRuntime(rt)

registry := loop.Registry()

if registerModules("dgate", rt,
registry, dgate.New(rtCtx),
req := loop.Registry()
if registerModules(
"dgate", rt, req,
dgate.New(rtCtx),
); printer == nil {
printer = &NoopPrinter{}
}

registry.RegisterNativeModule(
req.RegisterNativeModule(
"dgate_internal:console",
console.RequireWithPrinter(printer),
)
Expand All @@ -71,8 +88,8 @@ func SetupModuleEventLoop(
buffer.Enable(rt)
console.Enable(rt)

rt.Set("console", require.Require(rt, "dgate_internal:console").ToObject(rt))
rt.Set("fetch", require.Require(rt, "dgate/http").ToObject(rt).Get("fetch"))
rt.Set("console", require.Require(rt, "dgate_internal:console").ToObject(rt))
rt.Set("disableSetInterval", disableSetInterval)

for _, program := range programs {
Expand Down Expand Up @@ -125,18 +142,18 @@ func registerModules(
return exports
}

type smartFieldNameMapper struct{}
type smartMapper struct{}

var _ goja.FieldNameMapper = &smartFieldNameMapper{}
var _ goja.FieldNameMapper = &smartMapper{}

func (*smartFieldNameMapper) FieldName(_ reflect.Type, f reflect.StructField) string {
func (*smartMapper) FieldName(_ reflect.Type, f reflect.StructField) string {
if f.Tag.Get("json") != "" {
return f.Tag.Get("json")
}
return strcase.LowerCamelCase(f.Name)
}

func (*smartFieldNameMapper) MethodName(_ reflect.Type, m reflect.Method) string {
func (*smartMapper) MethodName(_ reflect.Type, m reflect.Method) string {
return strcase.LowerCamelCase(m.Name)
}

Expand Down

0 comments on commit be5a2bf

Please sign in to comment.