From 2958bf2ec28b37d003d64c1d244d384c838fbbb2 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 17 Jul 2024 15:02:32 +0200 Subject: [PATCH] Make all config structs suitable for config.FromEnv() --- config/tls.go | 10 +++++----- database/config.go | 14 +++++++------- database/db.go | 10 +++++----- logging/config.go | 8 ++++---- redis/config.go | 24 ++++++++++++------------ 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/config/tls.go b/config/tls.go index 9286ce83..d159c3bc 100644 --- a/config/tls.go +++ b/config/tls.go @@ -9,11 +9,11 @@ import ( // TLS provides TLS configuration options. type TLS struct { - Enable bool `yaml:"tls"` - Cert string `yaml:"cert"` - Key string `yaml:"key"` - Ca string `yaml:"ca"` - Insecure bool `yaml:"insecure"` + Enable bool `yaml:"tls" env:"TLS"` + Cert string `yaml:"cert" env:"CERT"` + Key string `yaml:"key" env:"KEY"` + Ca string `yaml:"ca" env:"CA"` + Insecure bool `yaml:"insecure" env:"INSECURE"` } // MakeConfig assembles a tls.Config from t and serverName. diff --git a/database/config.go b/database/config.go index bfcf299e..80c69fba 100644 --- a/database/config.go +++ b/database/config.go @@ -7,14 +7,14 @@ import ( // Config defines database client configuration. type Config struct { - Type string `yaml:"type" default:"mysql"` - Host string `yaml:"host"` - Port int `yaml:"port"` - Database string `yaml:"database"` - User string `yaml:"user"` - Password string `yaml:"password"` + Type string `yaml:"type" env:"TYPE" default:"mysql"` + Host string `yaml:"host" env:"HOST"` + Port int `yaml:"port" env:"PORT"` + Database string `yaml:"database" env:"DATABASE"` + User string `yaml:"user" env:"USER"` + Password string `yaml:"password" env:"PASSWORD,unset"` TlsOptions config.TLS `yaml:",inline"` - Options Options `yaml:"options"` + Options Options `yaml:"options" envPrefix:"OPTIONS_"` } // Validate checks constraints in the supplied database configuration and returns an error if they are violated. diff --git a/database/db.go b/database/db.go index d024e421..f0897b50 100644 --- a/database/db.go +++ b/database/db.go @@ -46,29 +46,29 @@ type DB struct { // Options define user configurable database options. type Options struct { // Maximum number of open connections to the database. - MaxConnections int `yaml:"max_connections" default:"16"` + MaxConnections int `yaml:"max_connections" env:"MAX_CONNECTIONS" default:"16"` // Maximum number of connections per table, // regardless of what the connection is actually doing, // e.g. INSERT, UPDATE, DELETE. - MaxConnectionsPerTable int `yaml:"max_connections_per_table" default:"8"` + MaxConnectionsPerTable int `yaml:"max_connections_per_table" env:"MAX_CONNECTIONS_PER_TABLE" default:"8"` // MaxPlaceholdersPerStatement defines the maximum number of placeholders in an // INSERT, UPDATE or DELETE statement. Theoretically, MySQL can handle up to 2^16-1 placeholders, // but this increases the execution time of queries and thus reduces the number of queries // that can be executed in parallel in a given time. // The default is 2^13, which in our tests showed the best performance in terms of execution time and parallelism. - MaxPlaceholdersPerStatement int `yaml:"max_placeholders_per_statement" default:"8192"` + MaxPlaceholdersPerStatement int `yaml:"max_placeholders_per_statement" env:"MAX_PLACEHOLDERS_PER_STATEMENT" default:"8192"` // MaxRowsPerTransaction defines the maximum number of rows per transaction. // The default is 2^13, which in our tests showed the best performance in terms of execution time and parallelism. - MaxRowsPerTransaction int `yaml:"max_rows_per_transaction" default:"8192"` + MaxRowsPerTransaction int `yaml:"max_rows_per_transaction" env:"MAX_ROWS_PER_TRANSACTION" default:"8192"` // WsrepSyncWait enforces Galera cluster nodes to perform strict cluster-wide causality checks // before executing specific SQL queries determined by the number you provided. // Please refer to the below link for a detailed description. // https://icinga.com/docs/icinga-db/latest/doc/03-Configuration/#galera-cluster - WsrepSyncWait int `yaml:"wsrep_sync_wait" default:"7"` + WsrepSyncWait int `yaml:"wsrep_sync_wait" env:"WSREP_SYNC_WAIT" default:"7"` } // Validate checks constraints in the supplied database options and returns an error if they are violated. diff --git a/logging/config.go b/logging/config.go index 00eb1406..0bf64ab1 100644 --- a/logging/config.go +++ b/logging/config.go @@ -14,12 +14,12 @@ type Options map[string]zapcore.Level // Config defines Logger configuration. type Config struct { // zapcore.Level at 0 is for info level. - Level zapcore.Level `yaml:"level" default:"0"` - Output string `yaml:"output"` + Level zapcore.Level `yaml:"level" env:"LEVEL" default:"0"` + Output string `yaml:"output" env:"OUTPUT"` // Interval for periodic logging. - Interval time.Duration `yaml:"interval" default:"20s"` + Interval time.Duration `yaml:"interval" env:"INTERVAL" default:"20s"` - Options `yaml:"options"` + Options `yaml:"options" env:"OPTIONS"` } // Validate checks constraints in the configuration and returns an error if they are violated. diff --git a/redis/config.go b/redis/config.go index 1d3f59d1..eb3107fb 100644 --- a/redis/config.go +++ b/redis/config.go @@ -8,12 +8,12 @@ import ( // Options define user configurable Redis options. type Options struct { - BlockTimeout time.Duration `yaml:"block_timeout" default:"1s"` - HMGetCount int `yaml:"hmget_count" default:"4096"` - HScanCount int `yaml:"hscan_count" default:"4096"` - MaxHMGetConnections int `yaml:"max_hmget_connections" default:"8"` - Timeout time.Duration `yaml:"timeout" default:"30s"` - XReadCount int `yaml:"xread_count" default:"4096"` + BlockTimeout time.Duration `yaml:"block_timeout" env:"BLOCK_TIMEOUT" default:"1s"` + HMGetCount int `yaml:"hmget_count" env:"HMGET_COUNT" default:"4096"` + HScanCount int `yaml:"hscan_count" env:"HSCAN_COUNT" default:"4096"` + MaxHMGetConnections int `yaml:"max_hmget_connections" env:"MAX_HMGET_CONNECTIONS" default:"8"` + Timeout time.Duration `yaml:"timeout" env:"TIMEOUT" default:"30s"` + XReadCount int `yaml:"xread_count" env:"XREAD_COUNT" default:"4096"` } // Validate checks constraints in the supplied Redis options and returns an error if they are violated. @@ -42,13 +42,13 @@ func (o *Options) Validate() error { // Config defines Config client configuration. type Config struct { - Host string `yaml:"host"` - Port int `yaml:"port"` - Username string `yaml:"username"` - Password string `yaml:"password"` - Database int `yaml:"database" default:"0"` + Host string `yaml:"host" env:"HOST"` + Port int `yaml:"port" env:"PORT"` + Username string `yaml:"username" env:"USERNAME"` + Password string `yaml:"password" env:"PASSWORD,unset"` + Database int `yaml:"database" env:"DATABASE" default:"0"` TlsOptions config.TLS `yaml:",inline"` - Options Options `yaml:"options"` + Options Options `yaml:"options" envPrefix:"OPTIONS_"` } // Validate checks constraints in the supplied Config configuration and returns an error if they are violated.