planetmint-go/config/config.go
Julian Strobl 4ba12f7b03
[config] Add planetmint section to app.toml
This patch extends `app.toml` and adds the following section with these
default values:

```
[planetmint]
watchmen-endpoint = "localhost"
watchmen-port = 7401
```

A global singleton `plmntConfig` is introduced to save and access the
values similar to how cosmos does it (see
vendor/github.com/cosmos/cosmos-sdk/types/config.go).

Different environments can be managed by changing the values in
`app.toml` and restarting the daemon.

// Closes #53

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
2023-08-04 10:53:38 +02:00

66 lines
1.7 KiB
Go

package app
import (
"encoding/json"
"fmt"
"sync"
)
const DefaultConfigTemplate = `
###############################################################################
### Planetmint ###
###############################################################################
[planetmint]
watchmen-endpoint = "{{ .PlmntConfig.WatchmenConfig.Endpoint }}"
watchmen-port = {{ .PlmntConfig.WatchmenConfig.Port }}
`
// Config defines Planetmint's top level configuration
type Config struct {
WatchmenConfig WatchmenConfig `mapstructure:"watchmen-config" json:"watchmen-config"`
}
// WatchmenConfig defines Planetmint's watchmen configuration
type WatchmenConfig struct {
Endpoint string `mapstructure:"watchmen-endpoint" json:"watchmen-endpoint"`
Port int `mapstructure:"watchmen-port" json:"watchmen-port"`
}
// cosmos-sdk wide global singleton
var (
plmntConfig *Config
initConfig sync.Once
)
// DefaultConfig returns planetmint's default configuration.
func DefaultConfig() *Config {
return &Config{
WatchmenConfig: WatchmenConfig{
Endpoint: "localhost",
Port: 7401,
},
}
}
// GetConfig returns the config instance for the SDK.
func GetConfig() *Config {
initConfig.Do(func() {
plmntConfig = DefaultConfig()
})
return plmntConfig
}
// SetWatchmenConfig sets Planetmint's watchmen configuration
func (config *Config) SetWatchmenConfig(watchmenConfig interface{}) {
jsonWatchmenConfig, err := json.Marshal(watchmenConfig)
if err != nil {
panic(err)
}
err = json.Unmarshal(jsonWatchmenConfig, &config.WatchmenConfig)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", config.WatchmenConfig.Port)
}