From fd6420e8b69da837086164dbb05993e421c2d21f Mon Sep 17 00:00:00 2001 From: Simon Esposito Date: Sat, 23 Nov 2024 09:48:23 +0000 Subject: [PATCH] Add runtime API to read Nakama config values (#164) --- CHANGELOG.md | 1 + index.d.ts | 115 +++++++++++++++++++++++++++++++++++++++ runtime/config.go | 130 +++++++++++++++++++++++++++++++++++++++++++++ runtime/runtime.go | 4 ++ 4 files changed, 250 insertions(+) create mode 100644 runtime/config.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 0982f20..3e90e16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr - Add new runtime function to get a list of user's friend status. - Add new Follow/Unfollow runtime APIs. - Add new NotificationsUpdate runtime API. +- Add new initializers function to get config values. ## [1.34.0] - 2024-10-21 ### Added diff --git a/index.d.ts b/index.d.ts index 54805d0..39b262c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1066,6 +1066,14 @@ declare namespace nkruntime { * The injector used to initialize features of the game server. */ export interface Initializer { + /** + * Get subset of Nakama configs. + * + * @returns Object containing config values. + * @throws {TypeError} + */ + getConfig(): Config; + /** * Register an RPC function by its ID to be called as a S2S function or by game clients. * @@ -3239,6 +3247,113 @@ declare namespace nkruntime { persistent: boolean } + export interface Config { + name: string + shutdown_grace_sec: number + logger: ConfigLogger + session: ConfigSession + socket: ConfigSocket + social: ConfigSocial + runtime: ConfigRuntime + iap: ConfigIAP + google_auth: ConfigGoogleAuth + satori: ConfigSatori + } + + export interface ConfigLogger { + level: string + } + + export interface ConfigSession { + encryption_key: string + token_expiry_sec: number + refresh_encryption_key: string + refresh_token_expiry_sec: number + single_socket: boolean + single_match: boolean + single_party: boolean + single_session: boolean + } + + export interface ConfigSocket { + server_key: string + port: number + address: string + protocol: string + } + + export interface ConfigSocial { + steam: ConfigSteam + facebookInstantGame: ConfigFacebookInstantGame + facebookLimitedLogin: ConfigFacebookLimitedLogin + apple: ConfigApple + } + + export interface ConfigSteam { + publisher_key: string + app_id: number + } + + export interface ConfigFacebookInstantGame { + app_secret: string + } + + export interface ConfigFacebookLimitedLogin { + app_id: string + } + + export interface ConfigApple { + bundle_id: string + } + + export interface ConfigRuntime { + env: string[] + http_key: string + } + + export interface ConfigIAP { + apple: ConfigIAPApple + google: ConfigIAPGoogle + huawei: ConfigIAPHuawei + facebook_instant: ConfigIAPFacebookInstant + } + + export interface ConfigIAPApple { + shared_password: string + notifications_endpoint_id: string + } + + export interface ConfigIAPGoogle { + client_email: string + private_key: string + notifications_endpoint_id: string + refund_check_period_min: number + package_name: string + } + + export interface ConfigIAPHuawei { + public_key: string + client_id: string + client_secret: string + } + + export interface ConfigIAPFacebookInstant { + public_key: string + client_id: string + client_secret: string + } + + export interface ConfigGoogleAuth { + credentials_json: string + } + + export interface ConfigSatori { + url: string + api_key_name: string + api_key: string + signing_key: string + } + const enum PresenceReason { PresenceReasonUnknown = 0, PresenceReasonJoin = 1, diff --git a/runtime/config.go b/runtime/config.go new file mode 100644 index 0000000..6784eaf --- /dev/null +++ b/runtime/config.go @@ -0,0 +1,130 @@ +// Copyright 2024 The Nakama Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package runtime + +// Config interface is the Nakama core configuration. +type Config interface { + GetName() string + GetShutdownGraceSec() int + GetLogger() LoggerConfig + GetSession() SessionConfig + GetSocket() SocketConfig + GetSocial() SocialConfig + GetRuntime() RuntimeConfig + GetIAP() IAPConfig + GetGoogleAuth() GoogleAuthConfig + GetSatori() SatoriConfig +} + +// LoggerConfig is configuration relevant to logging levels and output. +type LoggerConfig interface { + GetLevel() string +} + +// SessionConfig is configuration relevant to the session. +type SessionConfig interface { + GetEncryptionKey() string + GetTokenExpirySec() int64 + GetRefreshEncryptionKey() string + GetRefreshTokenExpirySec() int64 + GetSingleSocket() bool + GetSingleMatch() bool + GetSingleParty() bool + GetSingleSession() bool +} + +// SocketConfig is configuration relevant to the transport socket and protocol. +type SocketConfig interface { + GetServerKey() string + GetPort() int + GetAddress() string + GetProtocol() string +} + +// SocialConfig is configuration relevant to the social authentication providers. +type SocialConfig interface { + GetSteam() SocialConfigSteam + GetFacebookInstantGame() SocialConfigFacebookInstantGame + GetFacebookLimitedLogin() SocialConfigFacebookLimitedLogin + GetApple() SocialConfigApple +} + +// SocialConfigSteam is configuration relevant to Steam. +type SocialConfigSteam interface { + GetPublisherKey() string + GetAppID() int +} + +// SocialConfigFacebookInstantGame is configuration relevant to Facebook Instant Games. +type SocialConfigFacebookInstantGame interface { + GetAppSecret() string +} + +// SocialConfigFacebookLimitedLogin is configuration relevant to Facebook Limited Login. +type SocialConfigFacebookLimitedLogin interface { + GetAppId() string +} + +// SocialConfigApple is configuration relevant to Apple Sign In. +type SocialConfigApple interface { + GetBundleId() string +} + +// RuntimeConfig is configuration relevant to the Runtimes. +type RuntimeConfig interface { + GetEnv() []string + GetHTTPKey() string +} + +type IAPConfig interface { + GetApple() IAPAppleConfig + GetGoogle() IAPGoogleConfig + GetHuawei() IAPHuaweiConfig + GetFacebookInstant() IAPFacebookInstantConfig +} + +type IAPAppleConfig interface { + GetSharedPassword() string + GetNotificationsEndpointId() string +} + +type IAPGoogleConfig interface { + GetClientEmail() string + GetPrivateKey() string + GetNotificationsEndpointId() string + GetRefundCheckPeriodMin() int + GetPackageName() string +} + +type SatoriConfig interface { + GetUrl() string + GetApiKeyName() string + GetApiKey() string + GetSigningKey() string +} + +type IAPHuaweiConfig interface { + GetPublicKey() string + GetClientID() string + GetClientSecret() string +} + +type IAPFacebookInstantConfig interface { + GetAppSecret() string +} + +type GoogleAuthConfig interface { + GetCredentialsJSON() string +} diff --git a/runtime/runtime.go b/runtime/runtime.go index fb4900c..d46c5da 100644 --- a/runtime/runtime.go +++ b/runtime/runtime.go @@ -316,6 +316,10 @@ It is made available to the InitModule function as an input parameter when the f NOTE: You must not cache the reference to this and reuse it as a later point as this could have unintended side effects. */ type Initializer interface { + /* + GetConfig returns a read only subset of the Nakama configuration values. + */ + GetConfig() (Config, error) /* RegisterRpc registers a function with the given ID. This ID can be used within client code to send an RPC message to execute the function and return the result. Results are always returned as a JSON string (or optionally empty string).