-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
58 lines (47 loc) · 1.2 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
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/spf13/viper"
)
const (
ENV_VAR_KEY_APP_ENV = "APP_ENV"
)
var (
DEFAULT_CONFIG_PATH = []string{"/config.yml", "/config/config.yml"}
_, absPath, _, _ = runtime.Caller(0)
basepath = filepath.Dir(absPath)
)
// NewConfig initialize
func NewConfig() {
configPath, error := locateConfigFile()
fmt.Println(configPath)
fmt.Println(error)
}
func locateConfigFile() (configPath string, err error) {
isFound := false
fmt.Println("Locating configuration file...")
viper.SetEnvPrefix(os.Getenv(ENV_VAR_KEY_APP_ENV))
viper.SetConfigName("config")
viper.SetConfigType("yaml")
for index := 0; index < len(DEFAULT_CONFIG_PATH); index++ {
configPath = basepath + DEFAULT_CONFIG_PATH[index]
viper.AddConfigPath(basepath + DEFAULT_CONFIG_PATH[index])
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
continue
}
}
if isFound {
return configPath, nil
}
return "", errors.New("config file not found")
}
func loadConfigFile() {
if os.Getenv(ENV_VAR_KEY_APP_ENV) == "" {
os.Setenv(ENV_VAR_KEY_APP_ENV, "development")
}
}