Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add env variable support for config #24

Merged
merged 1 commit into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading