Settings and stored state for a Go plugin

New user here. I have a Go plugin that works fine. Now I would like to be able to have the plugin load some static settings when it initializes as well as save some state to persistent storage. For saving state I assume the Storage module is the way to go, but I haven’t seen any nice way to load settings. I could of course load and parse a file myself from a hard coded location, but I would like to avoid that if possible. Ideally I would like to append my own settings to the file given to Nakama via the --config flag.

Is there anything obvious I’m missing?

Seems the storage engine always requires a “user id” with stored data. Can’t really find any API docs for the Go plugin runtime, but this seems to be the case. My settings aren’t specific to a single user, but instead global for the entire server.

Apparently you should be able to get all server config from the context using the RUNTIME_CTX_ENV key. At least the docs say:

A table of key/value pairs which are defined in the YAML configuration of the server. This is useful to store API keys and other secrets which may be different between servers run in production and in development.

This looks promising. So I add in the following to the file local.yaml which is given to the server when starting:

# other settings
...

my_data:
  my_key1: 1
  my_key2: 42

I start the server as:

% ./nakama --config ./local.yml <other options>

I then try to get these settings with:

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	logger.Info("Initializing module")

	config, ok := ctx.Value(runtime.RUNTIME_CTX_ENV).(map[string]string)
	if !ok {
		return errors.New("invalid context")
	}

	logger.Info("config: %v, %d", config, len(config))
...

But config is always empty. Is there something I don’t grok here, or is this not how it should work? I’m new at using Go, so it may be a stupid noob mistake too.

Not sure about the Nakama version, this looks wrong to me:

% git branch
* (HEAD detached at v3.4.0)
  master
% ./nakama --version
3.0.0+dev

Is the version in main.go not updated or am I holding this wrong?

What you’re looking for is the runtime environment configuration. You can specify key-value pairs in Nakama’s configuration:

runtime:
  env:
    - "FOO=bar"
    - "BAZ=qux"

And read them in the runtime (Go example):

func InitModule(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, initializer runtime.Initializer) error {
	env := ctx.Value(runtime.RUNTIME_CTX_ENV).(map[string]string)
	logger.Info(env["FOO"]) // Prints: bar
}

Separately:

Not sure about the Nakama version, this looks wrong to me

The version is not set if you build from source. We recommend using one of the official releases or Docker images where possible.

1 Like

Thank you for clearing this up. I spent a long time trying various ways to get settings in and was about to start parsing an own config file. To me the below documentation for runtime.env did not tell me that this is what I should use…

List of Key-Value properties that are exposed to the Runtime scripts as environment variables.