# Settings and stored state for a Go plugin

**URL:** https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867
**Category:** Game Design
**Tags:** server-framework
**Created:** [August 9, 2021, 8:19pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867 "2021-08-09T20:19:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)
#### Post date: [August 9, 2021, 8:19pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/1 "2021-08-09T20:19:55Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)
#### Post date: [August 9, 2021, 8:35pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/2 "2021-08-09T20:35:11Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)
#### Post date: [August 15, 2021, 5:31pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/3 "2021-08-15T17:31:19Z")

</div>

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](https://heroiclabs.com/docs/nakama/getting-started/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:

```yaml
# other settings
...

my_data:
  my_key1: 1
  my_key2: 42

```

I start the server as:

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

```

I then try to get these settings with:

```go
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.

---

<div class="post-metadata">

### Author: ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)
#### Post date: [August 15, 2021, 5:34pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/4 "2021-08-15T17:34:39Z")

</div>

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

```bash
% 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?

---

<div class="post-metadata">

### Author: ![zyro](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/zyro/32/14_2.png) [@zyro](https://forum.heroiclabs.com/u/zyro)
#### Post date: [August 16, 2021, 2:05pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/5 "2021-08-16T14:05:20Z")

</div>

What you’re looking for is the [runtime environment configuration](https://heroiclabs.com/docs/nakama/getting-started/configuration/#runtime). You can specify key-value pairs in Nakama’s configuration:

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

```

And read them in the runtime (Go example):

```auto
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](https://github.com/heroiclabs/nakama/releases) or [Docker images](https://hub.docker.com/r/heroiclabs/nakama) where possible.

---

<div class="post-metadata">

### Author: ![chakie](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/chakie/32/772_2.png) [@chakie](https://forum.heroiclabs.com/u/chakie)
#### Post date: [August 16, 2021, 3:41pm UTC](https://forum.heroiclabs.com/t/settings-and-stored-state-for-a-go-plugin/1867/6 "2021-08-16T15:41:49Z")

</div>

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.
