/
config.go
49 lines (42 loc) · 1.04 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package ethclient
import (
"fmt"
"github.com/kelseyhightower/envconfig"
"github.com/rs/zerolog/log"
)
const (
DefaultEnvPrefix = "ethclient"
nameLenLimit = 32
)
type Config struct {
EnablePrometheus bool `default:"true"`
RpcUrl string `required:"true"`
RpcName string `required:"true"`
FailoverRpcUrl string `required:"true"`
FailoverRpcName string `required:"true"`
}
func (c *Config) Valid() error {
if len(c.RpcName) >= nameLenLimit {
return fmt.Errorf("invalid RpcName: %s", c.RpcName)
}
if len(c.FailoverRpcName) >= nameLenLimit {
return fmt.Errorf("invalid RpcFailoverName: %s", c.FailoverRpcName)
}
return nil
}
func ConfigFromEnv() *Config {
config := &Config{}
envconfig.MustProcess(DefaultEnvPrefix, config)
if err := config.Valid(); err != nil {
log.Fatal().Msgf("%s", err)
}
return config
}
func ConfigFromEnvPrefix(prefix string) *Config {
config := &Config{}
envconfig.MustProcess(prefix, config)
if err := config.Valid(); err != nil {
log.Fatal().Msgf("%s", err)
}
return config
}