-
Notifications
You must be signed in to change notification settings - Fork 15
/
env.go
198 lines (165 loc) · 4.57 KB
/
env.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package config
import (
"fmt"
"log"
"os"
"reflect"
"strconv"
"strings"
"toktik/logging"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
)
var EnvConfig = envConfigSchema{}
func (s *envConfigSchema) GetDSN() string {
return dsn
}
var dsn string
func init() {
envInit()
envValidate()
dsn = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s",
EnvConfig.PGSQL_HOST,
EnvConfig.PGSQL_USER,
EnvConfig.PGSQL_PASSWORD,
EnvConfig.PGSQL_DBNAME,
EnvConfig.PGSQL_PORT)
}
var defaultConfig = envConfigSchema{
ENV: "dev",
CONSUL_ADDR: "127.0.0.1:8500",
EXPORT_ENDPOINT: "127.0.0.1:4317",
PGSQL_HOST: "localhost",
PGSQL_PORT: "5432",
PGSQL_USER: "postgres",
PGSQL_PASSWORD: "",
PGSQL_DBNAME: "postgres",
REDIS_ADDR: "localhost:6379",
REDIS_PASSWORD: "",
REDIS_DB: 0,
STORAGE_TYPE: "s3",
MAX_REQUEST_BODY_SIZE: 200 * 1024 * 1024,
LOCAL_FS_LOCATION: "/tmp",
LOCAL_FS_BASEURL: "http://localhost/",
S3_ENDPOINT_URL: "http://localhost:9000",
S3_PUBLIC_URL: "http://localhost:9000",
S3_BUCKET: "bucket",
S3_SECRET_ID: "minio",
S3_SECRET_KEY: "12345678",
S3_PATH_STYLE: "true",
UNSPLASH_ACCESS_KEY: "access_key",
}
type envConfigSchema struct {
ENV string `env:"ENV,DREAM_ENV"`
CONSUL_ADDR string `env:"CONSUL_ADDR,DREAM_SERVICE_DISCOVERY_URI"`
EXPORT_ENDPOINT string
PGSQL_HOST string
PGSQL_PORT string
PGSQL_USER string
PGSQL_PASSWORD string
PGSQL_DBNAME string
REDIS_ADDR string
REDIS_PASSWORD string
REDIS_DB int
STORAGE_TYPE string
MAX_REQUEST_BODY_SIZE int
LOCAL_FS_LOCATION string
LOCAL_FS_BASEURL string
S3_ENDPOINT_URL string
S3_PUBLIC_URL string
S3_BUCKET string
S3_SECRET_ID string
S3_SECRET_KEY string
S3_PATH_STYLE string
UNSPLASH_ACCESS_KEY string
}
func (s *envConfigSchema) IsDev() bool {
return s.ENV == "dev" || s.ENV == "TESTING"
}
func envValidate() {
EnvConfig.CONSUL_ADDR = strings.TrimPrefix(EnvConfig.CONSUL_ADDR, "consul://")
}
// envInit Reads .env as environment variables and fill corresponding fields into EnvConfig.
// To use a value from EnvConfig , simply call EnvConfig.FIELD like EnvConfig.ENV
// Note: Please keep Env as the first field of envConfigSchema for better logging.
func envInit() {
err := godotenv.Load()
if err != nil {
log.Print("Error loading .env file, ignored")
}
v := reflect.ValueOf(defaultConfig)
typeOfV := v.Type()
for i := 0; i < v.NumField(); i++ {
envNameAlt := make([]string, 0)
fieldName := typeOfV.Field(i).Name
fieldType := typeOfV.Field(i).Type
fieldValue := v.Field(i).Interface()
envNameAlt = append(envNameAlt, fieldName)
if fieldTag, ok := typeOfV.Field(i).Tag.Lookup("env"); ok && len(fieldTag) > 0 {
tags := strings.Split(fieldTag, ",")
envNameAlt = append(envNameAlt, tags...)
}
switch fieldType {
case reflect.TypeOf(0):
{
configDefaultValue, ok := fieldValue.(int)
if !ok {
logging.Logger.WithFields(logrus.Fields{
"field": fieldName,
"type": "int",
"value": fieldValue,
"env": envNameAlt,
}).Warningf("Failed to parse default value")
continue
}
envValue := resolveEnv(envNameAlt, fmt.Sprintf("%d", configDefaultValue))
if EnvConfig.IsDev() {
fmt.Printf("Reading field[ %s ] default: %v env: %s\n", fieldName, configDefaultValue, envValue)
}
if len(envValue) > 0 {
envValueInteger, err := strconv.ParseInt(envValue, 10, 64)
if err != nil {
logging.Logger.WithFields(logrus.Fields{
"field": fieldName,
"type": "int",
"value": fieldValue,
"env": envNameAlt,
}).Warningf("Failed to parse env value, ignored")
continue
}
reflect.ValueOf(&EnvConfig).Elem().Field(i).SetInt(envValueInteger)
}
continue
}
case reflect.TypeOf(""):
{
configDefaultValue, ok := fieldValue.(string)
if !ok {
logging.Logger.WithFields(logrus.Fields{
"field": fieldName,
"type": "int",
"value": fieldValue,
"env": envNameAlt,
}).Warningf("Failed to parse default value")
continue
}
envValue := resolveEnv(envNameAlt, configDefaultValue)
if EnvConfig.IsDev() {
fmt.Printf("Reading field[ %s ] default: %v env: %s\n", fieldName, configDefaultValue, envValue)
}
if len(envValue) > 0 {
reflect.ValueOf(&EnvConfig).Elem().Field(i).SetString(envValue)
}
}
}
}
}
func resolveEnv(configKeys []string, defaultValue string) string {
for _, item := range configKeys {
envValue := os.Getenv(item)
if envValue != "" {
return envValue
}
}
return defaultValue
}