Skip to content

Commit

Permalink
Refactoring default parameters in configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
0xERR0R committed Nov 5, 2021
1 parent bf06775 commit f823db1
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 79 deletions.
56 changes: 21 additions & 35 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 13,7 @@ import (
"time"

"github.com/0xERR0R/blocky/log"
"github.com/creasty/defaults"
"gopkg.in/yaml.v2"
)

Expand Down Expand Up @@ -238,35 239,27 @@ func extractNet(upstream string) (NetProtocol, string) {
return NetProtocolTcpUdp, upstream
}

const (
cfgDefaultPort = "53"
cfgDefaultPrometheusPath = "/metrics"
cfgDefaultUpstreamTimeout = Duration(2 * time.Second)
cfgDefaultRefreshPeriod = Duration(4 * time.Hour)
cfgDefaultDownloadTimeout = Duration(60 * time.Second)
)

// Config main configuration
// nolint:maligned
type Config struct {
Upstream UpstreamConfig `yaml:"upstream"`
UpstreamTimeout Duration `yaml:"upstreamTimeout"`
UpstreamTimeout Duration `yaml:"upstreamTimeout" default:"2s"`
CustomDNS CustomDNSConfig `yaml:"customDNS"`
Conditional ConditionalUpstreamConfig `yaml:"conditional"`
Blocking BlockingConfig `yaml:"blocking"`
ClientLookup ClientLookupConfig `yaml:"clientLookup"`
Caching CachingConfig `yaml:"caching"`
QueryLog QueryLogConfig `yaml:"queryLog"`
Prometheus PrometheusConfig `yaml:"prometheus"`
LogLevel log.Level `yaml:"logLevel"`
LogFormat log.FormatType `yaml:"logFormat"`
LogPrivacy bool `yaml:"logPrivacy"`
LogTimestamp bool `yaml:"logTimestamp"`
Port string `yaml:"port"`
LogLevel log.Level `yaml:"logLevel" default:"info"`
LogFormat log.FormatType `yaml:"logFormat" default:"text"`
LogPrivacy bool `yaml:"logPrivacy" default:"false"`
LogTimestamp bool `yaml:"logTimestamp" default:"true"`
Port string `yaml:"port" default:"53"`
HTTPPort string `yaml:"httpPort"`
HTTPSPort string `yaml:"httpsPort"`
TLSPort string `yaml:"tlsPort"`
DisableIPv6 bool `yaml:"disableIPv6"`
DisableIPv6 bool `yaml:"disableIPv6" default:"false"`
CertFile string `yaml:"certFile"`
KeyFile string `yaml:"keyFile"`
BootstrapDNS Upstream `yaml:"bootstrapDns"`
Expand All @@ -278,8 271,8 @@ type Config struct {

// PrometheusConfig contains the config values for prometheus
type PrometheusConfig struct {
Enable bool `yaml:"enable"`
Path string `yaml:"path"`
Enable bool `yaml:"enable" default:"false"`
Path string `yaml:"path" default:"/metrics"`
}

// UpstreamConfig upstream server configuration
Expand Down Expand Up @@ -313,11 306,11 @@ type BlockingConfig struct {
BlackLists map[string][]string `yaml:"blackLists"`
WhiteLists map[string][]string `yaml:"whiteLists"`
ClientGroupsBlock map[string][]string `yaml:"clientGroupsBlock"`
BlockType string `yaml:"blockType"`
BlockTTL Duration `yaml:"blockTTL"`
DownloadTimeout Duration `yaml:"downloadTimeout"`
RefreshPeriod Duration `yaml:"refreshPeriod"`
FailStartOnListError bool `yaml:"failStartOnListError"`
BlockType string `yaml:"blockType" default:"ZEROIP"`
BlockTTL Duration `yaml:"blockTTL" default:"6h"`
DownloadTimeout Duration `yaml:"downloadTimeout" default:"60s"`
RefreshPeriod Duration `yaml:"refreshPeriod" default:"4h"`
FailStartOnListError bool `yaml:"failStartOnListError" default:"false"`
}

// ClientLookupConfig configuration for the client lookup
Expand All @@ -333,8 326,8 @@ type CachingConfig struct {
MaxCachingTime Duration `yaml:"maxTime"`
MaxItemsCount int `yaml:"maxItemsCount"`
Prefetching bool `yaml:"prefetching"`
PrefetchExpires Duration `yaml:"prefetchExpires"`
PrefetchThreshold int `yaml:"prefetchThreshold"`
PrefetchExpires Duration `yaml:"prefetchExpires" default:"2h"`
PrefetchThreshold int `yaml:"prefetchThreshold" default:"5"`
PrefetchMaxItemsCount int `yaml:"prefetchMaxItemsCount"`
}

Expand All @@ -343,7 336,7 @@ type QueryLogConfig struct {
// Deprecated
Dir string `yaml:"dir"`
// Deprecated
PerClient bool `yaml:"perClient"`
PerClient bool `yaml:"perClient" default:"false"`
Target string `yaml:"target"`
Type QueryLogType `yaml:"type"`
LogRetentionDays uint64 `yaml:"logRetentionDays"`
Expand All @@ -355,7 348,9 @@ var config = &Config{}
// LoadConfig creates new config from YAML file
func LoadConfig(path string, mandatory bool) {
cfg := Config{}
setDefaultValues(&cfg)
if err := defaults.Set(&cfg); err != nil {
log.Log().Fatal("Can't apply default values: ", err)
}

data, err := ioutil.ReadFile(path)

Expand Down Expand Up @@ -427,12 422,3 @@ func validateConfig(cfg *Config) {
func GetConfig() *Config {
return config
}

func setDefaultValues(cfg *Config) {
cfg.Port = cfgDefaultPort
cfg.LogTimestamp = true
cfg.Prometheus.Path = cfgDefaultPrometheusPath
cfg.UpstreamTimeout = cfgDefaultUpstreamTimeout
cfg.Blocking.RefreshPeriod = cfgDefaultRefreshPeriod
cfg.Blocking.DownloadTimeout = cfgDefaultDownloadTimeout
}
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 4,9 @@ go 1.17

require (
github.com/0xERR0R/go-cache v1.6.0
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/asaskevich/EventBus v0.0.0-20200907212545-49d423059eef
github.com/creasty/defaults v1.5.2
github.com/go-chi/chi v4.1.2 incompatible
github.com/go-chi/cors v1.2.0
github.com/hashicorp/go-multierror v1.1.1
Expand All @@ -18,6 20,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
github.com/swaggo/swag v1.7.3
github.com/x-cray/logrus-prefixed-formatter v0.5.2
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
gopkg.in/yaml.v2 v2.4.0
Expand All @@ -27,17 30,25 @@ require (
)

require (
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/spec v0.20.3 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/mattn/go-sqlite3 v1.14.9 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
Expand All @@ -52,6 63,7 @@ require (
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/tools v0.1.2 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
Expand Down
Loading

0 comments on commit f823db1

Please sign in to comment.