-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
137 lines (118 loc) · 5.59 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"errors"
"fmt"
"os"
"time"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap/zapcore"
"gopkg.in/yaml.v3"
)
// Config defines the structure of the configuration file.
type Config struct {
GitCommit string `yaml:"git_commit" envconfig:"DRAP_GIT_COMMIT"`
GitTag string `yaml:"git_tag" envconfig:"DRAP_GIT_TAG"`
BuildTime string `yaml:"build_time" envconfig:"DRAP_BUILD_TIME"`
IsProduction bool `yaml:"is_production" envconfig:"DRAP_IS_PRODUCTION"`
LogLevel zapcore.Level `yaml:"log_level" envconfig:"DRAP_LOG_LEVEL"`
LogFolder string `yaml:"log_folder" envconfig:"DRAP_LOG_FOLDER"`
LogMaxSize int `yaml:"log_max_size" envconfig:"DRAP_LOG_MAX_SIZE"`
ProfilerEndpointsEnable bool `yaml:"profiler_endpoints_enable" envconfig:"DRAP_PROFILER_ENDPOINTS_ENABLE"`
OpsEndpointsEnable bool `yaml:"ops_endpoints_enable" envconfig:"DRAP_OPS_ENDPOINTS_ENABLE"`
Server ServerConfig `yaml:"server"`
Redis RedisConfig `yaml:"redis"`
BoltDB BoltDBConfig `yaml:"boltdb"`
}
type ServerConfig struct {
Host string `yaml:"host" envconfig:"DRAP_SERVER_HOST"`
Port string `yaml:"port" envconfig:"DRAP_SERVER_PORT"`
CertsFile string `yaml:"certs_file" envconfig:"DRAP_SERVER_CERTS_FILE"`
KeyFile string `yaml:"key_file" envconfig:"DRAP_SERVER_KEY_FILE"`
ReadTimeout time.Duration `yaml:"read_timeout" envconfig:"DRAP_SERVER_READ_TIMEOUT"`
WriteTimeout time.Duration `yaml:"write_timeout" envconfig:"DRAP_SERVER_WRITE_TIMEOUT"`
LongRequestProcessingTimeout time.Duration `yaml:"long_request_processing_timeout" envconfig:"DRAP_SERVER_LONG_REQUEST_PROCESSING_TIMEOUT"`
LongRequestWriteTimeout time.Duration `yaml:"long_request_write_timeout" envconfig:"DRAP_SERVER_LONG_REQUEST_WRITE_TIMEOUT"`
RequestTimeout time.Duration `yaml:"request_timeout" envconfig:"DRAP_SERVER_REQUEST_TIMEOUT"` // Time to wait for a request to finish
ShutdownTimeout time.Duration `yaml:"shutdown_timeout" envconfig:"DRAP_SERVER_SHUTDOWN_TIMEOUT"`
}
type RedisConfig struct {
Host string `yaml:"host" envconfig:"DRAP_REDIS_HOST"`
Port string `yaml:"port" envconfig:"DRAP_REDIS_PORT"`
DialTimeout time.Duration `yaml:"dial_timeout" envconfig:"DRAP_REDIS_DIAL_TIMEOUT"`
ReadTimeout time.Duration `yaml:"read_timeout" envconfig:"DRAP_REDIS_READ_TIMEOUT"`
WriteTimeout time.Duration `yaml:"write_timeout" envconfig:"DRAP_REDIS_WRITE_TIMEOUT"`
PoolSize int `yaml:"pool_size" envconfig:"DRAP_REDIS_POOL_SIZE"`
PoolTimeout time.Duration `yaml:"pool_timeout" envconfig:"DRAP_REDIS_POOL_TIMEOUT"`
Username string `yaml:"username" envconfig:"DRAP_REDIS_USERNAME"`
Password string `yaml:"password" envconfig:"DRAP_REDIS_PASSWORD"`
DatabaseIndex int `yaml:"db_index" envconfig:"DRAP_REDIS_DATABASE_INDEX"`
}
type BoltDBConfig struct {
FilePath string `yaml:"filepath" envconfig:"DRAP_BOLTDB_FILE_PATH"`
Timeout time.Duration `yaml:"timeout" envconfig:"DRAP_BOLTDB_TIMEOUT"`
BucketName string `yaml:"bucket_name" envconfig:"DRAP_BOLTDB_BUCKET_NAME"`
}
// LoadConfigFile provides an instance of config structure for the all application.
func LoadConfigFile(configFile string) (*Config, error) {
file, err := os.Open(configFile)
if err != nil {
return nil, err
}
defer file.Close()
cfg := &Config{}
yd := yaml.NewDecoder(file)
err = yd.Decode(cfg)
if err != nil {
return nil, err
}
return cfg, nil
}
// LoadConfigEnv reads the environments variables and provides an instance of the App config.
func LoadConfigEnvs(prefix string, config *Config) error {
return envconfig.Process(prefix, config)
}
// InitConfig setup defaults values for non provided parameters
// and configures build tags values to be used if provided.
func InitConfig(config *Config, gitCommit, gitTag, buildTime string) error {
if len(gitCommit) != 0 {
config.GitCommit = gitCommit
}
if len(gitTag) != 0 {
config.GitTag = gitTag
}
if len(buildTime) != 0 {
config.BuildTime = buildTime
}
if len(config.Server.Host) == 0 || len(config.Server.Port) == 0 {
return errors.New("make sure to set valid server address and port in configuration file")
}
if len(config.Redis.Host) == 0 || len(config.Redis.Port) == 0 {
return errors.New("make sure to set valid redis address and port in configuration file")
}
return nil
}
// LoadAndInitConfigs loads in order the configs from various predefined sources
// then build the App configuration data.
func LoadAndInitConfigs(gitCommit, gitTag, buildTime string) (*Config, error) {
// Setup the yaml configuration from file.
config, err := LoadConfigFile("./config.yml")
if err != nil {
return config, fmt.Errorf("failed to load configurations from file: %s", err)
}
// Set the environment configuration.
err = godotenv.Load("./config.env")
if err != nil {
return config, fmt.Errorf("failed to set environment configurations: %s", err)
}
// Use environment variables with prefix `DRAP`.
err = LoadConfigEnvs("DRAP", config)
if err != nil {
return config, fmt.Errorf("failed to load configurations from environment: %s", err)
}
err = InitConfig(config, gitCommit, gitTag, buildTime)
if err != nil {
return config, fmt.Errorf("failed to initialize configurations: %s", err)
}
return config, nil
}