Skip to content

Commit

Permalink
feat: Add env variable support for config (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari authored Aug 15, 2024
1 parent 9a0694d commit a1cd60e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
41 changes: 33 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package config

import (
"fmt"
"os"
"strings"

"github.com/mitchellh/mapstructure"
"github.com/ojo-network/ethereum-api/pool"
"github.com/ojo-network/indexer/server"
"gopkg.in/yaml.v3"
"github.com/spf13/viper"
)

const (
configFileType = "yaml"
envVariablePrefix = "eth_api"
structTagName = "yaml"
)

type Exchange struct {
Expand All @@ -23,15 +30,33 @@ type Config struct {
func ParseConfig(filePath string) (*Config, error) {
config := Config{}

yamlFile, err := os.ReadFile(filePath)
if err != nil {
return nil, err
vpr := viper.New()
vpr.SetConfigFile(filePath)
vpr.SetConfigType(configFileType)
vpr.SetEnvPrefix(envVariablePrefix)
vpr.AutomaticEnv()
vpr.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))

if err := vpr.ReadInConfig(); err != nil {
return &config, fmt.Errorf("couldn't load config: %s", err)
}

tagConfig := func(decoderConfig *mapstructure.DecoderConfig) {
decoderConfig.TagName = structTagName
}

err = yaml.Unmarshal(yamlFile, &config)
if err != nil {
return nil, err
if err := vpr.Unmarshal(&config, tagConfig); err != nil {
return &config, fmt.Errorf("couldn't read config: %s", err)
}

// Split NodeUrls from environment variables
for i := range config.Exchanges {
envVarName := fmt.Sprintf("exchanges.%d.node_urls", i)
if v := vpr.GetString(envVarName); v != "" {
config.Exchanges[i].NodeUrls = strings.Split(v, ",")
}
}

return &config, config.Validate()
}

Expand Down
9 changes: 4 additions & 5 deletions sample-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
exchanges:
- name: "uniswap"
node_urls:
- "wss://ethereum-mainnet.core.chainstack.com/ws/5f899c2bcc20c7d53a77f049887752ca"
- "wss://ethereum.publicnode.com"
- "YOUR NODE URL HERE"
pools:
- address: "0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640"
base: "WETH"
Expand Down Expand Up @@ -55,7 +54,7 @@ exchanges:
invert_price: false
- name: "camelot"
node_urls:
- "wss://arbitrum-one-rpc.publicnode.com"
- "YOUR NODE URL HERE"
pools:
- address: "0x8Bf9C3975172c57E37b3D1c1348e9b5280FB3BaA"
base: "MILKTIA"
Expand All @@ -65,7 +64,7 @@ exchanges:
invert_price: false
- name: "balancer"
node_urls:
- "wss://mainnet.infura.io/ws/v3/6849a09aeeb044b592d46bcdce07ccef"
- "YOUR NODE URL HERE"
pools:
- address: "0x4216d5900a6109bba48418b5e2AB6cc4e61Cf477"
base: "AMPHRETH"
Expand Down Expand Up @@ -117,7 +116,7 @@ exchanges:
invert_price: false
- name: "pancake"
node_urls:
- "wss://mainnet.infura.io/ws/v3/6849a09aeeb044b592d46bcdce07ccef"
- "YOUR NODE URL HERE"
pools:
- address: "0x365EA9E5Cec960390f35b6509548e84073168A8B"
base: "SWBTC"
Expand Down

0 comments on commit a1cd60e

Please sign in to comment.