-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
158 lines (137 loc) · 4.46 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package config
import (
"github.com/pkg/errors"
"github.com/spf13/viper"
"os"
"path/filepath"
"time"
)
var (
appName = "web"
configuration *Configuration
prodConfigFileName = "config_prod.yaml"
devConfigFileName = "config_dev.yaml"
configType = "yaml"
configDir = "."
)
type Configuration struct {
IsProduction bool
Server ServerConfig
Database DbConfig
Redis RedisConfig
}
type ServerConfig struct {
Domain string `default:"http://localhost"`
Port string `default:"3000"`
Environment string `default:"development"` // development,test,production
JwtSecret string `default:"bisi-bisi-bisi"`
ReadTimeout int `default:"5"`
WriteTimeout int `default:"10"`
IdleTimeout int `default:"120"`
LogPath string `default:"stdout"`
LogLevel string `default:"info"`
UploadDir string `default:"./upload"`
}
type DbConfig struct {
Name string `default:"web"`
Username string `default:"web"`
Password string `default:"web?????"`
Host string `default:"localhost"`
Port string `default:"5432"`
LogMode bool `default:"false"`
MaxPoolSize int `default:"10"`
MaxIdleConn int `default:"1"`
}
type RedisConfig struct {
Host string
Password string
Db int
WriteTimeout time.Duration
}
func setDefaults() {
viper.SetDefault("isProduction", os.Getenv("APP_ENV") == "PRODUCTION")
viper.SetDefault("server.domain", "http://localhost")
viper.SetDefault("server.port", 3000)
viper.SetDefault("server.environment", "development")
viper.SetDefault("server.logPath", "stdout")
viper.SetDefault("server.logLevel", "debug")
viper.SetDefault("server.secret", "bisi-bisi-bisi")
viper.SetDefault("server.readTimeout", 5)
viper.SetDefault("server.writeTimeout", 10)
viper.SetDefault("server.IdleTimeout", 120)
viper.SetDefault("server.uploadDir", "./upload")
viper.SetDefault("database.name", "web")
viper.SetDefault("database.username", "web")
viper.SetDefault("database.password", "web?????")
viper.SetDefault("database.host", "localhost")
viper.SetDefault("database.port", 5432)
viper.SetDefault("database.logmode", false)
viper.SetDefault("database.maxPoolSize", 10)
viper.SetDefault("database.maxIdleConn", 1)
viper.SetDefault("redis.host", "web.com:6379")
viper.SetDefault("redis.password", "asdf")
viper.SetDefault("redis.db", 9)
viper.SetDefault("redis.writeTimeout", time.Second*5)
}
// os environment'ından okumak için. şimdilik çokda lazım değil..
func bindEnvs() {
viper.BindEnv("server.domain", "DOMAIN")
viper.BindEnv("server.port", "PORT")
viper.BindEnv("server.environment", "ENVIRONMENT")
viper.BindEnv("server.logPath", "LOG_PATH")
viper.BindEnv("server.secret", "SERVER_SECRET")
viper.BindEnv("server.timeout", "SERVER_TIMEOUT")
viper.BindEnv("database.name", "DB_NAME")
viper.BindEnv("database.username", "DB_USERNAME")
viper.BindEnv("database.password", "DB_PASSWORD")
viper.BindEnv("database.host", "DB_HOST")
viper.BindEnv("database.port", "DB_PORT")
viper.BindEnv("database.logmode", "DB_LOG_MODE")
}
// read configuration from file
func readConfiguration(path, filename string) error {
dir, err := filepath.Abs(filepath.Dir(path))
if err != nil {
return errors.Wrapf(err, "Abssulte path oluşturulamadı.")
}
err = viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
// if file does not exist, simply create one
if _, err := os.Stat(dir + "/" + filename); os.IsNotExist(err) {
os.Create(dir + "/" + filename)
} else if err != nil {
return errors.Wrapf(err, "Config dosyası olşuturulamadı.")
}
// let's write defaults
if err2 := viper.WriteConfig(); err2 != nil {
return errors.Wrapf(err2, "Yeni oluşturulan Config dosyasına yazılamadı.")
}
}
return nil
}
func Setup() (*Configuration, error) {
var configFilename string
if os.Getenv("APP_ENV") == "PRODUCTION" {
configFilename = prodConfigFileName
} else {
configFilename = devConfigFileName
}
viper.SetConfigName(configFilename)
viper.SetConfigType(configType)
viper.AddConfigPath(configDir)
//bindEnvs()
setDefaults()
if err := readConfiguration(configDir, configFilename); err != nil {
return nil, err
}
// Auto read env variables
viper.AutomaticEnv()
// Unmarshal config file to struct
if err := viper.Unmarshal(&configuration); err != nil {
return nil, err
}
return configuration, nil
}
func Get() *Configuration {
return configuration
}