Skip to content

Commit

Permalink
support setting an URL for chain.configPath & set default names for…
Browse files Browse the repository at this point in the history
… endpoints
  • Loading branch information
pk910 committed Aug 23, 2023
1 parent 9497e4b commit ae550a8
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package utils

import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"

"dario.cat/mergo"
"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -40,12 +45,29 @@ func ReadConfig(cfg *types.Config, path string) error {
if err != nil {
return err
}

} else {
f, err := os.Open(cfg.Chain.ConfigPath)
if err != nil {
return fmt.Errorf("error opening Chain Config file %v: %w", cfg.Chain.ConfigPath, err)
var reader io.Reader
if strings.HasPrefix(cfg.Chain.ConfigPath, "http://") || strings.HasPrefix(cfg.Chain.ConfigPath, "https://") {
client := &http.Client{Timeout: time.Second * 120}
resp, err := client.Get(cfg.Chain.ConfigPath)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("url: %v, result: %v %v", path, resp.StatusCode, resp.Status)
}
reader = resp.Body
} else {
f, err := os.Open(cfg.Chain.ConfigPath)
if err != nil {
return fmt.Errorf("error opening Chain Config file %v: %w", cfg.Chain.ConfigPath, err)
}
defer f.Close()
reader = f
}
decoder := yaml.NewDecoder(f)
decoder := yaml.NewDecoder(reader)
err = decoder.Decode(&chainConfig)
if err != nil {
return fmt.Errorf("error decoding Chain Config file %v: %v", cfg.Chain.ConfigPath, err)
Expand Down Expand Up @@ -100,6 +122,16 @@ func ReadConfig(cfg *types.Config, path string) error {
},
}
}
for idx, endpoint := range cfg.BeaconApi.Endpoints {
if endpoint.Name == "" {
url, _ := url.Parse(endpoint.Url)
if url != nil {
cfg.BeaconApi.Endpoints[idx].Name = url.Hostname()
} else {
cfg.BeaconApi.Endpoints[idx].Name = fmt.Sprintf("endpoint-%v", idx+1)
}
}
}
if cfg.BeaconApi.Endpoints == nil || len(cfg.BeaconApi.Endpoints) == 0 {
return fmt.Errorf("missing beacon node endpoints (need at least 1 endpoint to run the explorer)")
}
Expand Down Expand Up @@ -129,9 +161,8 @@ func readConfigFile(cfg *types.Config, path string) error {
decoder := yaml.NewDecoder(f)
err = decoder.Decode(cfg)
if err != nil {
return fmt.Errorf("error decoding config file %v: %v", path, err)
return fmt.Errorf("error decoding explorer config: %v", err)
}

return nil
}

Expand Down

0 comments on commit ae550a8

Please sign in to comment.