-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.go
79 lines (67 loc) · 1.49 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
package env
import (
"sync"
"github.com/spf13/viper"
)
type Config struct {
AppConfig AppConfig
PgConfig PgConfig
}
type AppConfig struct {
Name string
Env string
URL string
Port uint
LogFormat string
LogLevel string
SentryDsn string
}
type PgConfig struct {
Port uint
Host string
Username string
Password string
Database string
Schema string
SslMode string
}
var (
cfg Config
onceDefaultClient sync.Once
)
func Read() (*Config, error) {
var err error
onceDefaultClient.Do(func() {
viper.SetConfigType("env")
viper.AddConfigPath(".")
viper.SetConfigFile(".env")
viper.AutomaticEnv()
if viperErr := viper.ReadInConfig(); err != nil {
if _, ok := viperErr.(viper.ConfigFileNotFoundError); !ok {
err = viperErr
return
}
}
cfg = Config{
AppConfig: AppConfig{
Name: viper.GetString("APP_NAME"),
Env: viper.GetString("ENV"),
URL: viper.GetString("app.url"),
Port: viper.GetUint("PORT"),
LogFormat: viper.GetString("LOG_FORMAT"),
LogLevel: viper.GetString("LOG_LEVEL"),
SentryDsn: viper.GetString("SENTRY_DSN"),
},
PgConfig: PgConfig{
Port: viper.GetUint("PG_PORT"),
Host: viper.GetString("PG_HOST"),
Username: viper.GetString("PG_USERNAME"),
Password: viper.GetString("PG_PASSWORD"),
Database: viper.GetString("PG_DATABASE"),
Schema: viper.GetString("PG_SCHEMA"),
SslMode: viper.GetString("PG_SSLMODE"),
},
}
})
return &cfg, err
}