Skip to content

Commit

Permalink
Move get config runtime functions to initializers
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito committed Nov 22, 2024
1 parent bbbc617 commit 5be3285
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 307 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZH
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/heroiclabs/nakama-common v1.34.1-0.20241121105602-e24311ad3419 h1:sC+dslMcIE3E8lc1fXOIvYb7cPjtZUVvHiegHdLah4k=
github.com/heroiclabs/nakama-common v1.34.1-0.20241121105602-e24311ad3419/go.mod h1:lPG64MVCs0/tEkh311Cd6oHX9NLx2vAPx7WW7QCJHQ0=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a h1:tuL2ZPaeCbNw8rXmV9ywd00nXRv95V4/FmbIGKLQJAE=
github.com/heroiclabs/sql-migrate v0.0.0-20240528102547-233afc8cf05a/go.mod h1:hzCTPoEi/oml2BllVydJcNP63S7b56e5DzeQeLGvw1U=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand Down
10 changes: 10 additions & 0 deletions server/runtime_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type RuntimeGoInitializer struct {
version string
env map[string]string
nk runtime.NakamaModule
config Config

rpc map[string]RuntimeRpcFunction
beforeRt map[string]RuntimeBeforeRtFunction
Expand Down Expand Up @@ -79,6 +80,14 @@ type RuntimeGoInitializer struct {
fmCallbackHandler runtime.FmCallbackHandler
}

func (ri *RuntimeGoInitializer) GetConfig() (runtime.Config, error) {
cfg, err := ri.config.GetRuntimeConfig()
if err != nil {
return nil, err
}
return cfg, nil
}

func (ri *RuntimeGoInitializer) RegisterEvent(fn func(ctx context.Context, logger runtime.Logger, evt *api.Event)) error {
ri.eventFunctions = append(ri.eventFunctions, fn)
return nil
Expand Down Expand Up @@ -2860,6 +2869,7 @@ func NewRuntimeProviderGo(ctx context.Context, logger, startupLogger *zap.Logger
version: version,
env: env,
nk: nk,
config: config,

rpc: make(map[string]RuntimeRpcFunction),

Expand Down
12 changes: 0 additions & 12 deletions server/runtime_go_nakama.go
Original file line number Diff line number Diff line change
Expand Up @@ -4423,18 +4423,6 @@ func (n *RuntimeGoNakamaModule) ChannelIdBuild(ctx context.Context, senderId, ta
return channelId, nil
}

// @group configuration
// @summary Get a subset of the Nakama configuration values.
// @return config(runtime.Config) A number of Nakama configuration values.
// @return error(error) An optional error value if an error occurred.
func (n *RuntimeGoNakamaModule) GetConfig() (runtime.Config, error) {
c, err := n.config.GetRuntimeConfig()
if err != nil {
return nil, err
}
return c, nil
}

// @group satori
// @summary Get the Satori client.
// @return satori(runtime.Satori) The Satori client.
Expand Down
4 changes: 2 additions & 2 deletions server/runtime_javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type RuntimeJS struct {
env goja.Value
envMap map[string]string
vm *goja.Runtime
nakamaModule *runtimeJavascriptNakamaModule
nakamaModule *RuntimeJavascriptNakamaModule
callbacks *RuntimeJavascriptCallbacks
}

Expand Down Expand Up @@ -2425,7 +2425,7 @@ func evalRuntimeModules(rp *RuntimeProviderJS, modCache *RuntimeJSModuleCache, m
}
modName := modCache.Names[0]

initializer := NewRuntimeJavascriptInitModule(logger, modCache.Modules[modName].Ast, storageIndex, callbacks, matchHandlers, announceCallbackFn)
initializer := NewRuntimeJavascriptInitModule(logger, rp.config, modCache.Modules[modName].Ast, storageIndex, callbacks, matchHandlers, announceCallbackFn)
init, err := initializer.Constructor(r)
if err != nil {
return nil, err
Expand Down
105 changes: 104 additions & 1 deletion server/runtime_javascript_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,24 @@ type RuntimeJavascriptInitModule struct {
announceCallbackFn func(RuntimeExecutionMode, string)
storageIndex StorageIndex
ast *ast.Program
config Config
}

func NewRuntimeJavascriptInitModule(logger *zap.Logger, ast *ast.Program, storageIndex StorageIndex, callbacks *RuntimeJavascriptCallbacks, matchCallbacks *RuntimeJavascriptMatchHandlers, announceCallbackFn func(RuntimeExecutionMode, string)) *RuntimeJavascriptInitModule {
func NewRuntimeJavascriptInitModule(logger *zap.Logger, config Config, ast *ast.Program, storageIndex StorageIndex, callbacks *RuntimeJavascriptCallbacks, matchCallbacks *RuntimeJavascriptMatchHandlers, announceCallbackFn func(RuntimeExecutionMode, string)) *RuntimeJavascriptInitModule {
return &RuntimeJavascriptInitModule{
Logger: logger,
storageIndex: storageIndex,
announceCallbackFn: announceCallbackFn,
Callbacks: callbacks,
MatchCallbacks: matchCallbacks,
ast: ast,
config: config,
}
}

func (im *RuntimeJavascriptInitModule) mappings(r *goja.Runtime) map[string]func(goja.FunctionCall) goja.Value {
return map[string]func(goja.FunctionCall) goja.Value{
"getConfig": im.getConfig(r),
"registerRpc": im.registerRpc(r),
"registerRtBefore": im.registerRtBefore(r),
"registerRtAfter": im.registerRtAfter(r),
Expand Down Expand Up @@ -288,6 +291,106 @@ func (im *RuntimeJavascriptInitModule) Constructor(r *goja.Runtime) (*goja.Objec
return r.New(r.ToValue(constructor))
}

func (im *RuntimeJavascriptInitModule) getConfig(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
return func(f goja.FunctionCall) goja.Value {
rnc, err := im.config.GetRuntimeConfig()
if err != nil {
panic(r.NewGoError(err))
}

cfgObj := r.NewObject()
_ = cfgObj.Set("name", rnc.GetName())
_ = cfgObj.Set("shutdown_grace_sec", rnc.GetShutdownGraceSec())

lgCfg := r.NewObject()
_ = lgCfg.Set("level", rnc.GetLogger().GetLevel())
_ = cfgObj.Set("logger", lgCfg)

sessCfg := r.NewObject()
_ = sessCfg.Set("encryption_key", rnc.GetSession().GetEncryptionKey())
_ = sessCfg.Set("token_expiry_sec", rnc.GetSession().GetTokenExpirySec())
_ = sessCfg.Set("refresh_encryption_key", rnc.GetSession().GetRefreshEncryptionKey())
_ = sessCfg.Set("refresh_token_expiry_sec", rnc.GetSession().GetRefreshEncryptionKey())
_ = sessCfg.Set("single_socket", rnc.GetSession().GetSingleSocket())
_ = sessCfg.Set("single_match", rnc.GetSession().GetSingleMatch())
_ = sessCfg.Set("single_party", rnc.GetSession().GetSingleParty())
_ = sessCfg.Set("single_session", rnc.GetSession().GetSingleSession())
_ = cfgObj.Set("session", sessCfg)

socketCfg := r.NewObject()
_ = socketCfg.Set("server_key", rnc.GetSocket().GetServerKey())
_ = socketCfg.Set("port", rnc.GetSocket().GetPort())
_ = socketCfg.Set("address", rnc.GetSocket().GetPort())
_ = socketCfg.Set("protocol", rnc.GetSocket().GetProtocol())
_ = cfgObj.Set("socket", socketCfg)

// Social
steamCfg := r.NewObject()
_ = steamCfg.Set("publisher_key", rnc.GetSocial().GetSteam().GetPublisherKey())
_ = steamCfg.Set("app_id", rnc.GetSocial().GetSteam().GetAppID())

fbInstantCfg := r.NewObject()
_ = fbInstantCfg.Set("app_secret", rnc.GetSocial().GetFacebookInstantGame().GetAppSecret())

fbLimitedCfg := r.NewObject()
_ = fbLimitedCfg.Set("app_id", rnc.GetSocial().GetFacebookLimitedLogin().GetAppId())

appleCfg := r.NewObject()
_ = appleCfg.Set("bundle_id", rnc.GetSocial().GetApple().GetBundleId())

socialCfg := r.NewObject()
_ = socialCfg.Set("steam", steamCfg)
_ = socialCfg.Set("facebook_instant_game", fbInstantCfg)
_ = socialCfg.Set("facebook_limited_login", fbLimitedCfg)
_ = socialCfg.Set("apple", appleCfg)
_ = cfgObj.Set("social", socialCfg)

runtimeCfg := r.NewObject()
_ = runtimeCfg.Set("env", rnc.GetRuntime().GetEnv())
_ = runtimeCfg.Set("http_key", rnc.GetRuntime().GetHTTPKey())
_ = cfgObj.Set("runtime", runtimeCfg)

// IAP
iapAppleCfg := r.NewObject()
_ = iapAppleCfg.Set("shared_password", rnc.GetIAP().GetApple().GetSharedPassword())
_ = iapAppleCfg.Set("notifications_endpoint_id", rnc.GetIAP().GetApple().GetNotificationsEndpointId())

iapGoogleCfg := r.NewObject()
_ = iapGoogleCfg.Set("client_email", rnc.GetIAP().GetGoogle().GetClientEmail())
_ = iapGoogleCfg.Set("private_key", rnc.GetIAP().GetGoogle().GetPrivateKey())
_ = iapGoogleCfg.Set("notifications_endpoint_id", rnc.GetIAP().GetGoogle().GetNotificationsEndpointId())
_ = iapGoogleCfg.Set("refund_check_period_min", rnc.GetIAP().GetGoogle().GetRefundCheckPeriodMin())
_ = iapGoogleCfg.Set("package_name", rnc.GetIAP().GetGoogle().GetPackageName())

iapHuaweiCfg := r.NewObject()
_ = iapHuaweiCfg.Set("public_key", rnc.GetIAP().GetHuawei().GetPublicKey())
_ = iapHuaweiCfg.Set("client_id", rnc.GetIAP().GetHuawei().GetClientID())
_ = iapHuaweiCfg.Set("client_secret", rnc.GetIAP().GetHuawei().GetClientSecret())

iapFacebookInstantCfg := r.NewObject()
_ = iapFacebookInstantCfg.Set("app_secret", rnc.GetIAP().GetFacebookInstant().GetAppSecret())
iapCfg := r.NewObject()
_ = iapCfg.Set("apple", iapAppleCfg)
_ = iapCfg.Set("google", iapGoogleCfg)
_ = iapCfg.Set("huawei", iapHuaweiCfg)
_ = iapCfg.Set("apple", iapAppleCfg)
_ = cfgObj.Set("iap", iapCfg)

googleAuthCfg := r.NewObject()
_ = googleAuthCfg.Set("credentials_json", rnc.GetGoogleAuth().GetCredentialsJSON())
_ = cfgObj.Set("google_auth", googleAuthCfg)

satoriCfg := r.NewObject()
_ = satoriCfg.Set("url", rnc.GetSatori().GetUrl())
_ = satoriCfg.Set("api_key_name", rnc.GetSatori().GetApiKeyName())
_ = satoriCfg.Set("api_key", rnc.GetSatori().GetApiKey())
_ = satoriCfg.Set("signing_key", rnc.GetSatori().GetSigningKey())
_ = cfgObj.Set("satori", satoriCfg)

return cfgObj
}
}

func (im *RuntimeJavascriptInitModule) registerRpc(r *goja.Runtime) func(goja.FunctionCall) goja.Value {
return func(f goja.FunctionCall) goja.Value {
fName := f.Argument(0)
Expand Down
Loading

0 comments on commit 5be3285

Please sign in to comment.