From a1cd60ec7ad414b9be91bc522973eed5af71eb3f Mon Sep 17 00:00:00 2001 From: ryanbajollari <54822716+rbajollari@users.noreply.github.com> Date: Thu, 15 Aug 2024 16:43:37 +0200 Subject: [PATCH] feat: Add env variable support for config (#24) --- config/config.go | 41 +++++++++++++++++++++++++++++++++-------- sample-config.yaml | 9 ++++----- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 1e34310..73cb5c2 100644 --- a/config/config.go +++ b/config/config.go @@ -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 { @@ -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() } diff --git a/sample-config.yaml b/sample-config.yaml index 964b0e8..7e0c84e 100644 --- a/sample-config.yaml +++ b/sample-config.yaml @@ -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" @@ -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" @@ -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" @@ -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"