Skip to content

Commit

Permalink
Add runtime API to read Nakama config values (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
sesposito authored Nov 23, 2024
1 parent e24311a commit fd6420e
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
115 changes: 115 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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,
Expand Down
130 changes: 130 additions & 0 deletions runtime/config.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit fd6420e

Please sign in to comment.