Skip to content

Commit

Permalink
fix config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Patel committed May 4, 2022
1 parent 386dfbc commit b1b2e76
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
3 changes: 0 additions & 3 deletions example-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ target_groups:
# URL scheme, use "http" or "https".
scheme: http

# Snapshot path on a node.
snapshot_path: "/snapshot/tar.bz2"

# ------------------------------------------------
# Discovery
# ------------------------------------------------
Expand Down
14 changes: 7 additions & 7 deletions types/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
)

type BasicAuth struct {
Username string `json:"username"`
Password string `json:"password"`
Username string `json:"username" yaml:"username"`
Password string `json:"password" yaml:"password"`
}

func (b *BasicAuth) Apply(header http.Header) {
Expand All @@ -35,18 +35,18 @@ func (b *BasicAuth) Apply(header http.Header) {
}

type BearerAuth struct {
Token string `json:"token"`
Token string `json:"token" yaml:"token"`
}

func (b *BearerAuth) Apply(header http.Header) {
header.Set("authorization", "Bearer "+b.Token)
}

type TLSConfig struct {
CAFile string `json:"ca_file"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
InsecureSkipVerify bool `json:"insecure_skip_verify"`
CAFile string `json:"ca_file" yaml:"ca_file"`
CertFile string `json:"cert_file" yaml:"cert_file"`
KeyFile string `json:"key_file" yaml:"key_file"`
InsecureSkipVerify bool `json:"insecure_skip_verify" yaml:"insecure_skip_verify"`
}

func init() {
Expand Down
32 changes: 18 additions & 14 deletions types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,36 @@ import (

// Config describes the root-level config file.
type Config struct {
ScrapeInterval time.Duration `json:"scrape_interval"`
TargetGroups []*TargetGroup `json:"target_groups"`
ScrapeInterval time.Duration `json:"scrape_interval" yaml:"scrape_interval"`
TargetGroups []*TargetGroup `json:"target_groups" yaml:"target_groups"`
}

// LoadConfig reads the config object from the file system.
func LoadConfig(filePath string) (*Config, error) {
configBytes, err := os.ReadFile(filePath)
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()

conf := new(Config)
confErr := yaml.Unmarshal(configBytes, conf)
decoder := yaml.NewDecoder(f)
decoder.KnownFields(true)
confErr := decoder.Decode(conf)
return conf, confErr
}

// TargetGroup explains how to retrieve snapshots from a group of Solana nodes.
type TargetGroup struct {
Group string `json:"group"`
Scheme string `json:"scheme"`
APIPath string `json:"api_path"`
BasicAuth *BasicAuth `json:"basic_auth"`
BearerAuth *BearerAuth `json:"bearer_auth"`
TLSConfig *TLSConfig `json:"tls_config"`
Group string `json:"group" yaml:"group"`
Scheme string `json:"scheme" yaml:"scheme"`
APIPath string `json:"api_path" yaml:"api_path"`
BasicAuth *BasicAuth `json:"basic_auth" yaml:"basic_auth"`
BearerAuth *BearerAuth `json:"bearer_auth" yaml:"bearer_auth"`
TLSConfig *TLSConfig `json:"tls_config" yaml:"tls_config"`

StaticTargets *StaticTargets `json:"static_targets"`
FileTargets *FileTargets `json:"file_targets"`
StaticTargets *StaticTargets `json:"static_targets" yaml:"static_targets"`
FileTargets *FileTargets `json:"file_targets" yaml:"file_targets"`
}

func (t *TargetGroup) Discoverer() discovery.Discoverer {
Expand All @@ -67,7 +71,7 @@ func (t *TargetGroup) Discoverer() discovery.Discoverer {

// StaticTargets is a hardcoded list of Solana nodes.
type StaticTargets struct {
Targets []string `json:"targets"`
Targets []string `json:"targets" yaml:"targets"`
}

func (s *StaticTargets) DiscoverTargets(_ context.Context) ([]string, error) {
Expand All @@ -76,7 +80,7 @@ func (s *StaticTargets) DiscoverTargets(_ context.Context) ([]string, error) {

// FileTargets reads targets from a JSON file.
type FileTargets struct {
Path string `json:"path"`
Path string `json:"path" yaml:"path"`
}

func (d *FileTargets) DiscoverTargets(_ context.Context) ([]string, error) {
Expand Down
39 changes: 39 additions & 0 deletions types/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package types

import (
"path/filepath"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestLoadConfig(t *testing.T) {
_, testFile, _, ok := runtime.Caller(0)
assert.True(t, ok)
exampleConfig := filepath.Join(filepath.Dir(testFile), "../example-config.yml")

actual, err := LoadConfig(exampleConfig)
require.NoError(t, err)

expected := &Config{
ScrapeInterval: 15 * time.Second,
TargetGroups: []*TargetGroup{
{
Group: "mainnet",
Scheme: "http",
StaticTargets: &StaticTargets{
Targets: []string{
"solana-mainnet-1.example.org:8899",
"solana-mainnet-2.example.org:8899",
"solana-mainnet-3.example.org:8899",
},
},
},
},
}

assert.Equal(t, expected, actual)
}

0 comments on commit b1b2e76

Please sign in to comment.